File size: 2,587 Bytes
fdcac99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { execSync } from "node:child_process";
import type { PlopTypes } from "@turbo/gen";

interface PackageJson {
	name: string;
	scripts: Record<string, string>;
	dependencies: Record<string, string>;
	devDependencies: Record<string, string>;
}

export default function generator(plop: PlopTypes.NodePlopAPI): void {
	plop.setGenerator("init", {
		description: "Generate a new package for the Acme Monorepo",
		prompts: [
			{
				type: "input",
				name: "name",
				message:
					"What is the name of the package? (You can skip the `@decode/` prefix)",
			},
			{
				type: "input",
				name: "deps",
				message:
					"Enter a space separated list of dependencies you would like to install",
			},
		],
		actions: [
			(answers) => {
				if ("name" in answers && typeof answers.name === "string") {
					if (answers.name.startsWith("@decode/")) {
						answers.name = answers.name.replace("@decode/", "");
					}
				}
				return "Config sanitized";
			},
			{
				type: "add",
				path: "packages/{{ name }}/package.json",
				templateFile: "templates/package.json.hbs",
			},
			{
				type: "add",
				path: "packages/{{ name }}/tsconfig.json",
				templateFile: "templates/tsconfig.json.hbs",
			},
			{
				type: "add",
				path: "packages/{{ name }}/index.ts",
				template: "export * from './src';",
			},
			{
				type: "add",
				path: "packages/{{ name }}/src/index.ts",
				template: "export const name = '{{ name }}';",
			},
			{
				type: "modify",
				path: "packages/{{ name }}/package.json",
				async transform(content, answers) {
					if ("deps" in answers && typeof answers.deps === "string") {
						const pkg = JSON.parse(content) as PackageJson;
						for (const dep of answers.deps.split(" ").filter(Boolean)) {
							const version = await fetch(
								`https://registry.npmjs.org/-/package/${dep}/dist-tags`,
							)
								.then((res) => res.json())
								.then((json) => json.latest);
							if (!pkg.dependencies) pkg.dependencies = {};
							pkg.dependencies[dep] = `^${version}`;
						}
						return JSON.stringify(pkg, null, 2);
					}
					return content;
				},
			},
			async (answers) => {
				/**
				 * Install deps and format everything
				 */
				if ("name" in answers && typeof answers.name === "string") {
					// execSync("pnpm dlx sherif@latest --fix", {
					//   stdio: "inherit",
					// });
					execSync("pnpm i", { stdio: "inherit" });
					execSync(
						`pnpm @biomejs/biome format --write packages/${answers.name}/**`,
					);
					return "Package formated";
				}
				return "Package not formated";
			},
		],
	});
}