From b9bbdb5c8006c660d4b02b535156d8929e077682 Mon Sep 17 00:00:00 2001 From: CRIMX Date: Mon, 14 Oct 2024 22:12:23 +0800 Subject: [PATCH] chore(setup): add args (#12) --- README.md | 8 ++++++++ scripts/setup.mjs | 49 +++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8c02f5a..8f99849 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,14 @@ After you have cloned the repository generated by this template, run the followi node scripts/setup.mjs ``` +It will update the template with info inferred from git or directory name, and install dependencies. + +You can also set the info manually by running: + +```console +node scripts/setup.mjs --name "package-name" --description "package description" --repo "https://github.com/wopjs/template.git" +``` + ## License MIT @ [wopjs](https://github.com/wopjs) diff --git a/scripts/setup.mjs b/scripts/setup.mjs index 42efae7..9a57aeb 100644 --- a/scripts/setup.mjs +++ b/scripts/setup.mjs @@ -2,32 +2,35 @@ import cp from "node:child_process"; import fs from "node:fs"; import path from "node:path"; -let user = ( - exec("basename $(dirname $(git rev-parse --show-toplevel))") || "wopjs" -).toLowerCase(); +const args = process.argv.slice(2); + +let repo = (argv("repo") || exec("git remote get-url origin")) + .trim() + .toLowerCase(); + +let user = (path.basename(path.dirname(repo)) || "wopjs").toLowerCase(); -let name = ( - exec("basename $(git rev-parse --show-toplevel)") || - path.basename(process.cwd()) +let repoName = ( + path.basename(repo, ".git") || path.basename(process.cwd()) ).toLowerCase(); -let docsURL = `https://${user.toLowerCase()}.github.io/${name}`; -let userLink = `[${user}](https://github.com/${user.toLowerCase()})`; +let pkgName = argv("name") || `@${user}/${repoName}`; +let docsURL = `https://${user}.github.io/${repoName}`; +let userLink = `[${user}](https://github.com/${user})`; let pkg = JSON.parse(fs.readFileSync("package.json", "utf8")); -pkg.name = `@${user.toLowerCase()}/${name}`; -pkg.description = name; -pkg.keywords = name.split("-"); -pkg.repository = `${user.toLowerCase()}/${name}`; +pkg.name = pkgName; +pkg.description = argv("description") || repoName; +pkg.keywords = []; +pkg.repository = repo || `${user}/${repoName}`; if (user !== "wopjs") { pkg.maintainers = void 0; } -pkg.scripts.postinstall = void 0; let readme = fs.readFileSync("README.template.md", "utf8"); -readme = readme.replace(/wopjs\/template/g, `${user}/${name}`); +readme = readme.replace(/wopjs\/template/g, `${user}/${repoName}`); readme = readme.replace("https://wopjs.github.io/template", docsURL); -readme = readme.replace("Collection of common utilities.", `${name}.`); +readme = readme.replace("Collection of common utilities.", `${repoName}.`); readme = readme.replace("[wopjs](https://github.com/wopjs)", userLink); fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2) + "\n"); @@ -42,6 +45,20 @@ function exec(command) { try { return String(cp.execSync(command)); } catch { - return null; + return ""; } } + +function argv(key) { + let index = args.indexOf(`--${key}`); + if (index > -1) { + return args[index + 1] || ""; + } + + const matchedArg = args.find(arg => arg.startsWith(`--${key}=`)); + if (matchedArg) { + return matchedArg.split("=")[1] || ""; + } + + return ""; +}