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
4 changes: 4 additions & 0 deletions src/commands/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {red} from 'kleur';
import {logHelpEmulator} from '../help/emulator.help';
import {logHelpEmulatorStart} from '../help/emulator.start.help';
import {logHelpEmulatorWait} from '../help/emulator.wait.help';
import {clear} from '../services/emulator/clear.services';
import {start} from '../services/emulator/start.services';
import {stop} from '../services/emulator/stop.services';
import {wait} from '../services/emulator/wait.services';
Expand All @@ -19,6 +20,9 @@ export const emulator = async (args?: string[]) => {
case 'wait':
await wait(args);
break;
case 'clear':
await clear();
break;
default:
console.log(red('Unknown subcommand.'));
logHelpEmulator(args);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/hosting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {red} from 'kleur';
import {logHelpEmulatorClear} from '../help/hosting.clear.help';
import {logHelpHostingClear} from '../help/hosting.clear.help';
import {logHelpHostingDeploy} from '../help/hosting.deploy.help';
import {logHelpHosting} from '../help/hosting.help';
import {clear} from '../services/assets/clear.services';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const helpHosting = (args?: string[]) => {
logHelpHostingDeploy(args);
break;
case 'clear':
logHelpEmulatorClear(args);
logHelpHostingClear(args);
break;
default:
logHelpHosting(args);
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 @@ -31,6 +31,7 @@ export const HOSTING_CLEAR_DESCRIPTION =

export const EMULATOR_START_DESCRIPTION = 'Start the emulator for local development.';
export const EMULATOR_WAIT_DESCRIPTION = 'Wait until the emulator is ready.';
export const EMULATOR_CLEAR_DESCRIPTION = 'Clear the local emulator state (volume and container).';

export const FUNCTIONS_PUBLISH_DESCRIPTION = 'Publish a new version of your serverless functions.';
export const FUNCTIONS_UPGRADE_DESCRIPTION = 'Upgrade your serverless functions.';
Expand Down
2 changes: 2 additions & 0 deletions src/help/emulator.help.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {
EMULATOR_CLEAR_DESCRIPTION,
EMULATOR_DESCRIPTION,
EMULATOR_START_DESCRIPTION,
EMULATOR_WAIT_DESCRIPTION
Expand All @@ -10,6 +11,7 @@ import {TITLE} from './help';
const usage = `Usage: ${green('juno')} ${cyan('emulator')} ${magenta('<subcommand>')} ${yellow('[options]')}

Subcommands:
${magenta('clear')} ${EMULATOR_CLEAR_DESCRIPTION}
${magenta('start')} ${EMULATOR_START_DESCRIPTION}
${magenta('stop')} Stop the local network.
${magenta('wait')} ${EMULATOR_WAIT_DESCRIPTION}`;
Expand Down
2 changes: 1 addition & 1 deletion src/help/hosting.clear.help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ ${HOSTING_CLEAR_DESCRIPTION}
${usage}
`;

export const logHelpEmulatorClear = (args?: string[]) => {
export const logHelpHostingClear = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
38 changes: 38 additions & 0 deletions src/services/emulator/_runner.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type EmulatorType
} from '../../types/emulator';
import {isHeadless} from '../../utils/process.utils';
import {confirmAndExit} from '../../utils/prompt.utils';
import {
assertContainerRunnerRunning,
checkDockerVersion,
Expand Down Expand Up @@ -46,6 +47,14 @@ export const stopContainer = async () => {
await runWithConfig({fn});
};

export const clearContainerAndVolume = async () => {
const fn: RunWithConfigFn = async (args) => {
await clearEmulator(args);
};

await runWithConfig({fn});
};

type RunWithConfigFn = (params: {config: CliEmulatorConfig}) => Promise<void>;

const runWithConfig = async ({fn}: {fn: RunWithConfigFn}) => {
Expand Down Expand Up @@ -262,6 +271,35 @@ const stopEmulator = async ({config: {derivedConfig}}: {config: CliEmulatorConfi
});
};

const clearEmulator = async ({config: {config, derivedConfig}}: {config: CliEmulatorConfig}) => {
const {containerName, runner} = derivedConfig;

const {running} = await assertContainerRunning({containerName, runner});

if (running) {
console.log(yellow(`The ${runner} container ${containerName} must be stopped first.`));
return;
}

const volume = config.runner?.volume ?? containerName.replaceAll('-', '_');

await confirmAndExit(
`Are you sure you want to clear the emulator container "${containerName}" and volume "${volume}"?`
);

await spawn({
command: runner,
args: ['container', 'rm', containerName],
silentOut: true
});

await spawn({
command: runner,
args: ['volume', 'rm', volume],
silentOut: true
});
};

const assertContainerRunning = async ({
containerName,
runner
Expand Down
5 changes: 5 additions & 0 deletions src/services/emulator/clear.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {clearContainerAndVolume} from './_runner.services';

export const clear = async () => {
await clearContainerAndVolume();
};