Skip to content
10 changes: 10 additions & 0 deletions cli/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
},
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-restricted-properties": [
"error",
{
"object": "process",
"property": "exit",
"message": "Use cliError() or cliAbort() from error.ts instead of process.exit()"
}
]
}
}
63 changes: 24 additions & 39 deletions cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import process from "node:process";

import { resolve } from "node:path";
import { cliError, handleCliError } from "./error.js";
import { getNetwork } from "./api/network.js";
import { cacheSize, cleanCache, show } from "./cache.js";
import { add } from "./commands/add.js";
import { bench } from "./commands/bench.js";
import { build, DEFAULT_BUILD_OUTPUT_DIR } from "./commands/build.js";

Check warning on line 12 in cli/cli.ts

View workflow job for this annotation

GitHub Actions / check

'DEFAULT_BUILD_OUTPUT_DIR' is defined but never used
import { bump } from "./commands/bump.js";
import { check } from "./commands/check.js";
import { checkCandid } from "./commands/check-candid.js";
Expand Down Expand Up @@ -122,9 +123,7 @@
]),
)
.action(async (pkg, options) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
await add(pkg, options);
});

Expand All @@ -143,9 +142,7 @@
]),
)
.action(async (pkg, options) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
await remove(pkg, options);
});

Expand All @@ -164,9 +161,7 @@
]),
)
.action(async (options) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();

let compatible = await checkApiCompatibility();
if (!compatible) {
Expand All @@ -187,7 +182,7 @@
await resolvePackages({ conflicts: "warning" });

if (!ok) {
process.exit(1);
cliError();
}
});

