Skip to content

Commit

Permalink
Add --canister / -c flag to select specific Motoko canisters (#39)
Browse files Browse the repository at this point in the history
* Add '--canister' / '-c' flag

* Implement '-c' flag

* 0.10.0
  • Loading branch information
rvanasa authored Jun 6, 2023
1 parent 31950ae commit 0f0ff1a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "mo-dev",
"version": "0.9.0",
"version": "0.10.0",
"description": "A live reload development server for Motoko smart contracts.",
"author": "DFINITY Foundation (https://dfinity.org)",
"license": "Apache-2.0",
Expand Down
14 changes: 11 additions & 3 deletions src/commands/mo-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ const testModes: TestMode[] = [];
const addTestMode = (mode: string) => testModes.push(asTestMode(mode));
const testFiles: string[] = [];
const addTestFile = (file: string) => testFiles.push(file);
const canisterNames: string[] = [];
const addCanisterName = (name: string) => canisterNames.push(name);

const examples: [string, string][] = [
['-r', 'redeploy canisters on file change'],
['-d', 'upgrade canisters on file change'],
['-d', 'redeploy canisters on file change'],
['-d -y', 'upgrade canisters on file change'],
['-g', 'generate TypeScript bindings on file change'],
['-t', 'run unit tests on file change'],
['-r -c foo_canister -c bar_canister', 'redeploy `foo_canister` and `bar_canister` on file change'],
];

const {
Expand All @@ -30,7 +33,6 @@ const {
test,
yes,
hotReload,
ci,
} = program
.name('mo-dev')
.description(
Expand All @@ -55,6 +57,11 @@ const {
`only run tests with the given file name prefix`,
addTestFile,
)
.option(
'-c, --canister <canister>',
`use the given Motoko canister`,
addCanisterName,
)
.option(
'-y, --yes',
`respond "yes" to reinstall prompts (may reset canister state)`,
Expand Down Expand Up @@ -93,6 +100,7 @@ const settings: Settings = {
test: !!test || defaultSettings.test,
testModes: testModes.length ? testModes : defaultSettings.testModes,
testFiles: testFiles.length ? testFiles : defaultSettings.testModes,
canisterNames: canisterNames.length ? canisterNames : defaultSettings.canisterNames,
reinstall: !!yes || defaultSettings.reinstall,
hotReload: !!hotReload || defaultSettings.hotReload,
// ci: !!ci || defaultSettings.ci,
Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Settings {
test: boolean;
testModes: TestMode[];
testFiles: string[];
canisterNames: string[];
reinstall: boolean;
hotReload: boolean;
ci: boolean;
Expand All @@ -29,6 +30,7 @@ export const defaultSettings: Settings = {
test: false,
testModes: ['interpreter'],
testFiles: [],
canisterNames: [],
reinstall: false,
hotReload: false,
ci: process.env.CI && process.env.CI !== '0' && process.env.CI !== 'false',
Expand Down
28 changes: 28 additions & 0 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ export async function watch(settings: Settings) {
return;
}
canisters = getDfxCanisters(directory, dfxConfig);
if (settings.canisterNames.length) {
const showExpected = settings.canisterNames.some((name) => {
if (!canisters.some((c) => name === c.alias)) {
log(
0,
'unexpected canister:',
pc.yellow(pc.bold(name)),
);
return true;
}
});
if (showExpected) {
log(
0,
`options:`,
pc.gray(
canisters
.map((c) => pc.bold(pc.green(c.alias)))
.join(', '),
),
);
}
canisters = canisters.filter((canister) =>
settings.canisterNames.some(
(name) => name === canister.alias,
),
);
}
} catch (err) {
console.error(
`Error while loading 'dfx.json' file:\n${err.message || err}`,
Expand Down

0 comments on commit 0f0ff1a

Please sign in to comment.