Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: add --debug option #235

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions packages/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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}"`);

Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
}
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/commands/deps_commands/add.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/cli/commands/deps_commands/get.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/cli/commands/deps_commands/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]"];
Expand All @@ -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 {
Expand All @@ -30,7 +34,7 @@ export async function handler(argv) {
.join("\n");
console.log(formattedDeps);
} catch (e) {
error(e);
(argv.debug ? trace : error)(e);
}
}

Expand Down
48 changes: 28 additions & 20 deletions packages/cli/commands/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions packages/cli/commands/highlight.js
Original file line number Diff line number Diff line change
@@ -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 <source>";
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);
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/cli/commands/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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) {
Expand All @@ -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);
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions packages/cli/commands/new.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Loading