Skip to content

Commit 4907522

Browse files
feat: juno emulator pull (#512)
* feat: juno emulator pull Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * feat: print out Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * chore: fmt Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * chore: lint Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> --------- Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
1 parent 400d12f commit 4907522

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/commands/emulator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {logHelpEmulator} from '../help/emulator.help';
33
import {logHelpEmulatorStart} from '../help/emulator.start.help';
44
import {logHelpEmulatorWait} from '../help/emulator.wait.help';
55
import {clear} from '../services/emulator/clear.services';
6+
import {pull} from '../services/emulator/pull.services';
67
import {start} from '../services/emulator/start.services';
78
import {stop} from '../services/emulator/stop.services';
89
import {wait} from '../services/emulator/wait.services';
@@ -23,6 +24,9 @@ export const emulator = async (args?: string[]) => {
2324
case 'clear':
2425
await clear();
2526
break;
27+
case 'pull':
28+
await pull();
29+
break;
2630
default:
2731
console.log(red('Unknown subcommand.'));
2832
logHelpEmulator(args);

src/constants/help.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const HOSTING_PRUNE_DESCRIPTION =
3434
export const EMULATOR_START_DESCRIPTION = 'Start the emulator for local development.';
3535
export const EMULATOR_WAIT_DESCRIPTION = 'Wait until the emulator is ready.';
3636
export const EMULATOR_CLEAR_DESCRIPTION = 'Clear the local emulator state (volume and container).';
37+
export const EMULATOR_PULL_DESCRIPTION = 'Pull the latest emulator image.';
3738

3839
export const FUNCTIONS_PUBLISH_DESCRIPTION = 'Publish a new version of your serverless functions.';
3940
export const FUNCTIONS_UPGRADE_DESCRIPTION = 'Upgrade your serverless functions.';

src/help/emulator.help.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {cyan, green, magenta, yellow} from 'kleur';
22
import {
33
EMULATOR_CLEAR_DESCRIPTION,
44
EMULATOR_DESCRIPTION,
5+
EMULATOR_PULL_DESCRIPTION,
56
EMULATOR_START_DESCRIPTION,
67
EMULATOR_WAIT_DESCRIPTION
78
} from '../constants/help.constants';
@@ -12,6 +13,7 @@ const usage = `Usage: ${green('juno')} ${cyan('emulator')} ${magenta('<subcomman
1213
1314
Subcommands:
1415
${magenta('clear')} ${EMULATOR_CLEAR_DESCRIPTION}
16+
${magenta('pull')} ${EMULATOR_PULL_DESCRIPTION}
1517
${magenta('start')} ${EMULATOR_START_DESCRIPTION}
1618
${magenta('stop')} Stop the local network.
1719
${magenta('wait')} ${EMULATOR_WAIT_DESCRIPTION}`;

src/services/emulator/_runner.services.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {nonNullish} from '@dfinity/utils';
22
import {assertAnswerCtrlC, execute, spawn} from '@junobuild/cli-tools';
33
import {type EmulatorPorts} from '@junobuild/config';
4-
import {red, yellow} from 'kleur';
4+
import {green, red, yellow} from 'kleur';
55
import {basename, join} from 'node:path';
6+
import ora from 'ora';
67
import prompts from 'prompts';
78
import {readEmulatorConfig} from '../../configs/emulator.config';
89
import {junoConfigExist, junoConfigFile} from '../../configs/juno.config';
@@ -55,6 +56,14 @@ export const clearContainerAndVolume = async () => {
5556
await runWithConfig({fn});
5657
};
5758

59+
export const pullImage = async () => {
60+
const fn: RunWithConfigFn = async (args) => {
61+
await pullEmulator(args);
62+
};
63+
64+
await runWithConfig({fn});
65+
};
66+
5867
type RunWithConfigFn = (params: {config: CliEmulatorConfig}) => Promise<void>;
5968

6069
const runWithConfig = async ({fn}: {fn: RunWithConfigFn}) => {
@@ -299,6 +308,39 @@ const clearEmulator = async ({config: {config, derivedConfig}}: {config: CliEmul
299308
});
300309
};
301310

311+
const pullEmulator = async ({config: {derivedConfig}}: {config: CliEmulatorConfig}) => {
312+
const {runner, image} = derivedConfig;
313+
314+
await confirmAndExit(
315+
`Are you sure you want to pull the emulator image "${image}"? You will need to ${yellow('clear')} the emulator afterward to apply the update.`
316+
);
317+
318+
const spinner = ora('Pulling...').start();
319+
320+
try {
321+
await spawn({
322+
command: runner,
323+
args: ['pull', image],
324+
stdout: (o) => {
325+
// We print out to display some sort of progression
326+
console.log(o);
327+
},
328+
silentOut: true
329+
});
330+
331+
spinner.stop();
332+
333+
console.log('\nDone ✅\n');
334+
335+
console.log(
336+
`Run ${yellow('juno emulator clear')} to reset the state, then ${green('juno emulator start')} to use the updated image.`
337+
);
338+
} catch (error: unknown) {
339+
spinner.stop();
340+
throw error;
341+
}
342+
};
343+
302344
const assertContainerRunning = async ({
303345
containerName,
304346
runner
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {pullImage} from './_runner.services';
2+
3+
export const pull = async () => {
4+
await pullImage();
5+
};

0 commit comments

Comments
 (0)