From 6a9dc7b9e78c9246ad54bb42fa0cb9e63a5b9350 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 30 May 2025 08:44:37 +0200 Subject: [PATCH] refactor: extract execute deploy to module --- src/commands/deploy.ts | 86 +------------------ .../deploy.assert.services.ts} | 56 ++---------- src/services/deploy/deploy.assets.services.ts | 44 ++++++++++ .../deploy/deploy.execute.services.ts | 83 ++++++++++++++++++ 4 files changed, 135 insertions(+), 134 deletions(-) rename src/services/{deploy.services.ts => deploy/deploy.assert.services.ts} (51%) create mode 100644 src/services/deploy/deploy.assets.services.ts create mode 100644 src/services/deploy/deploy.execute.services.ts diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 960fe021..d59be8af 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -1,19 +1,8 @@ -import type {UploadFileStorage} from '@junobuild/cli-tools'; -import { - deploy as cliDeploy, - postDeploy as cliPostDeploy, - preDeploy as cliPreDeploy, - hasArgs -} from '@junobuild/cli-tools'; -import {uploadBlob, type Asset} from '@junobuild/core'; -import {red} from 'kleur'; -import {lstatSync} from 'node:fs'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; +import {hasArgs} from '@junobuild/cli-tools'; +import {junoConfigExist} from '../configs/juno.config'; import {clear} from '../services/clear.services'; -import {assertSatelliteMemorySize, listAssets} from '../services/deploy.services'; +import {executeDeploy} from '../services/deploy/deploy.execute.services'; import {links} from '../services/links.services'; -import {configEnv} from '../utils/config.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; import {init} from './init'; export const deploy = async (args?: string[]) => { @@ -29,72 +18,3 @@ export const deploy = async (args?: string[]) => { await links(args); }; - -const executeDeploy = async (args?: string[]) => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise => - await listAssets({ - startAfter, - env: { - env, - satellite: satelliteConfig - } - }); - - const assertMemory = async () => { - await assertSatelliteMemorySize(args); - }; - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); - - const uploadFile = async ({ - filename, - fullPath, - data, - collection, - headers, - encoding - }: UploadFileStorage) => { - await uploadBlob({ - satellite, - filename, - fullPath, - // @ts-expect-error type incompatibility NodeJS vs bundle - data, - collection, - headers, - encoding - }); - }; - - await cliPreDeploy({config: satelliteConfig}); - - const {result} = await cliDeploy({ - config: satelliteConfig, - listAssets: listExistingAssets, - assertSourceDirExists, - assertMemory, - uploadFile - }); - - if (result === 'skipped') { - process.exit(0); - } - - await cliPostDeploy({config: satelliteConfig}); -}; - -const assertSourceDirExists = (source: string) => { - try { - lstatSync(source); - } catch (_err: unknown) { - console.log( - `${red( - 'Cannot proceed deployment.' - )}\nAre you sure the folder containing your built app (the "source" tag in the configuration file for Juno) files is correctly configured, or have you built your app?` - ); - process.exit(1); - } -}; diff --git a/src/services/deploy.services.ts b/src/services/deploy/deploy.assert.services.ts similarity index 51% rename from src/services/deploy.services.ts rename to src/services/deploy/deploy.assert.services.ts index 86cd8e4e..324c5295 100644 --- a/src/services/deploy.services.ts +++ b/src/services/deploy/deploy.assert.services.ts @@ -1,19 +1,11 @@ import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin'; -import type {Asset} from '@junobuild/core'; -import {listAssets as listAssetsLib} from '@junobuild/core'; import {yellow} from 'kleur'; import {compare} from 'semver'; -import {readJunoConfig} from '../configs/juno.config'; -import {DAPP_COLLECTION} from '../constants/constants'; -import { - DEPLOY_LIST_ASSETS_PAGINATION, - MEMORY_HEAP_WARNING, - MEMORY_SIZE_ENDPOINT_VERSION -} from '../constants/deploy.constants'; -import type {SatelliteConfigEnv} from '../types/config'; -import {configEnv} from '../utils/config.utils'; -import {NEW_CMD_LINE, confirmAndExit} from '../utils/prompt.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; +import {readJunoConfig} from '../../configs/juno.config'; +import {MEMORY_HEAP_WARNING, MEMORY_SIZE_ENDPOINT_VERSION} from '../../constants/deploy.constants'; +import {configEnv} from '../../utils/config.utils'; +import {NEW_CMD_LINE, confirmAndExit} from '../../utils/prompt.utils'; +import {satelliteParameters} from '../../utils/satellite.utils'; export const assertSatelliteMemorySize = async (args?: string[]) => { const env = configEnv(args); @@ -64,41 +56,3 @@ export const assertSatelliteMemorySize = async (args?: string[]) => { )} MB. Are you sure you want to proceed with the deployment?` ); }; - -export const listAssets = async ({ - startAfter, - env -}: { - startAfter?: string; - env: SatelliteConfigEnv; -}): Promise => { - const {assets, items_page, matches_pages} = await listAssetsLib({ - collection: DAPP_COLLECTION, - satellite: await satelliteParameters(env), - filter: { - order: { - desc: true, - field: 'keys' - }, - paginate: { - startAfter, - limit: DEPLOY_LIST_ASSETS_PAGINATION - } - } - }); - - const last = (elements: T[]): T | undefined => { - const {length, [length - 1]: last} = elements; - return last; - }; - - if ((items_page ?? 0n) < (matches_pages ?? 0n)) { - const nextAssets = await listAssets({ - startAfter: last(assets)?.fullPath, - env - }); - return [...assets, ...nextAssets]; - } - - return assets; -}; diff --git a/src/services/deploy/deploy.assets.services.ts b/src/services/deploy/deploy.assets.services.ts new file mode 100644 index 00000000..ebdced2e --- /dev/null +++ b/src/services/deploy/deploy.assets.services.ts @@ -0,0 +1,44 @@ +import type {Asset} from '@junobuild/core'; +import {listAssets as listAssetsLib} from '@junobuild/core'; +import {DAPP_COLLECTION} from '../../constants/constants'; +import {DEPLOY_LIST_ASSETS_PAGINATION} from '../../constants/deploy.constants'; +import type {SatelliteConfigEnv} from '../../types/config'; +import {satelliteParameters} from '../../utils/satellite.utils'; + +export const listAssets = async ({ + startAfter, + env +}: { + startAfter?: string; + env: SatelliteConfigEnv; +}): Promise => { + const {assets, items_page, matches_pages} = await listAssetsLib({ + collection: DAPP_COLLECTION, + satellite: await satelliteParameters(env), + filter: { + order: { + desc: true, + field: 'keys' + }, + paginate: { + startAfter, + limit: DEPLOY_LIST_ASSETS_PAGINATION + } + } + }); + + const last = (elements: T[]): T | undefined => { + const {length, [length - 1]: last} = elements; + return last; + }; + + if ((items_page ?? 0n) < (matches_pages ?? 0n)) { + const nextAssets = await listAssets({ + startAfter: last(assets)?.fullPath, + env + }); + return [...assets, ...nextAssets]; + } + + return assets; +}; diff --git a/src/services/deploy/deploy.execute.services.ts b/src/services/deploy/deploy.execute.services.ts new file mode 100644 index 00000000..1e29b67b --- /dev/null +++ b/src/services/deploy/deploy.execute.services.ts @@ -0,0 +1,83 @@ +import type {UploadFileStorage} from '@junobuild/cli-tools'; +import { + deploy as cliDeploy, + postDeploy as cliPostDeploy, + preDeploy as cliPreDeploy +} from '@junobuild/cli-tools'; +import {type Asset, uploadBlob} from '@junobuild/core'; +import {red} from 'kleur'; +import {lstatSync} from 'node:fs'; +import {readJunoConfig} from '../../configs/juno.config'; +import {configEnv} from '../../utils/config.utils'; +import {satelliteParameters} from '../../utils/satellite.utils'; +import {assertSatelliteMemorySize} from './deploy.assert.services'; +import {listAssets} from './deploy.assets.services'; + +export const executeDeploy = async (args?: string[]) => { + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise => + await listAssets({ + startAfter, + env: { + env, + satellite: satelliteConfig + } + }); + + const assertMemory = async () => { + await assertSatelliteMemorySize(args); + }; + + const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + + const uploadFile = async ({ + filename, + fullPath, + data, + collection, + headers, + encoding + }: UploadFileStorage) => { + await uploadBlob({ + satellite, + filename, + fullPath, + // @ts-expect-error type incompatibility NodeJS vs bundle + data, + collection, + headers, + encoding + }); + }; + + await cliPreDeploy({config: satelliteConfig}); + + const {result} = await cliDeploy({ + config: satelliteConfig, + listAssets: listExistingAssets, + assertSourceDirExists, + assertMemory, + uploadFile + }); + + if (result === 'skipped') { + process.exit(0); + } + + await cliPostDeploy({config: satelliteConfig}); +}; + +const assertSourceDirExists = (source: string) => { + try { + lstatSync(source); + } catch (_err: unknown) { + console.log( + `${red( + 'Cannot proceed deployment.' + )}\nAre you sure the folder containing your built app (the "source" tag in the configuration file for Juno) files is correctly configured, or have you built your app?` + ); + process.exit(1); + } +};