From 894a5c6630fd141ee54b47b17c31a19035c0cbd8 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:13:41 +0200 Subject: [PATCH] refactor(new-package): replace `yargs` with `util.parseArgs` --- scripts/new-package.js | 56 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/scripts/new-package.js b/scripts/new-package.js index 78c45ff38..548d1e3a9 100644 --- a/scripts/new-package.js +++ b/scripts/new-package.js @@ -1,10 +1,11 @@ +// @ts-check // a script that takes a string as value and copies the folder packages/template // to a new folder with the name of the value import * as fs from "node:fs/promises"; import * as path from "node:path"; import { URL, fileURLToPath } from "node:url"; -import yargs from "yargs"; +import { parseArgs } from "node:util"; const EXPERIMENTAL_BANNER = "šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§\n\n### THIS TOOL IS EXPERIMENTAL ā€” USE WITH CAUTION\n\nšŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§šŸš§"; @@ -12,16 +13,11 @@ const USAGE_TOKEN_START = ""; const USAGE_TOKEN_END = ""; const WARNING_BANNER_TOKEN = ""; -async function main(argv) { - if (!fs.cp) { - console.error("Please use Node 16.13 or higher"); - return 1; - } - - // this is less than ideal, but the only way to make positional working with v16 of yargs - const projectName = argv["_"][0]; - const experimental = argv.experimental; - +/** + * @param {string} projectName + * @param {{ experimental?: boolean; }} options + */ +async function main(projectName, { experimental }) { // do some quick sanitization const cleanProjectName = projectName.replace(/[|&;$%@"<>()+,]/g, ""); @@ -101,15 +97,29 @@ async function main(argv) { return 0; } -const argv = yargs(process.argv.slice(2)) - .usage("Usage: new-package --experimental") - .example( - "new-package my-package --experimental", - "Create a new package named my-package" - ) - .demandCommand(1) - .string("name") - .boolean("experimental") - .default("experimental", false).argv; - -main(argv).then((exitCode) => (process.exitCode = exitCode)); +if (!fs.cp) { + console.error("Please use Node 16.13 or higher"); + process.exitCode = 1; +} else { + const { values, positionals } = parseArgs({ + args: process.argv.slice(2), + options: { + experimental: { + description: "Whether the package is experimental", + type: "boolean", + default: false, + }, + }, + strict: true, + allowPositionals: true, + tokens: false, + }); + if (positionals.length === 0) { + console.log("Usage: new-package [--experimental] "); + process.exitCode = 1; + } else { + main(positionals[0], values).then( + (exitCode) => (process.exitCode = exitCode) + ); + } +}