Skip to content
Draft
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@junobuild/admin": "^2.3.0",
"@junobuild/cdn": "^1.3.2",
"@junobuild/cli-tools": "^0.8.0",
"@junobuild/config": "^2.3.0-next-2025-09-27.1",
"@junobuild/config": "^2.3.0-next-2025-09-27.2",
"@junobuild/config-loader": "^0.4.5",
"@junobuild/core": "^2.2.0",
"@junobuild/did-tools": "^0.3.3",
Expand Down
66 changes: 56 additions & 10 deletions src/services/emulator/_runner.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {nonNullish} from '@dfinity/utils';
import {assertAnswerCtrlC, execute, spawn} from '@junobuild/cli-tools';
import {type EmulatorPorts} from '@junobuild/config';
import {type EmulatorPorts, type EmulatorRunner} from '@junobuild/config';
import {red, yellow} from 'kleur';
import {basename, join} from 'node:path';
import prompts from 'prompts';
Expand All @@ -23,6 +23,7 @@ import {
assertContainerRunnerRunning,
checkDockerVersion,
hasExistingContainer,
hasExistingVolume,
isContainerRunning
} from '../../utils/runner.utils';
import {initConfigNoneInteractive} from '../config/init.services';
Expand Down Expand Up @@ -100,9 +101,10 @@ const promptRunnerType = async (): Promise<{runnerType: EmulatorRunnerType}> =>
choices: [
{
title: 'Docker',
value: `docker`
value: 'docker'
},
{title: `Podman`, value: `podman`}
{title: 'Podman', value: 'podman'},
{title: 'Apple container', value: 'container'}
]
});

Expand Down Expand Up @@ -209,12 +211,28 @@ const startEmulator = async ({config: extendedConfig}: {config: CliEmulatorConfi
// Podman does not auto create the path folders.
await createDeployTargetDir({targetDeploy});

// Apple container does not auto create the volume.
const {result: createResult} = await createVolume({volume, runner});

if (createResult === 'error') {
console.log(red(`Unable to create a volume ${volume} for ${runner}.`));
return;
}

const image = config.runner?.image ?? `junobuild/${emulatorType}:latest`;

const platform = config.runner?.platform;

const network = config?.network;

const volumes = [
`${volume}:/juno/.juno`,
...(nonNullish(configFile) && nonNullish(configFilePath)
? [`${configFilePath}:/juno/${configFile}`]
: []),
`${targetDeploy}:/juno/target/deploy`
];

await execute({
command: runner,
args: [
Expand All @@ -233,13 +251,7 @@ const startEmulator = async ({config: extendedConfig}: {config: CliEmulatorConfi
]
: []),
...(nonNullish(network) ? ['-e', `NETWORK=${JSON.stringify(network)}`] : []),
'-v',
`${volume}:/juno/.juno`,
...(nonNullish(configFile) && nonNullish(configFilePath)
? ['-v', `${configFilePath}:/juno/${configFile}`]
: []),
'-v',
`${targetDeploy}:/juno/target/deploy`,
...volumes.flatMap((v) => ['-v', v]),
...(nonNullish(platform) ? [`--platform=${platform}`] : []),
image
]
Expand Down Expand Up @@ -278,3 +290,37 @@ const assertContainerRunning = async ({

return result;
};

const createVolume = async ({
volume,
runner
}: Pick<CliEmulatorDerivedConfig, 'runner'> & Required<Pick<EmulatorRunner, 'volume'>>): Promise<{
result: 'success' | 'error' | 'skip';
}> => {
// Docker and Podman auto-create the volume on run
if (runner !== 'container') {
return {result: 'skip'};
}

const check = await hasExistingVolume({
volume,
runner
});

if ('err' in check) {
return {result: 'error'};
}

const {exist} = check;

if (exist) {
return {result: 'skip'};
}

await execute({
command: runner,
args: ['volume', 'create', volume]
});

return {result: 'success'};
};
79 changes: 76 additions & 3 deletions src/utils/runner.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {notEmptyString} from '@dfinity/utils';
import {spawn} from '@junobuild/cli-tools';
import {type EmulatorRunner} from '@junobuild/config';
import {green, red, yellow} from 'kleur';
import {lt} from 'semver';
import {DOCKER_MIN_VERSION} from '../constants/dev.constants';
Expand Down Expand Up @@ -35,9 +37,13 @@ export const assertContainerRunnerRunning = async ({
runner
}: Pick<CliEmulatorDerivedConfig, 'runner'>) => {
try {
// container does not support ps
// Reference: https://github.com/apple/container/pull/299
const args = runner === 'container' ? ['ls', '--quiet'] : ['ps', '--quiet'];

await spawn({
command: runner,
args: ['ps', '--quiet'],
args,
silentOut: true
});
} catch (_e: unknown) {
Expand All @@ -54,13 +60,64 @@ export const hasExistingContainer = async ({
> => {
try {
let output = '';

const args =
runner === 'container' ? ['ls', '-aq'] : ['ps', '-aq', '-f', `name=^/${containerName}$`];

await spawn({
command: runner,
args: ['ps', '-aq', '-f', `name=^/${containerName}$`],
args,
stdout: (o) => (output += o),
silentOut: true
});

if (runner === 'container') {
const exist = output
.split(/\r?\n/)
.map((line) => line.trim())
.filter(notEmptyString)
.some((name) => name === containerName);

return {exist};
}

return {exist: output.trim().length > 0};
} catch (err: unknown) {
return {err};
}
};

export const hasExistingVolume = async ({
volume,
runner
}: Pick<CliEmulatorDerivedConfig, 'runner'> & Required<Pick<EmulatorRunner, 'volume'>>): Promise<
{exist: boolean} | {err: unknown}
> => {
try {
let output = '';

const args =
runner === 'container'
? ['volume', 'ls', '-q']
: ['volume', 'ls', '-q', '-f', `name=^${volume}$`];

await spawn({
command: runner,
args,
stdout: (o) => (output += o),
silentOut: true
});

if (runner === 'container') {
const exist = output
.split(/\r?\n/)
.map((line) => line.trim())
.filter(notEmptyString)
.some((name) => name === volume);

return {exist};
}

return {exist: output.trim().length > 0};
} catch (err: unknown) {
return {err};
Expand All @@ -75,13 +132,29 @@ export const isContainerRunning = async ({
> => {
try {
let output = '';

const args =
runner === 'container'
? ['ls', '--quiet']
: ['ps', '--quiet', '-f', `name=^/${containerName}$`];

await spawn({
command: runner,
args: ['ps', '--quiet', '-f', `name=^/${containerName}$`],
args,
stdout: (o) => (output += o),
silentOut: true
});

if (runner === 'container') {
const running = output
.split(/\r?\n/)
.map((line) => line.trim())
.filter(notEmptyString)
.some((name) => name === containerName);

return {running};
}

return {running: output.trim().length > 0};
} catch (err: unknown) {
return {err};
Expand Down
Loading