From c43bab054e3e456f2e9c6092c816346db18550f7 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 30 May 2025 20:43:28 +0200 Subject: [PATCH] feat: load satellite context - remove duplicated parameters code --- src/commands/config.ts | 14 ++------- src/commands/open.ts | 17 ++--------- src/commands/version.ts | 18 ++++------- src/services/clear.services.ts | 14 ++------- src/services/deploy/deploy.assert.services.ts | 9 ++---- .../deploy/deploy.execute.services.ts | 17 ++++------- src/services/links.services.ts | 13 ++------ .../snapshot/snapshot.satellite.services.ts | 10 ++----- src/services/start-stop.services.ts | 20 ++----------- .../upgrade/upgrade.satellite.services.ts | 15 ++-------- src/utils/satellite.utils.ts | 30 ++++++++++++++++++- 11 files changed, 61 insertions(+), 116 deletions(-) diff --git a/src/commands/config.ts b/src/commands/config.ts index c5152574..08dbba1e 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -11,22 +11,12 @@ import type {ModuleSettings} from '@junobuild/config'; import {red} from 'kleur'; import ora from 'ora'; import {initAgent} from '../api/agent.api'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; -import {configEnv} from '../utils/config.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; -import {init} from './init'; +import {assertConfigAndLoadSatelliteContext} from '../utils/satellite.utils'; export const config = async (args?: string[]) => { - if (!(await junoConfigExist())) { - await init(); - } - - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); + const {satellite, satelliteConfig} = await assertConfigAndLoadSatelliteContext(args); const {storage, authentication, datastore, settings} = satelliteConfig; - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); - const spinner = ora(`Configuring...`).start(); let results: Array> = []; diff --git a/src/commands/open.ts b/src/commands/open.ts index 252862c9..28270848 100644 --- a/src/commands/open.ts +++ b/src/commands/open.ts @@ -1,27 +1,16 @@ import {listCustomDomains, type CustomDomain} from '@junobuild/admin'; import {assertAnswerCtrlC, hasArgs, nextArg} from '@junobuild/cli-tools'; import prompts from 'prompts'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; -import {configEnv} from '../utils/config.utils'; import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils'; -import {consoleNoConfigFound} from '../utils/msg.utils'; import {openUrl} from '../utils/open.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../utils/satellite.utils'; export const open = async (args?: string[]) => { - if (!(await junoConfigExist())) { - consoleNoConfigFound(); - return; - } + const {satellite} = await assertConfigAndLoadSatelliteContext(args); + const {satelliteId} = satellite; const browser = nextArg({args, option: '-b'}) ?? nextArg({args, option: '--browser'}); - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); - const {satelliteId} = satellite; - if (hasArgs({args, options: ['-c', '--console']})) { await openUrl({url: consoleUrl(satelliteId), browser}); return; diff --git a/src/commands/version.ts b/src/commands/version.ts index 753b9048..1703770d 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -14,7 +14,6 @@ import {clean, compare} from 'semver'; import {version as cliCurrentVersion} from '../../package.json'; import {actorParameters} from '../api/actor.api'; import {getCliMissionControl, getCliOrbiters} from '../configs/cli.config'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import { MISSION_CONTROL_WASM_NAME, ORBITER_WASM_NAME, @@ -23,8 +22,11 @@ import { import {githubCliLastRelease} from '../rest/github.rest'; import type {AssetKey} from '../types/asset-key'; import {toAssetKeys} from '../utils/asset-key.utils'; -import {configEnv} from '../utils/config.utils'; -import {orbiterKey, satelliteKey, satelliteParameters} from '../utils/satellite.utils'; +import { + assertConfigAndLoadSatelliteContext, + orbiterKey, + satelliteKey +} from '../utils/satellite.utils'; import {lastRelease} from '../utils/upgrade.utils'; export const version = async (args?: string[]) => { @@ -111,15 +113,7 @@ const missionControlVersion = async () => { }; const satelliteVersion = async (args?: string[]) => { - if (!(await junoConfigExist())) { - console.log(`No ${yellow('config')} file found.`); - return; - } - - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const {satelliteId, ...actorParams} = satellite; const getVersion = async (): Promise => { diff --git a/src/services/clear.services.ts b/src/services/clear.services.ts index 53da0e88..a629f894 100644 --- a/src/services/clear.services.ts +++ b/src/services/clear.services.ts @@ -1,16 +1,11 @@ import {deleteAssets, listCustomDomains, setCustomDomains} from '@junobuild/admin'; import {deleteAsset} from '@junobuild/core'; import ora from 'ora'; -import {readJunoConfig} from '../configs/juno.config'; import {DAPP_COLLECTION} from '../constants/constants'; -import {configEnv} from '../utils/config.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../utils/satellite.utils'; export const clear = async (args?: string[]) => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const spinner = ora('Clearing app assets...').start(); @@ -38,10 +33,7 @@ const cleanFullPath = (fullPath: string): string => { }; export const clearAsset = async ({fullPath, args}: {fullPath: string; args?: string[]}) => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const spinner = ora(`Clearing ${fullPath}...`).start(); diff --git a/src/services/deploy/deploy.assert.services.ts b/src/services/deploy/deploy.assert.services.ts index 324c5295..70c09e05 100644 --- a/src/services/deploy/deploy.assert.services.ts +++ b/src/services/deploy/deploy.assert.services.ts @@ -1,15 +1,12 @@ import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin'; import {yellow} from 'kleur'; import {compare} from 'semver'; -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'; +import {assertConfigAndLoadSatelliteContext} from '../../utils/satellite.utils'; export const assertSatelliteMemorySize = async (args?: string[]) => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); + const {satellite, satelliteConfig} = await assertConfigAndLoadSatelliteContext(args); const {assertions} = satelliteConfig; @@ -17,8 +14,6 @@ export const assertSatelliteMemorySize = async (args?: string[]) => { return; } - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); - const currentVersion = await satelliteVersion({ satellite }); diff --git a/src/services/deploy/deploy.execute.services.ts b/src/services/deploy/deploy.execute.services.ts index dc1a094c..6b36de40 100644 --- a/src/services/deploy/deploy.execute.services.ts +++ b/src/services/deploy/deploy.execute.services.ts @@ -3,10 +3,8 @@ import {postDeploy as cliPostDeploy, preDeploy as cliPreDeploy} from '@junobuild import {type Asset, uploadBlob} from '@junobuild/core'; import {red} from 'kleur'; import {lstatSync} from 'node:fs'; -import {readJunoConfig} from '../../configs/juno.config'; import {type SatelliteParametersWithId} from '../../types/satellite'; -import {configEnv} from '../../utils/config.utils'; -import {satelliteParameters} from '../../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../../utils/satellite.utils'; import {assertSatelliteMemorySize} from './deploy.assert.services'; import {listAssets} from './deploy.list.services'; @@ -22,8 +20,11 @@ export const executeDeploy = async ({ args?: string[]; deployFn: (params: DeployFnParams) => Promise; }) => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); + const assertMemory = async () => { + await assertSatelliteMemorySize(args); + }; + + const {satellite, env, satelliteConfig} = await assertConfigAndLoadSatelliteContext(args); const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise => await listAssets({ @@ -34,12 +35,6 @@ export const executeDeploy = async ({ } }); - const assertMemory = async () => { - await assertSatelliteMemorySize(args); - }; - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); - const uploadFile = async ({ filename, fullPath, diff --git a/src/services/links.services.ts b/src/services/links.services.ts index c23bce63..62dbfcc8 100644 --- a/src/services/links.services.ts +++ b/src/services/links.services.ts @@ -1,11 +1,9 @@ import {listCustomDomains} from '@junobuild/admin'; import {red} from 'kleur'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; -import {configEnv} from '../utils/config.utils'; import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils'; import {terminalLink} from '../utils/links.utils'; import {isHeadless} from '../utils/process.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../utils/satellite.utils'; export const links = async (args?: string[]) => { // If a developer is using a JUNO_TOKEN to execute command(s), the links will not be printed. @@ -14,14 +12,7 @@ export const links = async (args?: string[]) => { return; } - if (!(await junoConfigExist())) { - return; - } - - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const {satelliteId} = satellite; const defaultUrl = defaultSatelliteDomain(satelliteId); diff --git a/src/services/snapshot/snapshot.satellite.services.ts b/src/services/snapshot/snapshot.satellite.services.ts index eae0888b..75d30172 100644 --- a/src/services/snapshot/snapshot.satellite.services.ts +++ b/src/services/snapshot/snapshot.satellite.services.ts @@ -1,9 +1,8 @@ import {assertNonNullish} from '@dfinity/utils'; -import {junoConfigExist, readJunoConfig} from '../../configs/juno.config'; +import {junoConfigExist} from '../../configs/juno.config'; import type {AssetKey} from '../../types/asset-key'; -import {configEnv} from '../../utils/config.utils'; import {consoleNoConfigFound} from '../../utils/msg.utils'; -import {satelliteParameters} from '../../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../../utils/satellite.utils'; import {createSnapshot, deleteSnapshot, restoreSnapshot} from './snapshot.services'; export const createSnapshotSatellite = async (params: {args?: string[]}) => { @@ -48,10 +47,7 @@ const executeSnapshotFn = async ({ }; const loadSatelliteId = async ({args}: {args?: string[]}): Promise => { - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const {satelliteId} = satellite; // TS guard. satelliteParameters exit if satelliteId is undefined. diff --git a/src/services/start-stop.services.ts b/src/services/start-stop.services.ts index fd0e7be0..2886ab35 100644 --- a/src/services/start-stop.services.ts +++ b/src/services/start-stop.services.ts @@ -1,15 +1,12 @@ import {Principal} from '@dfinity/principal'; -import {assertNonNullish, isNullish} from '@dfinity/utils'; +import {isNullish} from '@dfinity/utils'; import {cyan, red} from 'kleur'; import ora from 'ora'; import {canisterStart, canisterStop} from '../api/ic.api'; import {getCliMissionControl, getCliOrbiters} from '../configs/cli.config'; -import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import type {AssetKey} from '../types/asset-key'; import type {StartStopAction} from '../types/start-stop'; -import {configEnv} from '../utils/config.utils'; -import {consoleNoConfigFound} from '../utils/msg.utils'; -import {satelliteParameters} from '../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext} from '../utils/satellite.utils'; export const startStopMissionControl = async ({ action @@ -59,20 +56,9 @@ export const startStopSatellite = async ({ args?: string[]; action: StartStopAction; }) => { - if (!(await junoConfigExist())) { - consoleNoConfigFound(); - return; - } - - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const {satelliteId} = satellite; - // TS guard. satelliteParameters exit if satelliteId is undefined. - assertNonNullish(satelliteId); - await startStop({ action, segment: 'satellite', diff --git a/src/services/upgrade/upgrade.satellite.services.ts b/src/services/upgrade/upgrade.satellite.services.ts index afa283c6..0e330e5d 100644 --- a/src/services/upgrade/upgrade.satellite.services.ts +++ b/src/services/upgrade/upgrade.satellite.services.ts @@ -8,13 +8,10 @@ import { import {hasArgs, nextArg} from '@junobuild/cli-tools'; import {cyan, red} from 'kleur'; import {compare} from 'semver'; -import {junoConfigExist, readJunoConfig} from '../../configs/juno.config'; import {SATELLITE_WASM_NAME} from '../../constants/constants'; import type {AssertWasmModule, UpgradeWasm, UpgradeWasmModule} from '../../types/upgrade'; -import {configEnv} from '../../utils/config.utils'; -import {consoleNoConfigFound} from '../../utils/msg.utils'; import {NEW_CMD_LINE} from '../../utils/prompt.utils'; -import {satelliteKey, satelliteParameters} from '../../utils/satellite.utils'; +import {assertConfigAndLoadSatelliteContext, satelliteKey} from '../../utils/satellite.utils'; import {assertSatelliteBuildType} from './upgrade-assert.services'; import { confirmReset, @@ -26,15 +23,7 @@ import { } from './upgrade.services'; export const upgradeSatellite = async (args?: string[]) => { - if (!(await junoConfigExist())) { - consoleNoConfigFound(); - return; - } - - const env = configEnv(args); - const {satellite: satelliteConfig} = await readJunoConfig(env); - - const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + const {satellite} = await assertConfigAndLoadSatelliteContext(args); const {satelliteId} = satellite; console.log( diff --git a/src/utils/satellite.utils.ts b/src/utils/satellite.utils.ts index 449ba019..aef73adc 100644 --- a/src/utils/satellite.utils.ts +++ b/src/utils/satellite.utils.ts @@ -1,9 +1,37 @@ -import {isNullish} from '@dfinity/utils'; +import {assertNonNullish, isNullish} from '@dfinity/utils'; +import {type JunoConfigEnv, type SatelliteConfig} from '@junobuild/config'; import {red} from 'kleur'; import {actorParameters} from '../api/actor.api'; import {getCliOrbiters, getCliSatellites} from '../configs/cli.config'; +import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import type {SatelliteConfigEnv} from '../types/config'; import type {SatelliteParametersWithId} from '../types/satellite'; +import {configEnv} from './config.utils'; +import {consoleNoConfigFound} from './msg.utils'; + +export const assertConfigAndLoadSatelliteContext = async ( + args?: string[] +): Promise<{ + satellite: SatelliteParametersWithId; + satelliteConfig: SatelliteConfig; + env: JunoConfigEnv; +}> => { + if (!(await junoConfigExist())) { + consoleNoConfigFound(); + process.exit(1); + } + + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const satellite = await satelliteParameters({satellite: satelliteConfig, env}); + + // TS guard. satelliteParameters exit if satelliteId is undefined. + // Should not happen. + assertNonNullish(satellite.satelliteId); + + return {satellite, satelliteConfig, env}; +}; export const satelliteParameters = async ({ satellite: {satelliteId: deprecatedSatelliteId, id, ids},