Skip to content

Commit bf5de7f

Browse files
committed
feat: use consola for output formatting
1 parent ab0401b commit bf5de7f

File tree

10 files changed

+42
-50
lines changed

10 files changed

+42
-50
lines changed

LICENSE

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,3 @@ The above copyright notice and this permission notice shall be included in all c
3434

3535
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3636

37-
---
38-
39-
Colorette dependency is bundled (https://github.com/jorgebucaran/colorette)
40-
41-
Copyright © Jorge Bucaran <https://jorgebucaran.com>
42-
43-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44-
45-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46-
47-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const main = defineCommand({
6565
},
6666
},
6767
run({ args }) {
68-
console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
68+
consol.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
6969
},
7070
});
7171

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
"release": "pnpm test && changelogen --release --push && npm publish",
3030
"test": "pnpm lint && vitest run --coverage"
3131
},
32+
"dependencies": {
33+
"consola": "^3.2.3"
34+
},
3235
"devDependencies": {
3336
"@types/node": "^20.4.0",
3437
"@vitest/coverage-v8": "^0.32.4",
3538
"changelogen": "^0.5.4",
36-
"colorette": "^2.0.20",
3739
"eslint": "^8.44.0",
3840
"eslint-config-unjs": "^0.2.1",
3941
"jiti": "^1.19.1",
@@ -43,5 +45,5 @@
4345
"unbuild": "^1.2.1",
4446
"vitest": "^0.32.4"
4547
},
46-
"packageManager": "pnpm@8.6.5"
48+
"packageManager": "pnpm@8.6.6"
4749
}

playground/commands/build.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import consola from 'consola'
12
import { defineCommand } from "../../src";
23

34
export default defineCommand({
@@ -37,7 +38,7 @@ export default defineCommand({
3738
},
3839
},
3940
run({ args }) {
40-
console.log("Build");
41-
console.log("Parsed args:", args);
41+
consola.log("Build");
42+
consola.log("Parsed args:", args);
4243
},
4344
});

playground/commands/deploy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import consola from 'consola'
12
import { defineCommand } from "../../src";
23

34
export default defineCommand({
@@ -53,7 +54,7 @@ export default defineCommand({
5354
},
5455
},
5556
run({ args }) {
56-
console.log("Build");
57-
console.log("Parsed args:", args);
57+
consola.log("Build");
58+
consola.log("Parsed args:", args);
5859
},
5960
});

playground/hello.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import consola from 'consola'
12
import { defineCommand, runMain } from "../src";
23

34
const main = defineCommand({
@@ -18,7 +19,7 @@ const main = defineCommand({
1819
},
1920
},
2021
run({ args }) {
21-
console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
22+
consola.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
2223
},
2324
});
2425

pnpm-lock.yaml

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/command.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
3939
);
4040
const subCommandName = opts.rawArgs[subCommandArgIndex];
4141
if (!subCommandName && !cmd.run) {
42-
throw new CLIError(
43-
`Missing sub command. Use --help to see available sub commands.`,
44-
"ESUBCOMMAND",
45-
);
42+
throw new CLIError(`No command specified.`, "E_NO_COMMAND");
4643
}
4744
if (!subCommands[subCommandName]) {
4845
throw new CLIError(
49-
`Unknown sub command: ${subCommandName}`,
50-
"ESUBCOMMAND",
46+
`Unknown command \`${subCommandName}\``,
47+
"E_UNKOWN_COMMAND",
5148
);
5249
}
5350
const subCommand = await resolveValue(subCommands[subCommandName]);

src/main.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bgRed } from "colorette";
1+
import consola from "consola";
22
import type { ArgsDef, CommandDef } from "./types";
33
import { resolveSubCommand, runCommand } from "./command";
44
import { CLIError } from "./_utils";
@@ -23,14 +23,12 @@ export async function runMain<T extends ArgsDef = ArgsDef>(
2323
} catch (error: any) {
2424
const isCLIError = error instanceof CLIError;
2525
if (!isCLIError) {
26-
console.error(error, "\n");
26+
consola.error(error, "\n");
2727
}
28-
console.error(
29-
`\n${bgRed(` ${error.code || error.name} `)} ${error.message}\n`,
30-
);
3128
if (isCLIError) {
3229
await showUsage(...(await resolveSubCommand(cmd, rawArgs)));
3330
}
31+
consola.error(error.message);
3432
process.exit(1);
3533
}
3634
}