Expand All @@ -200,9 +195,7 @@
.option("--no-bench", "Do not run benchmarks")
.option("--verbose")
.action(async (options) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
let compatible = await checkApiCompatibility();
if (compatible) {
await publish(options);
Expand Down Expand Up @@ -242,9 +235,7 @@
.default("warning"),
)
.action(async (options) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
if (options.install) {
await installAll({
silent: true,
Expand All @@ -263,7 +254,7 @@
.command("moc-args")
.description("Print global moc compiler flags from [moc] config section")
.action(async () => {
checkConfigFile(true);
checkConfigFile();
let config = readConfig();
let args = getGlobalMocArgs(config);
if (args.length) {
Expand Down Expand Up @@ -304,7 +295,7 @@
.addOption(new Option("--output, -o <output>", "Output directory"))
.allowUnknownOption(true) // TODO: restrict unknown before "--"
.action(async (canisters, options) => {
checkConfigFile(true);
checkConfigFile();
const { extraArgs, args } = parseExtraArgs(canisters);
await installAll({
silent: true,
Expand Down Expand Up @@ -333,7 +324,7 @@
)
.allowUnknownOption(true)
.action(async (files, options) => {
checkConfigFile(true);
checkConfigFile();
const { extraArgs, args: fileList } = parseExtraArgs(files);
await installAll({
silent: true,
Expand All @@ -351,7 +342,7 @@
.command("check-candid <new-candid> <original-candid>")
.description("Check Candid interface compatibility between two Candid files")
.action(async (newCandid, originalCandid) => {
checkConfigFile(true);
checkConfigFile();
await installAll({
silent: true,
lock: "ignore",
Expand All @@ -369,7 +360,7 @@
.option("--verbose", "Verbose console output")
.allowUnknownOption(true)
.action(async (oldFile, canister, options) => {
checkConfigFile(true);
checkConfigFile();
const { extraArgs } = parseExtraArgs();
await installAll({
silent: true,
Expand Down Expand Up @@ -408,7 +399,7 @@
.option("-w, --watch", "Enable watch mode")
.option("--verbose", "Verbose output")
.action(async (filter, options) => {
checkConfigFile(true);
checkConfigFile();
await installAll({
silent: true,
lock: "ignore",
Expand Down Expand Up @@ -444,7 +435,7 @@
// .addOption(new Option('--force-gc', 'Force GC'))
.addOption(new Option("--verbose", "Show more information"))
.action(async (filter, options) => {
checkConfigFile(true);
checkConfigFile();
await installAll({
silent: true,
lock: "ignore",
Expand All @@ -458,9 +449,7 @@
.command("template")
.description("Apply template")
.action(async () => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
await template();
});

Expand Down Expand Up @@ -659,9 +648,7 @@
.addArgument(new Argument("<tool>").choices(TOOLCHAINS))
.addArgument(new Argument("[version]"))
.action(async (tool, version) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
await toolchain.use(tool, version);
});

Expand All @@ -672,9 +659,7 @@
)
.addArgument(new Argument("[tool]").choices(TOOLCHAINS))
.action(async (tool?: Tool) => {
if (!checkConfigFile()) {
process.exit(1);
}
checkConfigFile();
await toolchain.update(tool);
});

Expand Down Expand Up @@ -732,7 +717,7 @@
.option("-g, --generate", "Generate declarations for Motoko canisters")
.option("-d, --deploy", "Deploy Motoko canisters")
.action(async (options) => {
checkConfigFile(true);
checkConfigFile();
await watch(options);
});

Expand All @@ -745,10 +730,10 @@
new Option("--check", "Check code formatting (do not change source files)"),
)
.action(async (filter, options) => {
checkConfigFile(true);
checkConfigFile();
let { ok } = await format(filter, options);
if (!ok) {
process.exit(1);
cliError();
}
});

Expand All @@ -766,7 +751,7 @@
)
.allowUnknownOption(true)
.action(async (filter, options) => {
checkConfigFile(true);
checkConfigFile();
const { extraArgs } = parseExtraArgs();
await lint(filter, {
...options,
Expand All @@ -790,7 +775,7 @@
.choices(["md", "adoc", "html"]),
)
.action(async (options) => {
checkConfigFile(true);
checkConfigFile();
await docs(options);
});

Expand All @@ -815,9 +800,9 @@
).default(70),
)
.action(async (options) => {
checkConfigFile(true);
checkConfigFile();
await docsCoverage(options);
});
program.addCommand(docsCommand);

program.parse();
program.parseAsync().catch(handleCliError);
10 changes: 4 additions & 6 deletions cli/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { checkRequirements } from "../check-requirements.js";
import { syncLocalCache } from "./install/sync-local-cache.js";
import { notifyInstalls } from "../notify-installs.js";
import { resolvePackages } from "../resolve-packages.js";
import { cliError } from "../error.js";

type AddOptions = {
verbose?: boolean;
Expand All @@ -30,9 +31,7 @@ export async function add(
{ verbose = false, dev = false, lock }: AddOptions = {},
asName?: string,
) {
if (!checkConfigFile()) {
return;
}
checkConfigFile();

let config = readConfig();
if (dev) {
Expand Down Expand Up @@ -87,8 +86,7 @@ export async function add(
} else {
let versionRes = await getHighestVersion(name);
if ("err" in versionRes) {
console.log(chalk.red("Error: ") + versionRes.err);
return;
cliError(versionRes.err);
}
ver = versionRes.ok;
}
Expand All @@ -105,7 +103,7 @@ export async function add(
verbose: verbose,
});
if (!res) {
process.exit(1);
cliError();
}
} else if (!pkgDetails.path) {
let res = await installMopsDep(pkgDetails.name, pkgDetails.version, {
Expand Down
6 changes: 2 additions & 4 deletions cli/commands/available-updates.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import process from "node:process";
import chalk from "chalk";
import { mainActor } from "../api/actors.js";
import { Config } from "../types.js";
import { getDepName, getDepPinnedVersion } from "../helpers/get-dep-name.js";
import { SemverPart } from "../declarations/main/main.did.js";
import { cliError } from "../error.js";

// [pkg, oldVersion, newVersion]
export async function getAvailableUpdates(
Expand Down Expand Up @@ -52,8 +51,7 @@ export async function getAvailableUpdates(
);

if ("err" in res) {
console.log(chalk.red("Error:"), res.err);
process.exit(1);
cliError("Error: " + res.err);
}

return res.ok
Expand Down
8 changes: 3 additions & 5 deletions cli/commands/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getDfxVersion } from "../helpers/get-dfx-version.js";
import { getMocPath } from "../helpers/get-moc-path.js";
import { sources } from "./sources.js";
import { MOTOKO_GLOB_CONFIG } from "../constants.js";
import { cliError } from "../error.js";

import { Benchmark, Benchmarks } from "../declarations/main/main.did.js";
import { BenchResult, _SERVICE } from "../declarations/bench/bench.did.js";
Expand Down Expand Up @@ -75,12 +76,9 @@ export async function bench(
if (replicaType === "pocket-ic" && !config.toolchain?.["pocket-ic"]) {
let dfxVersion = getDfxVersion();
if (!dfxVersion || new SemVer(dfxVersion).compare("0.24.1") < 0) {
console.log(
chalk.red(
"Please update dfx to the version >=0.24.1 or specify pocket-ic version in mops.toml",
),
cliError(
"Please update dfx to the version >=0.24.1 or specify pocket-ic version in mops.toml",
);
process.exit(1);
} else {
replicaType = "dfx-pocket-ic";
}
Expand Down
7 changes: 5 additions & 2 deletions cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execa } from "execa";
import { exists } from "fs-extra";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { cliError } from "../error.js";
import { CliError, cliError } from "../error.js";
import { isCandidCompatible } from "../helpers/is-candid-compatible.js";
import { resolveCanisterConfigs } from "../helpers/resolve-canisters.js";
import { CanisterConfig, Config } from "../types.js";
Expand Down Expand Up @@ -147,6 +147,9 @@ export async function build(
);
}
} catch (err: any) {
if (err instanceof CliError) {
throw err;
}
cliError(
`Error during Candid compatibility check for canister ${canisterName}${err?.message ? `\n${err.message}` : ""}`,
);
Expand All @@ -173,7 +176,7 @@ export async function build(
);
await writeFile(wasmPath, newWasm);
} catch (err: any) {
if (err.message?.includes("Build failed for canister")) {
if (err instanceof CliError) {
throw err;
}
cliError(
Expand Down
14 changes: 4 additions & 10 deletions cli/commands/bump.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import process from "node:process";
import prompts from "prompts";
import chalk from "chalk";
import { checkConfigFile, readConfig, writeConfig } from "../mops.js";
import { cliError } from "../error.js";

export async function bump(part: string) {
if (!checkConfigFile()) {
return;
}
checkConfigFile();

if (part && !["major", "minor", "patch"].includes(part)) {
console.log(
chalk.red("Unknown version part. Available parts: major, minor, patch"),
);
process.exit(1);
cliError("Unknown version part. Available parts: major, minor, patch");
}

let config = readConfig();

if (!config.package) {
console.log(chalk.red("No [package] section found in mops.toml."));
process.exit(1);
cliError("No [package] section found in mops.toml.");
}

console.log(`Current version: ${chalk.yellow.bold(config.package.version)}`);
Expand Down
5 changes: 4 additions & 1 deletion cli/commands/check-candid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk";
import { isCandidCompatible } from "../helpers/is-candid-compatible.js";
import { cliError } from "../error.js";
import { CliError, cliError } from "../error.js";

export interface CheckCandidOptions {
verbose?: boolean;
Expand All @@ -17,6 +17,9 @@ export async function checkCandid(
}
console.log(chalk.green("✓ Candid compatibility check passed"));
} catch (error: any) {
if (error instanceof CliError) {
throw error;
}
cliError(
`Error while checking Candid compatibility${error?.message ? `\n${error.message}` : ""}`,
);
Expand Down
Loading
Loading