|
| 1 | +import chalk from "chalk"; |
| 2 | +import { execa } from "execa"; |
| 3 | +import { exists } from "fs-extra"; |
| 4 | +import { mkdir } from "node:fs/promises"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { getMocPath } from "../helpers/get-moc-path.js"; |
| 7 | +import { readConfig } from "../mops.js"; |
| 8 | +import { CanisterConfig } from "../types.js"; |
| 9 | +import { sourcesArgs } from "./sources.js"; |
| 10 | + |
| 11 | +export interface BuildOptions { |
| 12 | + outputDir: string; |
| 13 | + verbose: boolean; |
| 14 | + extraArgs: string[]; |
| 15 | +} |
| 16 | + |
| 17 | +export const DEFAULT_BUILD_OUTPUT_DIR = ".mops/.build"; |
| 18 | + |
| 19 | +export async function build( |
| 20 | + canisterNames: string[] | undefined, |
| 21 | + options: Partial<BuildOptions>, |
| 22 | +): Promise<void> { |
| 23 | + if (canisterNames?.length == 0) { |
| 24 | + throw new Error("No canisters specified to build"); |
| 25 | + } |
| 26 | + |
| 27 | + let outputDir = options.outputDir ?? DEFAULT_BUILD_OUTPUT_DIR; |
| 28 | + let mocPath = getMocPath(); |
| 29 | + let canisters: Record<string, CanisterConfig> = {}; |
| 30 | + let config = readConfig(); |
| 31 | + if (config.canisters) { |
| 32 | + canisters = |
| 33 | + Object.fromEntries( |
| 34 | + Object.entries(config.canisters).map(([name, c]) => |
| 35 | + typeof c === "string" ? [name, { main: c }] : [name, c], |
| 36 | + ), |
| 37 | + ) ?? {}; |
| 38 | + } |
| 39 | + if (!Object.keys(canisters).length) { |
| 40 | + throw new Error(`No Motoko canisters found in mops.toml configuration`); |
| 41 | + } |
| 42 | + |
| 43 | + if (canisterNames) { |
| 44 | + canisterNames = canisterNames.filter((name) => name in canisters); |
| 45 | + if (canisterNames.length === 0) { |
| 46 | + throw new Error("No valid canister names specified"); |
| 47 | + } |
| 48 | + for (let name of canisterNames) { |
| 49 | + if (!(name in canisters)) { |
| 50 | + throw new Error( |
| 51 | + `Motoko canister '${name}' not found in mops.toml configuration`, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (!(await exists(outputDir))) { |
| 58 | + await mkdir(outputDir, { recursive: true }); |
| 59 | + } |
| 60 | + for (let [canisterName, canister] of Object.entries(canisters)) { |
| 61 | + options.verbose && console.time(`build canister ${canisterName}`); |
| 62 | + console.log(chalk.blue("build canister"), chalk.bold(canisterName)); |
| 63 | + let motokoPath = canister.main; |
| 64 | + if (!motokoPath) { |
| 65 | + throw new Error(`No main file is specified for canister ${canisterName}`); |
| 66 | + } |
| 67 | + let args = [ |
| 68 | + "-c", |
| 69 | + "--idl", |
| 70 | + "-o", |
| 71 | + join(outputDir, `${canisterName}.wasm`), |
| 72 | + motokoPath, |
| 73 | + ...(options.extraArgs ?? []), |
| 74 | + ...(await sourcesArgs()).flat(), |
| 75 | + ]; |
| 76 | + if (config.build?.args) { |
| 77 | + if (typeof config.build.args === "string") { |
| 78 | + throw new Error( |
| 79 | + `[build] config 'args' should be an array of strings in mops.toml config file`, |
| 80 | + ); |
| 81 | + } |
| 82 | + args.push(...config.build.args); |
| 83 | + } |
| 84 | + if (canister.args) { |
| 85 | + if (typeof canister.args === "string") { |
| 86 | + throw new Error( |
| 87 | + `Canister config 'args' should be an array of strings for canister ${canisterName}`, |
| 88 | + ); |
| 89 | + } |
| 90 | + args.push(...canister.args); |
| 91 | + } |
| 92 | + try { |
| 93 | + if (options.verbose) { |
| 94 | + console.log(chalk.gray(mocPath, JSON.stringify(args))); |
| 95 | + } |
| 96 | + const result = await execa(mocPath, args, { |
| 97 | + stdio: options.verbose ? "inherit" : "pipe", |
| 98 | + reject: false, |
| 99 | + }); |
| 100 | + |
| 101 | + if (result.exitCode !== 0) { |
| 102 | + console.error( |
| 103 | + chalk.red(`Error: Failed to build canister ${canisterName}`), |
| 104 | + ); |
| 105 | + if (!options.verbose) { |
| 106 | + if (result.stderr) { |
| 107 | + console.error(chalk.red(result.stderr)); |
| 108 | + } |
| 109 | + if (result.stdout?.trim()) { |
| 110 | + console.error(chalk.yellow("Build output:")); |
| 111 | + console.error(result.stdout); |
| 112 | + } |
| 113 | + } |
| 114 | + // throw new Error( |
| 115 | + // `Build failed for canister ${canisterName} (exit code: ${result.exitCode})`, |
| 116 | + // ); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + |
| 120 | + if (options.verbose && result.stdout && result.stdout.trim()) { |
| 121 | + console.log(result.stdout); |
| 122 | + } |
| 123 | + } catch (error: any) { |
| 124 | + if (error.message?.includes("Build failed for canister")) { |
| 125 | + throw error; |
| 126 | + } |
| 127 | + console.error( |
| 128 | + chalk.red(`Error while compiling canister ${canisterName}`), |
| 129 | + ); |
| 130 | + if (error.message) { |
| 131 | + console.error(chalk.red(`Details: ${error.message}`)); |
| 132 | + } |
| 133 | + |
| 134 | + throw new Error(`Build execution failed for canister ${canisterName}`); |
| 135 | + } |
| 136 | + options.verbose && console.timeEnd(`build canister ${canisterName}`); |
| 137 | + } |
| 138 | + |
| 139 | + console.log( |
| 140 | + chalk.green(`\n✓ Built ${Object.keys(canisters).length} canisters`), |
| 141 | + ); |
| 142 | +} |
0 commit comments