diff --git a/packages/cli/commands/build.js b/packages/cli/commands/build.js index a5ce0b96..7fae1fc4 100644 --- a/packages/cli/commands/build.js +++ b/packages/cli/commands/build.js @@ -22,7 +22,7 @@ import { writeFileSync, } from "fs"; import { dirname, isAbsolute, join, relative, resolve } from "path"; -import { error, info, warn } from "../lib/colors.js"; +import { error, info, warn, trace } from "../lib/colors.js"; import { getPlatform, npmCommand } from "../lib/platforms.js"; import { Progress } from "../lib/progress.js"; @@ -80,7 +80,8 @@ export const asyncCompile = async (...args) => compileFile(...args); * @param {Object} options Options to build */ export const build = async (configPath, options = {}) => { - const { skipBundle, skipNpmInstall, silent, clean } = options; + const { skipBundle, skipNpmInstall, silent, clean, debug } = options; + const errorOrTrace = debug ? trace : error; if (!silent) info(`Compiling from "${configPath}"`); @@ -116,7 +117,7 @@ export const build = async (configPath, options = {}) => { { configs: {}, npmDeps: {}, npmDevDeps: {} } ).catch((compileError) => { progress.fail(); - console.error(compileError.message); + errorOrTrace(compileError.message); process.exit(1); }); @@ -173,11 +174,11 @@ export const build = async (configPath, options = {}) => { ); } catch (e) { progress.fail(`Error: ${e.message}`); - error(e, "Dependency Install"); + errorOrTrace(e, "Dependency Install"); } } catch (e) { progress.fail(`Error: ${e}`); - error(e, "Compilation"); + errorOrTrace(e, "Compilation"); } if (!skipNpmInstall) { @@ -216,7 +217,7 @@ export const build = async (configPath, options = {}) => { const platform = getPlatform(target); await platform.build(destination, skipBundle); } catch (e) { - error(e, "Bundling"); + errorOrTrace(e, "Bundling"); } }; @@ -260,6 +261,7 @@ export const handler = (argv) => { skipBundle: argv["skip-bundle"], skipNpmInstall: argv["skip-npm-install"], silent: argv.silent, + debug: argv.debug, }; const config = join(argv.project, "clio.toml"); build(config, options); @@ -287,6 +289,10 @@ const builder = { describe: "Wipe the build directory before build", type: "boolean", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export default { diff --git a/packages/cli/commands/deps_commands/add.js b/packages/cli/commands/deps_commands/add.js index ef4c7c70..448ee792 100644 --- a/packages/cli/commands/deps_commands/add.js +++ b/packages/cli/commands/deps_commands/add.js @@ -1,4 +1,4 @@ -import { error } from "../../lib/colors.js"; +import { error, trace } from "../../lib/colors.js"; import { installDependency } from "clio-manifest"; import { join } from "path"; @@ -17,13 +17,17 @@ export const builder = { describe: "Force fetching dependencies even if they're already fetched", type: "boolean", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export async function handler(argv) { try { const config = join(argv.project, "clio.toml"); await installDependency(config, argv.source, argv); } catch (e) { - error(e); + (argv.debug ? trace : error)(e); } } diff --git a/packages/cli/commands/deps_commands/get.js b/packages/cli/commands/deps_commands/get.js index 73a9a862..1c38b26d 100644 --- a/packages/cli/commands/deps_commands/get.js +++ b/packages/cli/commands/deps_commands/get.js @@ -1,4 +1,4 @@ -import { error } from "../../lib/colors.js"; +import { error, trace } from "../../lib/colors.js"; import { fetchDependencies } from "clio-manifest"; import { join } from "path"; @@ -11,13 +11,17 @@ export const builder = { type: "string", default: ".", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export async function handler(argv) { try { const config = join(argv.project, "clio.toml"); fetchDependencies(config); } catch (e) { - error(e); + (argv.debug ? trace : error)(e); } } diff --git a/packages/cli/commands/deps_commands/show.js b/packages/cli/commands/deps_commands/show.js index 6c072607..908a5568 100644 --- a/packages/cli/commands/deps_commands/show.js +++ b/packages/cli/commands/deps_commands/show.js @@ -4,7 +4,7 @@ import { logNoClioDeps, } from "clio-manifest"; -import { error } from "../../lib/colors.js"; +import { error, trace } from "../../lib/colors.js"; import { join } from "path"; export const command = ["$0 [project]", "show [project]"]; @@ -15,6 +15,10 @@ export const builder = { type: "string", default: ".", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export async function handler(argv) { try { @@ -30,7 +34,7 @@ export async function handler(argv) { .join("\n"); console.log(formattedDeps); } catch (e) { - error(e); + (argv.debug ? trace : error)(e); } } diff --git a/packages/cli/commands/docs.js b/packages/cli/commands/docs.js index 489bc9b2..4dd4410e 100644 --- a/packages/cli/commands/docs.js +++ b/packages/cli/commands/docs.js @@ -11,6 +11,7 @@ import { AutoComplete } from "../lib/prompt.js"; import chalk from "chalk"; import { compileFile } from "clio-core"; import enquirer from "enquirer"; +import { error, trace } from "../lib/colors.js"; const { blue, magenta, red, yellow } = chalk; @@ -23,10 +24,14 @@ export const builder = { type: "string", default: ".", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export function handler(argv) { - entry(argv.project); + entry(argv.project, argv.debug); } function onSelect(root, prompt, configPath) { @@ -38,16 +43,17 @@ function onSelect(root, prompt, configPath) { }; } -function onFnSelect(root, fnMap, prompt, configPath) { +function onFnSelect(root, fnMap, prompt, configPath, isDebugMode = false) { return async function onAnswer(answer) { await prompt.clear(); return answer == ".." ? docs(dirname(root)) - : docsFn(root, answer, fnMap[answer], configPath); + : docsFn(root, answer, fnMap[answer], configPath, isDebugMode); }; } -function selectFn(root, fns, configPath) { +function selectFn(root, fns, configPath, isDebugMode = false) { + const errorOrTrace = isDebugMode ? trace : error; const fnMap = Object.fromEntries(fns.map((fn) => [fn.name, fn])); const prompt = new AutoComplete({ name: "function", @@ -56,17 +62,18 @@ function selectFn(root, fns, configPath) { }); prompt .run() - .then(onFnSelect(root, fnMap, prompt, configPath)) - .catch(console.error); + .then(onFnSelect(root, fnMap, prompt, configPath, isDebugMode)) + .catch(errorOrTrace); } -function selectFile(root, choices, configPath) { +function selectFile(root, choices, configPath, isDebugMode = false) { + const errorOrTrace = isDebugMode ? trace : error; const prompt = new AutoComplete({ name: "file", message: "Select a file or a directory", choices: ["..", ...choices], }); - prompt.run().then(onSelect(root, prompt, configPath)).catch(console.error); + prompt.run().then(onSelect(root, prompt, configPath)).catch(errorOrTrace); } // TODO: move to clio-highlight @@ -80,7 +87,8 @@ function colorize(docs) { ); } -function docsFn(root, name, info, configPath) { +function docsFn(root, name, info, configPath, isDebugMode = false) { + const errorOrTrace = isDebugMode ? trace : error; const doc = [ info.returns ? `@returns ${info.returns}` : null, info.accepts ? `@accepts ${info.accepts.join(" ")}` : null, @@ -104,12 +112,12 @@ function docsFn(root, name, info, configPath) { .run() .then((answer) => { prompt.clear(); - if (!answer) return docsFile(root, configPath); + if (!answer) return docsFile(root, configPath, isDebugMode); }) - .catch(console.error); + .catch(errorOrTrace); } -async function docsFile(file, configPath) { +async function docsFile(file, configPath, isDebugMode) { const config = getPackageConfig(configPath); const sourceDir = getSourceFromConfig(configPath, config); const destination = getDestinationFromConfig(configPath, config); @@ -133,17 +141,17 @@ async function docsFile(file, configPath) { const fns = Object.entries(scope) .filter(([_, info]) => info.description) .map(([name, info]) => ({ ...info, name })); - return selectFn(file, fns, configPath); + return selectFn(file, fns, configPath, isDebugMode); } -function docsDirectory(projectPath, configPath) { +function docsDirectory(projectPath, configPath, isDebugMode = false) { const choices = readdirSync(projectPath) .filter((dir) => !dir.startsWith(".")) .filter((name) => { const abs = join(projectPath, name); return isDirectory(abs) || isClioFile(name); }); - selectFile(projectPath, choices, configPath); + selectFile(projectPath, choices, configPath, isDebugMode); } function isDirectory(dir) { @@ -154,15 +162,15 @@ function isClioFile(file) { return file.endsWith(".clio"); } -export function docs(path, configPath) { +export function docs(path, configPath, isDebugMode = false) { return isDirectory(path) - ? docsDirectory(path, configPath) - : docsFile(path, configPath); + ? docsDirectory(path, configPath, isDebugMode) + : docsFile(path, configPath, isDebugMode); } -function entry(projectPath) { +function entry(projectPath, isDebugMode = false) { const configPath = join(projectPath, "clio.toml"); - return docs(projectPath, configPath); + return docs(projectPath, configPath, isDebugMode); } export default { diff --git a/packages/cli/commands/highlight.js b/packages/cli/commands/highlight.js index 2bb5abab..694bd57e 100644 --- a/packages/cli/commands/highlight.js +++ b/packages/cli/commands/highlight.js @@ -1,17 +1,27 @@ -import { error } from "../lib/colors.js"; +import { error, trace } from "../lib/colors.js"; import { highlight } from "clio-highlight"; export const command = "highlight "; export const describe = "Highlight a Clio file"; export const builder = { - source: { describe: "source file to host", type: "string" }, + source: { + describe: "source file to host", + type: "string", + }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export function handler(argv) { + const { debug } = argv; + const errorOrTrace = debug ? trace : error; + try { const colorized = highlight(argv.source); if (colorized) console.log(colorized); } catch (err) { - error(err); + errorOrTrace(err); } } diff --git a/packages/cli/commands/host.js b/packages/cli/commands/host.js index bec2906b..6af8d295 100644 --- a/packages/cli/commands/host.js +++ b/packages/cli/commands/host.js @@ -5,7 +5,7 @@ import { } from "clio-manifest"; import { build } from "./build.js"; -import { error } from "../lib/colors.js"; +import { error, trace } from "../lib/colors.js"; import { getPlatform } from "../lib/platforms.js"; import { join } from "path"; @@ -27,6 +27,10 @@ export const builder = { describe: "Wipe the build directory before build", type: "boolean", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export function handler(argv) { @@ -50,7 +54,11 @@ export async function host(argv, args) { return await platform.host(destination, args); } catch (e) { - error(e); + if (argv.debug) { + trace(e); + } else { + error(e); + } } } diff --git a/packages/cli/commands/new.js b/packages/cli/commands/new.js index 2deb9581..f2a93d81 100644 --- a/packages/cli/commands/new.js +++ b/packages/cli/commands/new.js @@ -1,5 +1,5 @@ import { basename, dirname } from "path"; -import { error, info, success } from "../lib/colors.js"; +import { error, info, success, trace } from "../lib/colors.js"; import { fetchDependencies } from "clio-manifest"; import { rmSync } from "fs"; @@ -25,9 +25,18 @@ export const builder = { type: "string", default: "node", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export function handler(argv) { - createPackage(argv.project, targetAlias(argv.target), argv.template); + createPackage( + argv.project, + targetAlias(argv.target), + argv.template, + argv.debug + ); } function targetAlias(target) { @@ -89,13 +98,14 @@ async function createPackageJs(packageName, template) { export async function createPackage( packageName, target = "js", - template = "node" + template = "node", + isDebugMode = false ) { try { preValidations(packageName, target); if (target === "js") return await createPackageJs(packageName, template); } catch (e) { - error(e); + (argv.debug ? trace : error)(e); process.exit(1); } } diff --git a/packages/cli/commands/run.js b/packages/cli/commands/run.js index b7dacc9b..4de9f3c5 100644 --- a/packages/cli/commands/run.js +++ b/packages/cli/commands/run.js @@ -5,7 +5,7 @@ import { } from "clio-manifest"; import { build } from "./build.js"; -import { error } from "../lib/colors.js"; +import { error, trace } from "../lib/colors.js"; import { getPlatform } from "../lib/platforms.js"; import { join } from "path"; @@ -27,6 +27,10 @@ export const builder = { describe: "Wipe the build directory before build", type: "boolean", }, + debug: { + describe: "Show stack traces instead of error messages", + type: "boolean", + }, }; export function handler(argv) { @@ -41,6 +45,7 @@ export async function run(argv, args, forkOptions = {}) { skipBundle: true, silent: argv.silent, clean: argv.clean, + debug: argv.debug, }); const config = getPackageConfig(configPath); @@ -50,7 +55,7 @@ export async function run(argv, args, forkOptions = {}) { return await platform.run(destination, args, forkOptions); } catch (e) { - error(e); + (argv.debug ? trace : error)(e); } }