Skip to content

Commit caaf571

Browse files
feat: help messages and upgrade options (#4)
* feat: print help * feat: upgrade with local file
1 parent 6bdf095 commit caaf571

File tree

6 files changed

+127
-19
lines changed

6 files changed

+127
-19
lines changed

src/commands/help.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const JUNO_LOGO = ` __ __ __ __ _ ____
55
__) || | || \\| |/ \\
66
\\___/ \\___/ |_|\\__|\\____/`;
77

8+
const title = `${JUNO_LOGO} CLI ${grey(`v${version}`)}`;
9+
810
export const help = `
9-
${JUNO_LOGO} CLI ${grey(`v${version}`)}
11+
${title}
1012
1113
Usage: ${cyan('juno [command]')}
1214
@@ -21,3 +23,22 @@ Commands:
2123
${cyan('upgrade')} upgrade your satellite to a specific version code
2224
${cyan('version')} check the version of a satellite
2325
`;
26+
27+
export const helpUpgrade = `
28+
${title}
29+
30+
Usage: ${cyan('juno upgrade [options]')}
31+
32+
Options:
33+
${cyan('-s, --src')} a local wasm file to upgrade your satellite
34+
${cyan('-h, --help')} output usage information
35+
`;
36+
37+
export const helpCommand = (command: string) => `
38+
${title}
39+
40+
Usage: ${cyan(`juno ${command}`)}
41+
42+
Options:
43+
${cyan('-h, --help')} output usage information
44+
`;

src/commands/upgrade.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import {satelliteVersion, upgradeSatellite} from '@junobuild/admin';
1+
import {SatelliteParameters, satelliteVersion, upgradeSatellite} from '@junobuild/admin';
22
import {red} from 'kleur';
33
import prompts from 'prompts';
44
import {compare} from 'semver';
55
import {SATELLITE_WASM_NAME} from '../constants/constants';
6+
import {hasArgs, nextArg} from '../utils/args.utils';
67
import {GitHubAsset, GitHubRelease, githubReleases} from '../utils/github.utils';
78
import {junoConfigExist, readSatelliteConfig} from '../utils/satellite.config.utils';
89
import {satelliteParameters} from '../utils/satellite.utils';
9-
import {upgradeWasm} from '../utils/wasm.utils';
10+
import {upgradeWasmGitHub, upgradeWasmLocal} from '../utils/wasm.utils';
1011

1112
const promptReleases = async (
1213
githubReleases: GitHubRelease[]
@@ -27,7 +28,7 @@ const promptReleases = async (
2728
return assets?.find(({name}) => name === SATELLITE_WASM_NAME);
2829
};
2930

30-
export const upgrade = async () => {
31+
export const upgrade = async (args?: string[]) => {
3132
if (!(await junoConfigExist())) {
3233
console.log(`${red('No configuration found.')}`);
3334
return;
@@ -37,6 +38,15 @@ export const upgrade = async () => {
3738

3839
const satellite = satelliteParameters(satelliteId);
3940

41+
if (hasArgs({args, options: ['-s', '--src']})) {
42+
await upgradeCustom({satellite, args});
43+
return;
44+
}
45+
46+
await upgradeRelease(satellite);
47+
};
48+
49+
const upgradeRelease = async (satellite: SatelliteParameters) => {
4050
const currentVersion = await satelliteVersion({
4151
satellite
4252
});
@@ -79,5 +89,28 @@ export const upgrade = async () => {
7989
wasm_module
8090
});
8191

82-
await upgradeWasm({asset, upgrade: upgradeSatelliteWasm});
92+
await upgradeWasmGitHub({asset, upgrade: upgradeSatelliteWasm});
93+
};
94+
95+
const upgradeCustom = async ({
96+
satellite,
97+
args
98+
}: {
99+
satellite: SatelliteParameters;
100+
args?: string[];
101+
}) => {
102+
const src = nextArg({args, option: '-s'}) ?? nextArg({args, option: '--src'});
103+
104+
if (src === undefined) {
105+
console.log(`${red('No source file provided.')}`);
106+
return;
107+
}
108+
109+
const upgradeSatelliteWasm = async ({wasm_module}: {wasm_module: Array<number>}) =>
110+
upgradeSatellite({
111+
satellite,
112+
wasm_module
113+
});
114+
115+
await upgradeWasmLocal({src, upgrade: upgradeSatelliteWasm});
83116
};

src/commands/version.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {GitHubAsset, githubLastRelease} from '../utils/github.utils';
1313
import {confirm, NEW_CMD_LINE} from '../utils/prompt.utils';
1414
import {junoConfigExist, readSatelliteConfig} from '../utils/satellite.config.utils';
1515
import {satelliteKey, satelliteParameters} from '../utils/satellite.utils';
16-
import {upgradeWasm} from '../utils/wasm.utils';
16+
import {upgradeWasmGitHub} from '../utils/wasm.utils';
1717

1818
export const version = async () => {
1919
await missionControlVersion();
@@ -59,7 +59,7 @@ const missionControlVersion = async () => {
5959
wasm_module
6060
});
6161

62-
await upgradeWasm({asset, upgrade: upgradeMissionControlWasm});
62+
await upgradeWasmGitHub({asset, upgrade: upgradeMissionControlWasm});
6363
};
6464

6565
const satelliteVersion = async () => {
@@ -98,7 +98,7 @@ const satelliteVersion = async () => {
9898
wasm_module
9999
});
100100

101-
await upgradeWasm({asset, upgrade: upgradeSatelliteWasm});
101+
await upgradeWasmGitHub({asset, upgrade: upgradeSatelliteWasm});
102102
};
103103

104104
const shouldUpgradeVersion = async ({

src/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ import {login, logout} from './commands/auth';
33
import {clear} from './commands/clear';
44
import {config} from './commands/config';
55
import {deploy} from './commands/deploy';
6-
import {help} from './commands/help';
6+
import {help, helpCommand, helpUpgrade} from './commands/help';
77
import {init} from './commands/init';
88
import {upgrade} from './commands/upgrade';
99
import {version as versionCommand} from './commands/version';
10+
import {hasArgs} from './utils/args.utils';
1011

1112
export const run = async () => {
12-
const [cmd, ...rest] = process.argv.slice(2);
13+
const [cmd, ...args] = process.argv.slice(2);
14+
15+
if (hasArgs({args, options: ['-h', '--help']})) {
16+
switch (cmd) {
17+
case 'upgrade':
18+
console.log(helpUpgrade);
19+
break;
20+
default:
21+
console.log(helpCommand(cmd));
22+
}
23+
return;
24+
}
1325

1426
switch (cmd) {
1527
case 'login':
@@ -34,10 +46,7 @@ export const run = async () => {
3446
await versionCommand();
3547
break;
3648
case 'upgrade':
37-
await upgrade();
38-
break;
39-
case 'help':
40-
console.log(help);
49+
await upgrade(args);
4150
break;
4251
default:
4352
console.log(`${red('Unknown command.')}`);

src/utils/args.utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const hasArgs = ({args, options}: {args?: string[]; options: string[]}): boolean =>
2+
args?.find((arg) => options.includes(arg)) !== undefined;
3+
4+
export const nextArg = ({args, option}: {args?: string[]; option: string}): string | undefined => {
5+
const index = (args ?? []).findIndex((arg) => arg === option);
6+
7+
if (index === -1) {
8+
return undefined;
9+
}
10+
11+
return args?.[index + 1];
12+
};

src/utils/wasm.utils.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createHash} from 'crypto';
2+
import {readFile} from 'fs/promises';
23
import {cyan} from 'kleur';
34
import ora from 'ora';
45
import {downloadFromURL} from './download.utils';
@@ -7,11 +8,15 @@ import {confirmAndExit, NEW_CMD_LINE} from './prompt.utils';
78

89
const executeUpgradeWasm = async ({
910
upgrade,
10-
wasm
11+
wasm,
12+
hash
1113
}: {
1214
wasm: Buffer;
15+
hash: string;
1316
upgrade: ({wasm_module}: {wasm_module: Array<number>}) => Promise<void>;
1417
}) => {
18+
await confirmAndExit(`Wasm hash is ${cyan(hash)}.${NEW_CMD_LINE}Start upgrade now?`);
19+
1520
const spinner = ora('Upgrading Wasm...').start();
1621

1722
try {
@@ -23,7 +28,37 @@ const executeUpgradeWasm = async ({
2328
}
2429
};
2530

26-
export const upgradeWasm = async ({
31+
export const upgradeWasmLocal = async ({
32+
src,
33+
upgrade
34+
}: {
35+
src: string;
36+
upgrade: ({wasm_module}: {wasm_module: Array<number>}) => Promise<void>;
37+
}) => {
38+
const loadWasm = async (file: string): Promise<{hash: string; wasm: Buffer}> => {
39+
const wasm = await readFile(file);
40+
41+
return {
42+
wasm,
43+
hash: createHash('sha256').update(wasm).digest('hex')
44+
};
45+
};
46+
47+
const spinner = ora('Loading Wasm...').start();
48+
49+
try {
50+
const {hash, wasm} = await loadWasm(src);
51+
52+
spinner.stop();
53+
54+
await executeUpgradeWasm({upgrade, wasm, hash});
55+
} catch (err: unknown) {
56+
spinner.stop();
57+
throw err;
58+
}
59+
};
60+
61+
export const upgradeWasmGitHub = async ({
2762
asset,
2863
upgrade
2964
}: {
@@ -48,9 +83,7 @@ export const upgradeWasm = async ({
4883

4984
spinner.stop();
5085

51-
await confirmAndExit(`Wasm hash is ${cyan(hash)}.${NEW_CMD_LINE}Start upgrade now?`);
52-
53-
await executeUpgradeWasm({upgrade, wasm});
86+
await executeUpgradeWasm({upgrade, wasm, hash});
5487
} catch (err: unknown) {
5588
spinner.stop();
5689
throw err;

0 commit comments

Comments
 (0)