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
6 changes: 5 additions & 1 deletion src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {red} from 'kleur';
import {logHelpDev} from '../help/dev.help';
import {logHelpDevStart} from '../help/dev.start.help';
import {logHelpDevWait} from '../help/dev.wait.help';
import {logHelpFunctionsBuild} from '../help/functions.build.help';
import {logHelpFunctionsEject} from '../help/functions.eject.help';
import {start} from '../services/dev/start.services';
Expand All @@ -20,7 +21,7 @@ export const dev = async (args?: string[]) => {
await stop();
break;
case 'wait':
await wait();
await wait(args);
break;
case 'eject':
await eject(args);
Expand All @@ -41,6 +42,9 @@ export const helpDev = (args?: string[]) => {
case 'start':
logHelpDevStart(args);
break;
case 'wait':
logHelpDevWait(args);
break;
case 'build':
logHelpFunctionsBuild(args);
break;
Expand Down
1 change: 1 addition & 0 deletions src/constants/help.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const WHOAMI_DESCRIPTION =
'Display your current profile, access key, and links to your satellite.';

export const DEV_START_DESCRIPTION = 'Start a local Internet Computer network in a container.';
export const DEV_WAIT_DESCRIPTION = 'Wait until the emulator is ready.';

export const FUNCTIONS_PUBLISH_DESCRIPTION = 'Publish a new version of your serverless functions.';
export const FUNCTIONS_UPGRADE_DESCRIPTION = 'Upgrade your serverless functions.';
Expand Down
8 changes: 6 additions & 2 deletions src/help/dev.help.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {DEV_DESCRIPTION, DEV_START_DESCRIPTION} from '../constants/help.constants';
import {
DEV_DESCRIPTION,
DEV_START_DESCRIPTION,
DEV_WAIT_DESCRIPTION
} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

Expand All @@ -8,7 +12,7 @@ const usage = `Usage: ${green('juno')} ${cyan('dev')} ${magenta('<subcommand>')}
Subcommands:
${magenta('start')} ${DEV_START_DESCRIPTION}
${magenta('stop')} Stop the local network.
${magenta('wait')} Wait until the emulator is ready.
${magenta('wait')} ${DEV_WAIT_DESCRIPTION}
${magenta('build')} Alias for ${green('juno')} ${cyan('functions')} ${magenta('build')}.
${magenta('eject')} Alias for ${green('juno')} ${cyan('functions')} ${magenta('eject')}.`;

Expand Down
28 changes: 28 additions & 0 deletions src/help/dev.wait.help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {DEV_WAIT_DESCRIPTION, OPTION_HELP} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

const usage = `Usage: ${green('juno')} ${cyan('dev')} ${magenta('wait')} ${yellow('[options]')}

Options:
${yellow('-t, --timeout')} Timeout for the emulator to be ready (in ms, default 2min).
${OPTION_HELP}`;

const doc = `${DEV_WAIT_DESCRIPTION}

\`\`\`
${usage}
\`\`\`
`;

const help = `${TITLE}

${DEV_WAIT_DESCRIPTION}

${usage}
`;

export const logHelpDevWait = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
39 changes: 36 additions & 3 deletions src/services/dev/wait.services.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import {isEmptyString} from '@dfinity/utils';
import {nextArg} from '@junobuild/cli-tools';
import {green, red} from 'kleur';
import ora from 'ora';
import {readEmulatorConfig} from '../../configs/emulator.config';
import type {CliEmulatorConfig} from '../../types/emulator';
import {dispatchRequest} from '../emulator/emulator.admin.services';

const TIMEOUT_IN_MILLISECONDS = 15000;
const DEFAULT_TIMEOUT_IN_MILLISECONDS = 2 * 60 * 1000;
const RETRY_IN_MILLISECONDS = 500;

export const wait = async () => {
export const wait = async (args?: string[]) => {
const timeout = parseTimeout(args);

if (!timeout.valid) {
console.log(
red(
`Invalid timeout argument. Must be a number in milliseconds greater than ${RETRY_IN_MILLISECONDS}ms.`
)
);
process.exit(1);
}

const parsedResult = await readEmulatorConfig();

if (!parsedResult.success) {
Expand All @@ -20,7 +33,7 @@ export const wait = async () => {

try {
status = await waitEmulatorReady({
count: TIMEOUT_IN_MILLISECONDS / RETRY_IN_MILLISECONDS,
count: timeout.value / RETRY_IN_MILLISECONDS,
config: parsedResult.config
});
} finally {
Expand All @@ -35,6 +48,26 @@ export const wait = async () => {
console.log(`Emulator is ${green('ready')}.`);
};

const parseTimeout = (args?: string[]): {valid: true; value: number} | {valid: false} => {
const timoutArg = nextArg({args, option: '-t'}) ?? nextArg({args, option: '--timeout'});

if (isEmptyString(timoutArg)) {
return {valid: true, value: DEFAULT_TIMEOUT_IN_MILLISECONDS};
}

const timeout = parseInt(timoutArg);

if (isNaN(timeout)) {
return {valid: false};
}

if (timeout < RETRY_IN_MILLISECONDS) {
return {valid: false};
}

return {valid: true, value: timeout};
};

const waitEmulatorReady = async ({
count,
config
Expand Down