src/usage.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import consola from "consola";
2+
import { colors } from 'consola/utils'
13
import { formatLineColumns, resolveValue } from "./_utils";
24
import type { ArgsDef, CommandDef } from "./types";
35
import { resolveArgs } from "./args";
@@ -7,9 +9,9 @@ export async function showUsage<T extends ArgsDef = ArgsDef>(
79
parent?: CommandDef<T>,
810
) {
911
try {
10-
console.log((await renderUsage(cmd, parent)) + "\n");
12+
consola.log((await renderUsage(cmd, parent)) + "\n");
1113
} catch (error) {
12-
console.error(error);
14+
consola.error(error);
1315
}
1416
}
1517

@@ -36,7 +38,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
3638
const isRequired = arg.required !== false && arg.default === undefined;
3739
// (isRequired ? " (required)" : " (optional)"
3840
const usageHint = arg.default ? `="${arg.default}"` : "";
39-
posLines.push([name + usageHint, arg.description || ""]);
41+
posLines.push(["`" + name + usageHint + "`", arg.description || ""]);
4042
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
4143
} else {
4244
const isRequired = arg.required === true && arg.default === undefined;
@@ -55,7 +57,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
5557
}`
5658
: "");
5759
argLines.push([
58-
argStr + (isRequired ? " (required)" : ""),
60+
"`" + argStr + (isRequired ? " (required)" : "") + "`",
5961
arg.description || "",
6062
]);
6163
if (isRequired) {
@@ -70,7 +72,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
7072
for (const [name, sub] of Object.entries(subCommands)) {
7173
const subCmd = await resolveValue(sub);
7274
const meta = await resolveValue(subCmd?.meta);
73-
commandsLines.push([name, meta?.description || ""]);
75+
commandsLines.push([`\`${name}\``, meta?.description || ""]);
7476
commandNames.push(name);
7577
}
7678
usageLine.push(commandNames.join("|"));
@@ -79,34 +81,34 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
7981
const usageLines: (string | undefined)[] = [];
8082

8183
const version = cmdMeta.version || parentMeta.version;
84+
8285
usageLines.push(
83-
commandName + (version ? ` v${version}` : ""),
84-
cmdMeta.description,
86+
colors.gray(`${cmdMeta.description} (${commandName + (version ? ` v${version}` : "")})`),
8587
"",
8688
);
8789

8890
const hasOptions = argLines.length > 0 || posLines.length > 0;
8991
usageLines.push(
90-
`USAGE: ${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(
92+
`${colors.underline(colors.bold('USAGE'))} \`${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(
9193
" ",
92-
)}`,
94+
)}\``,
9395
"",
9496
);
9597

9698
if (posLines.length > 0) {
97-
usageLines.push("ARGUMENTS:", "");
99+
usageLines.push(colors.underline(colors.bold("ARGUMENTS")), "");
98100
usageLines.push(formatLineColumns(posLines, " "));
99101
usageLines.push("");
100102
}
101103

102104
if (argLines.length > 0) {
103-
usageLines.push("OPTIONS:", "");
105+
usageLines.push(colors.underline(colors.bold("OPTIONS")), "");
104106
usageLines.push(formatLineColumns(argLines, " "));
105107
usageLines.push("");
106108
}
107109

108110
if (commandsLines.length > 0) {
109-
usageLines.push("COMMANDS:", "");
111+
usageLines.push(colors.underline(colors.bold("COMMANDS")), "");
110112
usageLines.push(formatLineColumns(commandsLines, " "));
111113
usageLines.push(
112114
"",

0 commit comments

Comments
 (0)