Skip to content

Commit

Permalink
chore(setup): add args (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx authored Oct 14, 2024
1 parent b127215 commit b9bbdb5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 33 additions & 16 deletions scripts/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 "";
}

0 comments on commit b9bbdb5

Please sign in to comment.