refactor: use parseArgs from node:util to do basic parsing
This commit is contained in:
parent
c4c41e0e31
commit
f1a4a6d18c
2 changed files with 40 additions and 16 deletions
|
|
@ -39,7 +39,6 @@ function validateHttpUrlOrHost(
|
||||||
try {
|
try {
|
||||||
const normalized = normalizeHttpUrl(value);
|
const normalized = normalizeHttpUrl(value);
|
||||||
const url = new URL(normalized);
|
const url = new URL(normalized);
|
||||||
|
|
||||||
if (!url.hostname) {
|
if (!url.hostname) {
|
||||||
return `${label} must include a domain or hostname`;
|
return `${label} must include a domain or hostname`;
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +124,7 @@ async function preflightWriteTargets(filePaths: string[]): Promise<void> {
|
||||||
* @param _args - The command arguments.
|
* @param _args - The command arguments.
|
||||||
* @returns {Promise<void>} A promise that resolves when the initialization is complete.
|
* @returns {Promise<void>} A promise that resolves when the initialization is complete.
|
||||||
*/
|
*/
|
||||||
export async function initCommand(_args: string[]): Promise<void> {
|
export async function initCommand(args?: string[]): Promise<void> {
|
||||||
p.intro(c.bold("myst init"));
|
p.intro(c.bold("myst init"));
|
||||||
p.note(
|
p.note(
|
||||||
[
|
[
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
import { parseArgs } from "node:util";
|
||||||
|
|
||||||
import * as c from "yoctocolors";
|
import * as c from "yoctocolors";
|
||||||
import pkg from "../package.json" with { type: "json" };
|
import pkg from "../package.json" with { type: "json" };
|
||||||
|
|
||||||
|
|
@ -7,40 +9,63 @@ import { getHelpText } from "./help.js";
|
||||||
|
|
||||||
type CommandName = "init";
|
type CommandName = "init";
|
||||||
|
|
||||||
type CommandHandler = (args: string[]) => Promise<void>;
|
type CommandHandler = (args?: string[]) => Promise<void>;
|
||||||
|
|
||||||
const commandHandlers: Record<CommandName, CommandHandler> = {
|
const commandHandlers: Record<CommandName, CommandHandler> = {
|
||||||
init: initCommand,
|
init: initCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
function isKnownCommand(value: string): value is CommandName {
|
|
||||||
return value in commandHandlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
async function main(): Promise<void> {
|
||||||
const [command, ...options] = process.argv.slice(2);
|
const { values, positionals } = parseArgs({
|
||||||
const flags = options.filter((o) => o.startsWith("-"));
|
args: process.argv.slice(2),
|
||||||
|
options: {
|
||||||
|
version: {
|
||||||
|
type: "boolean",
|
||||||
|
short: "v",
|
||||||
|
},
|
||||||
|
help: {
|
||||||
|
type: "boolean",
|
||||||
|
short: "h",
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
type: "string",
|
||||||
|
short: "o",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
allowPositionals: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (flags.includes("-v") || flags.includes("--version")) {
|
const command = positionals[0];
|
||||||
process.stdout.write(`${pkg.version}\n`);
|
|
||||||
process.exitCode = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!command || flags.includes("-h") || flags.includes("--help")) {
|
if (!command) {
|
||||||
process.stdout.write(getHelpText());
|
process.stdout.write(getHelpText());
|
||||||
process.exitCode = 0;
|
process.exitCode = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isKnownCommand = (cmd: string): cmd is CommandName =>
|
||||||
|
cmd in commandHandlers;
|
||||||
|
|
||||||
if (!isKnownCommand(command)) {
|
if (!isKnownCommand(command)) {
|
||||||
process.stderr.write(`Unknown command: ${command}\n`);
|
process.stderr.write(`Unknown command: ${command}\n`);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (values.help) {
|
||||||
|
process.stdout.write(getHelpText());
|
||||||
|
process.exitCode = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.version) {
|
||||||
|
process.stdout.write(`${pkg.version}`);
|
||||||
|
process.exitCode = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await commandHandlers[command](options);
|
await commandHandlers[command]();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : "Unknown error";
|
const message = error instanceof Error ? error.message : "Unknown error";
|
||||||
process.stderr.write(`${c.red("Error:")} ${message}\n`);
|
process.stderr.write(`${c.red("Error:")} ${message}\n`);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue