Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PromiseSettledResult<void>> = [];
Expand Down
17 changes: 3 additions & 14 deletions src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
18 changes: 6 additions & 12 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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[]) => {
Expand Down Expand Up @@ -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<string | undefined> => {
Expand Down
14 changes: 3 additions & 11 deletions src/services/clear.services.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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();

Expand Down
9 changes: 2 additions & 7 deletions src/services/deploy/deploy.assert.services.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
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;

if (assertions?.heapMemory === false) {
return;
}

const satellite = await satelliteParameters({satellite: satelliteConfig, env});

const currentVersion = await satelliteVersion({
satellite
});
Expand Down
17 changes: 6 additions & 11 deletions src/services/deploy/deploy.execute.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -22,8 +20,11 @@ export const executeDeploy = async ({
args?: string[];
deployFn: (params: DeployFnParams) => Promise<DeployResult>;
}) => {
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<Asset[]> =>
await listAssets({
Expand All @@ -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,
Expand Down
13 changes: 2 additions & 11 deletions src/services/links.services.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions src/services/snapshot/snapshot.satellite.services.ts
Original file line number Diff line number Diff line change
@@ -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[]}) => {
Expand Down Expand Up @@ -48,10 +47,7 @@ const executeSnapshotFn = async ({
};

const loadSatelliteId = async ({args}: {args?: string[]}): Promise<string> => {
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.
Expand Down
20 changes: 3 additions & 17 deletions src/services/start-stop.services.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 2 additions & 13 deletions src/services/upgrade/upgrade.satellite.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
30 changes: 29 additions & 1 deletion src/utils/satellite.utils.ts
Original file line number Diff line number Diff line change
@@ -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},
Expand Down