Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple environments with --mode #91

Merged
merged 6 commits into from
Feb 16, 2024
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
4 changes: 2 additions & 2 deletions src/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const clear = async (args?: string[]) => {
return;
}

await clearAsset(file);
await clearAsset({fullPath: file, args});
return;
}

await clearServices();
await clearServices(args);
};
5 changes: 3 additions & 2 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {setConfig} from '@junobuild/admin';
import ora from 'ora';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {configEnv} from '../utils/config.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {init} from './init';

export const config = async () => {
export const config = async (args?: string[]) => {
if (!(await junoConfigExist())) {
await init();
}

const {satelliteId, storage} = await readSatelliteConfig();
const {satelliteId, storage} = await readSatelliteConfig(configEnv(args));

const spinner = ora(`Configuring...`).start();

Expand Down
13 changes: 7 additions & 6 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {assertSatelliteMemorySize} from '../services/deploy.services';
import {links} from '../services/links.services';
import {hasArgs} from '../utils/args.utils';
import {gzipFiles} from '../utils/compress.utils';
import {configEnv} from '../utils/config.utils';
import {listSourceFiles} from '../utils/deploy.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {init} from './init';
Expand All @@ -49,22 +50,22 @@ export const deploy = async (args?: string[]) => {
}

if (hasArgs({args, options: ['-c', '--clear']})) {
await clear();
await clear(args);
}

await executeDeploy();
await executeDeploy(args);

await links();
await links(args);
};

const executeDeploy = async () => {
const executeDeploy = async (args?: string[]) => {
const {
satelliteId,
source = DEPLOY_DEFAULT_SOURCE,
ignore = DEPLOY_DEFAULT_IGNORE,
encoding = DEPLOY_DEFAULT_ENCODING,
gzip = DEPLOY_DEFAULT_GZIP
} = await readSatelliteConfig();
} = await readSatelliteConfig(configEnv(args));

const sourceAbsolutePath = join(process.cwd(), source);

Expand All @@ -75,7 +76,7 @@ const executeDeploy = async () => {
return;
}

await assertSatelliteMemorySize();
await assertSatelliteMemorySize(args);

const satellite = satelliteParameters(satelliteId);

Expand Down
16 changes: 16 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Commands:
${cyan('whoami')} Display the current controller.
`;

export const helpMode = `${yellow('-m, --mode')} Set env mode. For example production, development or a custom string. Default is production.`;

export const helpUpgrade = `
${TITLE}

Expand All @@ -47,6 +49,7 @@ Options:
${yellow('-s, --src')} A local gzipped wasm file for the upgrade.
${yellow('-r, --reset')} Reset to the initial state.
${yellow('-n, --nocheck')} Skip assertions and execute upgrade without prompts.
${helpMode}
${yellow('-h, --help')} Output usage information.

Notes:
Expand Down Expand Up @@ -93,6 +96,7 @@ Usage: ${green('juno')} ${cyan('open')} ${yellow('[options]')}
Options:
${yellow('-b, --browser')} A particular browser to open. supported: chrome|firefox|edge.
${yellow('-c, --console')} Open satellite in the console.
${helpMode}
${yellow('-h, --help')} Output usage information.
`;

Expand All @@ -103,6 +107,7 @@ Usage: ${green('juno')} ${cyan('deploy')} ${yellow('[options]')}

Options:
${yellow('-c, --clear')} Clear existing dapp files before proceeding with deployment.
${helpMode}
${yellow('-h, --help')} Output usage information.
`;

Expand All @@ -113,6 +118,17 @@ Usage: ${green('juno')} ${cyan('clear')} ${yellow('[options]')}

Options:
${yellow('-f, --fullPath')} Clear a particular file of your dapp.
${helpMode}
${yellow('-h, --help')} Output usage information.
`;

export const helpConfig = `
${TITLE}

Usage: ${green('juno')} ${cyan('config')} ${yellow('[options]')}

Options:
${helpMode}
${yellow('-h, --help')} Output usage information.
`;

Expand Down
3 changes: 2 additions & 1 deletion src/commands/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {listCustomDomains, type CustomDomain} from '@junobuild/admin';
import prompts from 'prompts';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {hasArgs, nextArg} from '../utils/args.utils';
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';
Expand All @@ -16,7 +17,7 @@ export const open = async (args?: string[]) => {

const browser = nextArg({args, option: '-b'}) ?? nextArg({args, option: '--browser'});

const {satelliteId} = await readSatelliteConfig();
const {satelliteId} = await readSatelliteConfig(configEnv(args));
if (hasArgs({args, options: ['-c', '--console']})) {
await openUrl({url: consoleUrl(satelliteId), browser});
return;
Expand Down
9 changes: 5 additions & 4 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import {githubCliLastRelease} from '../rest/github.rest';
import type {AssetKey} from '../types/asset-key';
import {actorParameters} from '../utils/actor.utils';
import {toAssetKeys} from '../utils/asset-key.utils';
import {configEnv} from '../utils/config.utils';
import {orbiterKey, satelliteKey, satelliteParameters} from '../utils/satellite.utils';
import {lastRelease} from '../utils/upgrade.utils';

export const version = async () => {
export const version = async (args?: string[]) => {
await cliVersion();
await missionControlVersion();
await satelliteVersion();
await satelliteVersion(args);
await orbitersVersion();
};

Expand Down Expand Up @@ -83,13 +84,13 @@ const missionControlVersion = async () => {
});
};

const satelliteVersion = async () => {
const satelliteVersion = async (args?: string[]) => {
if (!(await junoConfigExist())) {
console.log(`No ${yellow('config')} file found.`);
return;
}

const {satelliteId} = await readSatelliteConfig();
const {satelliteId} = await readSatelliteConfig(configEnv(args));

const satellite = satelliteParameters(satelliteId);

Expand Down
4 changes: 2 additions & 2 deletions src/commands/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {green} from 'kleur';
import {getToken, getUse, isDefaultProfile} from '../configs/cli.config';
import {links} from '../services/links.services';

export const whoami = async () => {
export const whoami = async (args?: string[]) => {
const {success} = info();

if (!success) {
return;
}

await links();
await links(args);
};

const info = (): {success: boolean} => {
Expand Down
15 changes: 10 additions & 5 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type {JunoConfig, JunoConfigFnOrObject, SatelliteConfig} from '@junobuild/config';
import type {
JunoConfig,
JunoConfigEnv,
JunoConfigFnOrObject,
SatelliteConfig
} from '@junobuild/config';
import {nonNullish} from '@junobuild/utils';
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
Expand Down Expand Up @@ -26,8 +31,8 @@ export const saveConfig = async ({
});
};

export const readSatelliteConfig = async (): Promise<SatelliteConfig> => {
const {satellite} = await readJunoConfig();
export const readSatelliteConfig = async (env: JunoConfigEnv): Promise<SatelliteConfig> => {
const {satellite} = await readJunoConfig(env);
return satellite;
};

Expand Down Expand Up @@ -134,11 +139,11 @@ const writeJunoConfig = async ({
}
};

const readJunoConfig = async (): Promise<JunoConfig> => {
const readJunoConfig = async (env: JunoConfigEnv): Promise<JunoConfig> => {
const {configPath, configType} = junoConfigFile();

const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
typeof userConfig === 'function' ? userConfig({mode: 'production'}) : userConfig;
typeof userConfig === 'function' ? userConfig(env) : userConfig;

switch (configType) {
case 'ts': {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
help,
helpClear,
helpCommand,
helpConfig,
helpDeploy,
helpDev,
helpLogin,
Expand Down Expand Up @@ -61,6 +62,9 @@ export const run = async () => {
case 'clear':
console.log(helpClear);
break;
case 'config':
console.log(helpConfig);
break;
case 'deploy':
console.log(helpDeploy);
break;
Expand Down Expand Up @@ -93,7 +97,7 @@ export const run = async () => {
await clear(args);
break;
case 'version':
await versionCommand();
await versionCommand(args);
break;
case 'open':
await open(args);
Expand All @@ -102,7 +106,7 @@ export const run = async () => {
await upgrade(args);
break;
case 'whoami':
await whoami();
await whoami(args);
break;
case 'use':
use(args);
Expand Down
9 changes: 5 additions & 4 deletions src/services/clear.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {deleteAsset} from '@junobuild/core-peer';
import ora from 'ora';
import {readSatelliteConfig} 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 () => {
const {satelliteId} = await readSatelliteConfig();
export const clear = async (args?: string[]) => {
const {satelliteId} = await readSatelliteConfig(configEnv(args));

const spinner = ora('Clearing dapp assets...').start();

Expand All @@ -25,8 +26,8 @@ const cleanFullPath = (fullPath: string): string => {
return `${path.startsWith('/') ? '' : '/'}${path}`;
};

export const clearAsset = async (fullPath: string) => {
const {satelliteId} = await readSatelliteConfig();
export const clearAsset = async ({fullPath, args}: {fullPath: string; args?: string[]}) => {
const {satelliteId} = await readSatelliteConfig(configEnv(args));

const spinner = ora(`Clearing ${fullPath}...`).start();

Expand Down
5 changes: 3 additions & 2 deletions src/services/deploy.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {yellow} from 'kleur';
import {compare} from 'semver';
import {readSatelliteConfig} 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 () => {
const {satelliteId, assertions} = await readSatelliteConfig();
export const assertSatelliteMemorySize = async (args?: string[]) => {
const {satelliteId, assertions} = await readSatelliteConfig(configEnv(args));

if (assertions?.heapMemory === false) {
return;
Expand Down
5 changes: 3 additions & 2 deletions src/services/links.services.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {listCustomDomains} from '@junobuild/admin';
import {red} from 'kleur';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {configEnv} from '../utils/config.utils';
import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils';
import {terminalLink} from '../utils/links.utils';
import {isProcessToken} from '../utils/process.utils';
import {satelliteParameters} from '../utils/satellite.utils';

export const links = async () => {
export const links = async (args?: string[]) => {
// If a developer is using a JUNO_TOKEN to execute command(s), the links will not be printed.
// This is particularly useful for CI environment where such output is not needed and also because only ADMIN controllers can list custom domains.
if (isProcessToken()) {
Expand All @@ -17,7 +18,7 @@ export const links = async () => {
return;
}

const {satelliteId} = await readSatelliteConfig();
const {satelliteId} = await readSatelliteConfig(configEnv(args));

const defaultUrl = defaultSatelliteDomain(satelliteId);
const adminUrl = consoleUrl(satelliteId);
Expand Down
3 changes: 2 additions & 1 deletion src/services/upgrade/upgrade.satellite.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {junoConfigExist, readSatelliteConfig} 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';
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';
Expand All @@ -29,7 +30,7 @@ export const upgradeSatellite = async (args?: string[]) => {
return;
}

const {satelliteId} = await readSatelliteConfig();
const {satelliteId} = await readSatelliteConfig(configEnv(args));

console.log(
`${NEW_CMD_LINE}Initiating upgrade for satellite ${cyan(satelliteId)}.${NEW_CMD_LINE}`
Expand Down
9 changes: 9 additions & 0 deletions src/utils/config.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {JunoConfigEnv} from '@junobuild/config';
import {nextArg} from './args.utils';

export const configEnv = (args?: string[]): JunoConfigEnv => {
const mode = nextArg({args, option: '-m'}) ?? nextArg({args, option: '--mode'});
return {
mode: mode ?? 'production'
};
};
Loading