@bomb.sh/tools
Version:
The internal dev, build, and lint CLI for Bombshell projects
34 lines (33 loc) • 1.22 kB
JavaScript
import { cwd } from "node:process";
import { pathToFileURL } from "node:url";
import { readFile, writeFile } from "node:fs/promises";
import { x } from "tinyexec";
//#region src/commands/init.ts
async function init(ctx) {
const [_name = "."] = ctx.args;
const cwdUrl = pathToFileURL(`${cwd()}/`);
const inPlace = _name === ".";
const name = inPlace ? decodeURIComponent(cwdUrl.pathname.split("/").filter(Boolean).pop() ?? ".") : _name;
const target = inPlace ? "." : name;
const dest = inPlace ? cwdUrl : new URL(`./${name}/`, cwdUrl);
const gigetArgs = [
"giget@latest",
"gh:bombshell-dev/template",
target
];
if (inPlace) gigetArgs.push("--force");
for await (const line of x("pnpx", gigetArgs)) console.info(line);
const promises = [];
for (const file of ["package.json", "README.md"]) promises.push(postprocess(new URL(file, dest), (contents) => {
return contents.replaceAll("$name", name);
}));
await Promise.all(promises);
}
async function postprocess(file, transform) {
const result = await transform(await readFile(file, { encoding: "utf8" }));
if (!result) return;
await writeFile(file, result, { encoding: "utf8" });
}
//#endregion
export { init };
//# sourceMappingURL=init.mjs.map