-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
50 lines (42 loc) · 1.36 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Command } from "commander";
import fs from "fs/promises";
import path from "path";
import packageJson from "./package.json";
export interface RenderTemplateOpts {
name: string;
}
export async function renderTemplate(options: RenderTemplateOpts) {
const ignores = [".git", "node_modules"];
const replacements: { [key: string]: string } = {
"airfocus-automation": options.name,
};
const sourceNames = await fs.readdir(__dirname, { recursive: true });
const files = sourceNames
.filter(
(sourceName) => !ignores.some((ignore) => sourceName.startsWith(ignore)),
)
.map((sourceName) => path.join(__dirname, sourceName));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stat = await fs.stat(file);
if (stat.isFile()) {
const template = await fs.readFile(file, "utf-8");
const rendered = Object.keys(replacements).reduce(
(str, key) => str.replace(new RegExp(key, "g"), replacements[key]),
template,
);
await fs.writeFile(file, rendered, "utf-8");
}
}
}
export function run() {
const program = new Command(packageJson.name)
.version(packageJson.version)
.requiredOption("-n, --name <string>")
.action((opts: RenderTemplateOpts) => renderTemplate(opts));
return program.parseAsync();
}
run().catch((err) => {
console.error(err);
process.exit(1);
});