diff --git a/package-lock.json b/package-lock.json index b0145ba..f7e8185 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "@dfinity/identity": "^0.20.2", "@dfinity/principal": "^0.20.2", "@junobuild/admin": "^0.0.45", - "@junobuild/config": "^0.0.1", + "@junobuild/config": "^0.0.1-next-2024-02-17", "@junobuild/core-peer": "^0.0.10", "@junobuild/utils": "^0.0.18", "conf": "^12.0.0", @@ -1453,9 +1453,9 @@ } }, "node_modules/@junobuild/config": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/@junobuild/config/-/config-0.0.1.tgz", - "integrity": "sha512-UAmUKJVOCAAKvEwHI5PZ/6wi0wo2gRblSnkRW+sghaU/ST2/Vsrstd+tDBAyU12haEHVSCjMWho0fOEJyS5KJg==" + "version": "0.0.1-next-2024-02-17", + "resolved": "https://registry.npmjs.org/@junobuild/config/-/config-0.0.1-next-2024-02-17.tgz", + "integrity": "sha512-L4JMtaM5vSQKxZaLDmgoC9ieFBTRTw/w6X71uyXVC32a7yxN09pnyCQfd29Mg/M7cSirH+7y0pijf7s7I61z/A==" }, "node_modules/@junobuild/core-peer": { "version": "0.0.10", @@ -7564,9 +7564,9 @@ "requires": {} }, "@junobuild/config": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/@junobuild/config/-/config-0.0.1.tgz", - "integrity": "sha512-UAmUKJVOCAAKvEwHI5PZ/6wi0wo2gRblSnkRW+sghaU/ST2/Vsrstd+tDBAyU12haEHVSCjMWho0fOEJyS5KJg==" + "version": "0.0.1-next-2024-02-17", + "resolved": "https://registry.npmjs.org/@junobuild/config/-/config-0.0.1-next-2024-02-17.tgz", + "integrity": "sha512-L4JMtaM5vSQKxZaLDmgoC9ieFBTRTw/w6X71uyXVC32a7yxN09pnyCQfd29Mg/M7cSirH+7y0pijf7s7I61z/A==" }, "@junobuild/core-peer": { "version": "0.0.10", diff --git a/package.json b/package.json index 11d7748..ebab688 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@dfinity/identity": "^0.20.2", "@dfinity/principal": "^0.20.2", "@junobuild/admin": "^0.0.45", - "@junobuild/config": "^0.0.1", + "@junobuild/config": "^0.0.1-next-2024-02-17", "@junobuild/core-peer": "^0.0.10", "@junobuild/utils": "^0.0.18", "conf": "^12.0.0", diff --git a/src/commands/config.ts b/src/commands/config.ts index f090cde..fd174d6 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,6 +1,6 @@ import {setConfig} from '@junobuild/admin'; import ora from 'ora'; -import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config'; +import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import {configEnv} from '../utils/config.utils'; import {satelliteParameters} from '../utils/satellite.utils'; import {init} from './init'; @@ -10,7 +10,9 @@ export const config = async (args?: string[]) => { await init(); } - const {satelliteId, storage} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite} = await readJunoConfig(env); + const {storage} = satellite; const spinner = ora(`Configuring...`).start(); @@ -24,7 +26,7 @@ export const config = async (args?: string[]) => { iframe: storage?.iframe } }, - satellite: satelliteParameters(satelliteId) + satellite: satelliteParameters({satellite, env}) }); } finally { spinner.stop(); diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 90f852b..b5c1570 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -18,7 +18,7 @@ import {minimatch} from 'minimatch'; import {lstatSync} from 'node:fs'; import {readFile} from 'node:fs/promises'; import {basename, extname, join, relative} from 'node:path'; -import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config'; +import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import {COLLECTION_DAPP, DAPP_COLLECTION, UPLOAD_BATCH_SIZE} from '../constants/constants'; import { DEPLOY_DEFAULT_ENCODING, @@ -29,6 +29,7 @@ import { import {clear} from '../services/clear.services'; import {assertSatelliteMemorySize} from '../services/deploy.services'; import {links} from '../services/links.services'; +import type {SatelliteConfigEnv} from '../types/config'; import {hasArgs} from '../utils/args.utils'; import {gzipFiles} from '../utils/compress.utils'; import {configEnv} from '../utils/config.utils'; @@ -59,17 +60,26 @@ export const deploy = async (args?: string[]) => { }; const executeDeploy = async (args?: string[]) => { + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + const { - satelliteId, source = DEPLOY_DEFAULT_SOURCE, ignore = DEPLOY_DEFAULT_IGNORE, encoding = DEPLOY_DEFAULT_ENCODING, gzip = DEPLOY_DEFAULT_GZIP - } = await readSatelliteConfig(configEnv(args)); + } = satelliteConfig; const sourceAbsolutePath = join(process.cwd(), source); - const sourceFiles = await listFiles({sourceAbsolutePath, satelliteId, ignore, encoding, gzip}); + const sourceFiles = await listFiles({ + sourceAbsolutePath, + ignore, + encoding, + gzip, + satellite: satelliteConfig, + env + }); if (sourceFiles.length === 0) { console.log('No new files to upload.'); @@ -78,7 +88,7 @@ const executeDeploy = async (args?: string[]) => { await assertSatelliteMemorySize(args); - const satellite = satelliteParameters(satelliteId); + const satellite = satelliteParameters({satellite: satelliteConfig, env}); const fileDetailsPath = (file: FileDetails): string => file.alternateFile ?? file.file; @@ -145,15 +155,14 @@ const assertSourceDirExists = (source: string) => { const filterFilesToUpload = async ({ files, sourceAbsolutePath, - satelliteId + ...env }: { files: FileDetails[]; sourceAbsolutePath: string; - satelliteId: string; -}): Promise => { +} & SatelliteConfigEnv): Promise => { const existingAssets = await listAssets({ collection: DAPP_COLLECTION, - satellite: satelliteParameters(satelliteId) + satellite: satelliteParameters(env) }); const promises = files.map( @@ -201,15 +210,14 @@ const fileNeedUpload = async ({ const listFiles = async ({ sourceAbsolutePath, - satelliteId, ignore, encoding, - gzip + gzip, + ...env }: { sourceAbsolutePath: string; -} & Required>): Promise< - FileDetails[] -> => { +} & SatelliteConfigEnv & + Required>): Promise => { assertSourceDirExists(sourceAbsolutePath); const sourceFiles = listSourceFiles({sourceAbsolutePath, ignore}); @@ -282,6 +290,6 @@ const listFiles = async ({ return await filterFilesToUpload({ files: encodingFiles, sourceAbsolutePath, - satelliteId + ...env }); }; diff --git a/src/commands/open.ts b/src/commands/open.ts index 87baf69..703307a 100644 --- a/src/commands/open.ts +++ b/src/commands/open.ts @@ -1,6 +1,6 @@ import {listCustomDomains, type CustomDomain} from '@junobuild/admin'; import prompts from 'prompts'; -import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config'; +import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import {hasArgs, nextArg} from '../utils/args.utils'; import {configEnv} from '../utils/config.utils'; import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils'; @@ -17,13 +17,17 @@ export const open = async (args?: string[]) => { const browser = nextArg({args, option: '-b'}) ?? nextArg({args, option: '--browser'}); - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const satellite = satelliteParameters({satellite: satelliteConfig, env}); + const {satelliteId} = satellite; + if (hasArgs({args, options: ['-c', '--console']})) { await openUrl({url: consoleUrl(satelliteId), browser}); return; } - const satellite = satelliteParameters(satelliteId); const domains = await listCustomDomains({satellite}); if (domains.length === 0) { diff --git a/src/commands/version.ts b/src/commands/version.ts index fe6f99e..8d11495 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -8,7 +8,7 @@ import {cyan, green, red, yellow} from 'kleur'; import {clean, compare} from 'semver'; import {version as cliCurrentVersion} from '../../package.json'; import {getCliMissionControl, getCliOrbiters} from '../configs/cli.config'; -import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config'; +import {junoConfigExist, readJunoConfig} from '../configs/juno.config'; import { MISSION_CONTROL_WASM_NAME, ORBITER_WASM_NAME, @@ -90,9 +90,11 @@ const satelliteVersion = async (args?: string[]) => { return; } - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); - const satellite = satelliteParameters(satelliteId); + const satellite = satelliteParameters({satellite: satelliteConfig, env}); + const {satelliteId} = satellite; const currentVersion = await satelliteVersionLib({ satellite diff --git a/src/configs/juno.config.ts b/src/configs/juno.config.ts index 52feada..14f9f75 100644 --- a/src/configs/juno.config.ts +++ b/src/configs/juno.config.ts @@ -1,9 +1,4 @@ -import type { - JunoConfig, - JunoConfigEnv, - JunoConfigFnOrObject, - SatelliteConfig -} from '@junobuild/config'; +import type {JunoConfig, JunoConfigEnv, JunoConfigFnOrObject} from '@junobuild/config'; import {nonNullish} from '@junobuild/utils'; import {existsSync} from 'node:fs'; import {access, readFile, writeFile} from 'node:fs/promises'; @@ -14,7 +9,7 @@ import { } from '../constants/config.constants'; import {JUNO_CONFIG_FILENAME, JUNO_JSON} from '../constants/constants'; import {DEPLOY_DEFAULT_SOURCE} from '../constants/deploy.constants'; -import type {ConfigType} from '../types/config'; +import type {ConfigType, JunoConfigWithSatelliteId} from '../types/config'; import {readTemplateFile} from '../utils/fs.utils'; import {nodeRequire} from '../utils/node.utils'; @@ -22,7 +17,7 @@ export const saveJunoConfig = async ({ config, configType }: { - config: JunoConfig; + config: JunoConfigWithSatelliteId; configType: ConfigType; }): Promise => { await writeJunoConfig({ @@ -31,11 +26,6 @@ export const saveJunoConfig = async ({ }); }; -export const readSatelliteConfig = async (env: JunoConfigEnv): Promise => { - const {satellite} = await readJunoConfig(env); - return satellite; -}; - export const junoConfigExist = async (): Promise => { try { const {configPath} = junoConfigFile(); @@ -107,7 +97,7 @@ const writeJunoConfig = async ({ config, configType }: { - config: JunoConfig; + config: JunoConfigWithSatelliteId; configType: ConfigType; }): Promise => { switch (configType) { @@ -139,7 +129,7 @@ const writeJunoConfig = async ({ } }; -const readJunoConfig = async (env: JunoConfigEnv): Promise => { +export const readJunoConfig = async (env: JunoConfigEnv): Promise => { const {configPath, configType} = junoConfigFile(); const config = (userConfig: JunoConfigFnOrObject): JunoConfig => diff --git a/src/services/clear.services.ts b/src/services/clear.services.ts index d052f92..12cf419 100644 --- a/src/services/clear.services.ts +++ b/src/services/clear.services.ts @@ -1,20 +1,21 @@ import {deleteAssets} from '@junobuild/admin'; import {deleteAsset} from '@junobuild/core-peer'; import ora from 'ora'; -import {readSatelliteConfig} from '../configs/juno.config'; +import {readJunoConfig} from '../configs/juno.config'; import {DAPP_COLLECTION} from '../constants/constants'; import {configEnv} from '../utils/config.utils'; import {satelliteParameters} from '../utils/satellite.utils'; export const clear = async (args?: string[]) => { - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); const spinner = ora('Clearing dapp assets...').start(); try { await deleteAssets({ collection: DAPP_COLLECTION, - satellite: satelliteParameters(satelliteId) + satellite: satelliteParameters({satellite: satelliteConfig, env}) }); } finally { spinner.stop(); @@ -27,14 +28,15 @@ const cleanFullPath = (fullPath: string): string => { }; export const clearAsset = async ({fullPath, args}: {fullPath: string; args?: string[]}) => { - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); const spinner = ora(`Clearing ${fullPath}...`).start(); try { await deleteAsset({ collection: DAPP_COLLECTION, - satellite: satelliteParameters(satelliteId), + satellite: satelliteParameters({satellite: satelliteConfig, env}), fullPath: cleanFullPath(fullPath) }); } finally { diff --git a/src/services/deploy.services.ts b/src/services/deploy.services.ts index f8108a4..be6c879 100644 --- a/src/services/deploy.services.ts +++ b/src/services/deploy.services.ts @@ -1,20 +1,23 @@ import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin'; import {yellow} from 'kleur'; import {compare} from 'semver'; -import {readSatelliteConfig} from '../configs/juno.config'; +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 {satelliteId, assertions} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const {assertions} = satelliteConfig; if (assertions?.heapMemory === false) { return; } - const satellite = satelliteParameters(satelliteId); + const satellite = satelliteParameters({satellite: satelliteConfig, env}); const currentVersion = await satelliteVersion({ satellite diff --git a/src/services/links.services.ts b/src/services/links.services.ts index d3efb01..3ef5cd0 100644 --- a/src/services/links.services.ts +++ b/src/services/links.services.ts @@ -1,6 +1,6 @@ import {listCustomDomains} from '@junobuild/admin'; import {red} from 'kleur'; -import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config'; +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'; @@ -18,13 +18,16 @@ export const links = async (args?: string[]) => { return; } - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const satellite = satelliteParameters({satellite: satelliteConfig, env}); + const {satelliteId} = satellite; const defaultUrl = defaultSatelliteDomain(satelliteId); const adminUrl = consoleUrl(satelliteId); try { - const satellite = satelliteParameters(satelliteId); const domains = await listCustomDomains({satellite}); console.log(`\n🛠️ ${terminalLink(adminUrl)}`); diff --git a/src/services/upgrade/upgrade.satellite.services.ts b/src/services/upgrade/upgrade.satellite.services.ts index 48c0fcc..25bab81 100644 --- a/src/services/upgrade/upgrade.satellite.services.ts +++ b/src/services/upgrade/upgrade.satellite.services.ts @@ -7,7 +7,7 @@ import { import {isNullish} from '@junobuild/utils'; import {cyan, red} from 'kleur'; import {compare} from 'semver'; -import {junoConfigExist, readSatelliteConfig} from '../../configs/juno.config'; +import {junoConfigExist, readJunoConfig} from '../../configs/juno.config'; import {SATELLITE_WASM_NAME} from '../../constants/constants'; import type {UpgradeWasm, UpgradeWasmModule} from '../../types/upgrade'; import {hasArgs, nextArg} from '../../utils/args.utils'; @@ -30,14 +30,16 @@ export const upgradeSatellite = async (args?: string[]) => { return; } - const {satelliteId} = await readSatelliteConfig(configEnv(args)); + const env = configEnv(args); + const {satellite: satelliteConfig} = await readJunoConfig(env); + + const satellite = satelliteParameters({satellite: satelliteConfig, env}); + const {satelliteId} = satellite; console.log( `${NEW_CMD_LINE}Initiating upgrade for satellite ${cyan(satelliteId)}.${NEW_CMD_LINE}` ); - const satellite = satelliteParameters(satelliteId); - const consoleSuccess = () => { console.log(`✅ Satellite successfully upgraded.`); }; diff --git a/src/types/config.ts b/src/types/config.ts index 9732cd6..be63a6a 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -1 +1,13 @@ +import type {JunoConfig, JunoConfigEnv, SatelliteConfig} from '@junobuild/config'; + export type ConfigType = 'ts' | 'js' | 'json'; + +export interface SatelliteConfigEnv { + satellite: SatelliteConfig; + env: JunoConfigEnv; +} + +export type JunoConfigWithSatelliteId = Omit & { + satellite: Omit & + Required>; +}; diff --git a/src/utils/satellite.utils.ts b/src/utils/satellite.utils.ts index ec4e674..a7ec87e 100644 --- a/src/utils/satellite.utils.ts +++ b/src/utils/satellite.utils.ts @@ -1,11 +1,27 @@ import {type SatelliteParameters} from '@junobuild/admin'; +import {isNullish} from '@junobuild/utils'; +import {red} from 'kleur'; import {getCliOrbiters, getCliSatellites} from '../configs/cli.config'; +import type {SatelliteConfigEnv} from '../types/config'; import {actorParameters} from './actor.utils'; -export const satelliteParameters = (satelliteId: string): SatelliteParameters => ({ - satelliteId, - ...actorParameters() -}); +export const satelliteParameters = ({ + satellite: {satelliteId: satelliteIdUser, satellitesIds}, + env: {mode} +}: SatelliteConfigEnv): Omit & + Required> => { + const satelliteId = satellitesIds?.[mode] ?? satelliteIdUser; + + if (isNullish(satelliteId)) { + console.log(`${red(`A satellite ID for ${mode} must be set in your configuration.`)}`); + process.exit(1); + } + + return { + satelliteId, + ...actorParameters() + }; +}; /** * For display purpose, use either the name or id. Most probably we should find a name but for simplicity reason we fallback to Id.