-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
205 lines (182 loc) · 4.67 KB
/
index.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env node
import path from "node:path";
import {fileURLToPath} from "node:url";
import {cp, readFile, writeFile} from "node:fs/promises";
import {glob} from "glob";
import color from "picocolors";
import prompts from "prompts";
import yargs from "yargs";
import {hideBin} from "yargs/helpers";
// List of templates
const TEMPLATES = [
{
title: "Next.js + ESLint + TypeScript + Shadcn/ui",
value: "next-eslint-ts-shadcn",
},
{
title: "Next.js + ESLint + TypeScript + Tailwind",
value: "next-eslint-ts-tw",
},
{
title: "React (vite) + ESLint + TypeScript + Tailwind",
value: "react-eslint-ts-tw",
},
];
// List of extras
const EXTRAS = {
"next-eslint-ts-shadcn": [
{
title: "Mercado Pago",
value: "mercadopago",
},
{
title: "Kinde Auth",
value: "kinde",
},
{
title: "Clerk Auth",
value: "clerk",
},
{
title: "Auth0",
value: "auth0",
},
{
title: "Supabase",
value: "supabase",
},
{
title: "libSQL + Drizzle",
value: "libsql",
},
],
"next-eslint-ts-tw": [
{
title: "Mercado Pago",
value: "mercadopago",
},
{
title: "Kinde Auth",
value: "kinde",
},
{
title: "Clerk Auth",
value: "clerk",
},
{
title: "Auth0",
value: "auth0",
},
{
title: "Supabase",
value: "supabase",
},
{
title: "libSQL + Drizzle",
value: "libsql",
},
],
};
// Specify CLI arguments
const args = yargs(hideBin(process.argv)).options({
name: {
alias: "n",
type: "string",
description: "Name of the project",
},
template: {
alias: "t",
type: "string",
description: "Template to use",
},
});
// Orverride arguments passed on the CLI
prompts.override(args.argv);
async function main() {
// Get the initial values for the prompts
const {
_: [initialName, initialProject],
} = await args.argv;
// Create the project prompt
const project = await prompts(
[
{
type: "text",
name: "name",
message: "What is the name of your project?",
initial: initialName || "appncy-project",
validate: (value) => {
if (value.match(/[^a-zA-Z0-9-_]+/g))
return "Project name can only contain letters, numbers, dashes and underscores";
return true;
},
},
{
type: "select",
name: "template",
message: `Which template would you like to use?`,
initial: initialProject || 0,
choices: TEMPLATES,
},
],
{
onCancel: () => {
console.log("\nBye 👋\n");
process.exit(0);
},
},
);
// Get the template folder for the selected template
const template = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"templates",
project.template,
);
// Get the destination folder for the project
const destination = path.join(process.cwd(), project.name);
// Get the extras for the selected template
let extras: string[] = [];
if (EXTRAS[project.template]) {
const {extras: results} = await prompts({
type: "multiselect",
name: "extras",
message: "Which extras would you like to add?",
choices: EXTRAS[project.template],
});
// Assign to variable
extras = results;
}
// Copy files from the template folder to the current directory
await cp(path.join(template, "project"), destination, {recursive: true});
for await (const extra of extras) {
// Copy files from the extra folder to the current directory
await cp(path.join(template, "extras", extra), destination, {recursive: true});
}
// Get all files from the destination folder
const files = await glob(`**/*`, {nodir: true, cwd: destination, absolute: true});
// Read each file and replace the tokens
for await (const file of files) {
const data = await readFile(file, "utf8");
const draft = data.replace(/{{name}}/g, project.name);
await writeFile(file, draft, "utf8");
}
// Log outro message
console.log("\n✨ Project created ✨");
console.log(`\n${color.yellow(`Next steps:`)}\n`);
console.log(`${color.green(`cd`)} ${project.name}`);
console.log(`${color.green(`pnpm`)} install`);
console.log(`${color.green(`pnpm`)} dev`);
// Extras log
if (extras.length) {
console.log(
`\nCheck out ${color.italic(
extras.map((extra) => `${extra.toUpperCase()}.md`).join(", "),
)} for more info on how to use it.`,
);
}
// Contact logs
console.log("\n---\n");
console.log(`Questions 👀? ${color.underline(color.cyan("https://x.com/goncy"))}`);
}
// Run the main function
main().catch(console.error);