-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
290 lines (258 loc) · 8.85 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env node
import chalk from "chalk";
import ora from "ora";
import inquirer from "inquirer";
import { Listr } from "listr2";
import { readFile, writeFile, rm } from "node:fs/promises";
import { existsSync, mkdir } from "node:fs";
import { platform } from "node:os";
import { promisify } from "node:util";
import { exec } from "node:child_process";
import { setTimeout } from "node:timers/promises";
const asyncExec = promisify(exec);
// fix: inquirer list type not working in windows
const listType = platform() === "win32" ? "rawlist" : "list";
async function confirmRemoveDir(projectName: string) {
return new Promise<boolean>((resolve, reject) => {
inquirer
.prompt({
type: "confirm",
name: "exists",
message: `Project ${chalk.cyan(projectName)} is already exists. Do you want to remove it? (hit enter for NO)?`,
default: false
})
.then((answer) => answer.exists && resolve(true))
.catch((err) => reject(new Error(err)));
});
}
async function welcomeMessage() {
console.log(
`${chalk.yellowBright(`
${chalk.magenta("W E L C O M E T O")}
.d88b. .d8888b .d8888b 888d888 8888b.
d8P Y8b 88K d88P" 888P" "88b
88888888 "Y8888b. 888 888 .d888888
Y8b. X88 Y88b. 888 888 888
"Y8888 88888P' "Y8888P 888 "Y888888
`)}\nHello! ${chalk.bold(`esbuild create react app`)} is a minimal replacement of ${chalk.cyan(
`create react app`
)} using a truly blazing fast ${chalk.bold(
`esbuild bundler.`
)}\nUp and running in less than 1 minute with almost zero configuration needed.
`
);
}
async function templatechoice() {
return new Promise<string>((resolve, reject) => {
inquirer
.prompt({
type: listType,
name: "selected",
message: `To get started please choose a template`,
choices: ["Javascript", "Typescript"]
})
.then((answer) => resolve(answer.selected))
.catch((err) => reject(new Error(err)));
});
}
async function fetchTemplateRepo(projectName: string, template: string) {
const templates = {
js: `https://github.com/awran5/esbuild-react-app-js-template`,
ts: `https://github.com/awran5/esbuild-react-app-ts-template`
};
const templateChosen = template === "Typescript" ? templates.ts : templates.js;
try {
await asyncExec(`git clone ${templateChosen} .`, { cwd: projectName });
} catch (err) {
console.log(`Failed to clone repository. ${err}`);
process.exit(1);
}
}
async function isDependencies() {
return new Promise<boolean>((resolve, reject) => {
inquirer
.prompt({
type: listType,
name: "dependencies",
message: `Do you want to install dependencies?`,
choices: ["Yes", "No"]
})
.then((answer) => resolve(answer.dependencies === "Yes"))
.catch((err) => reject(new Error(err)));
});
}
async function installDependencies(projectName: string) {
try {
await asyncExec("npm i", { cwd: projectName });
} catch (err) {
console.log(`Failed to install dependencies. ${err}`);
process.exit(1);
}
}
async function updateFiles(projectName: string) {
// Get author name
let author = "your-name";
try {
author = (await asyncExec(`git config --global user.name`)).stdout.trim();
} catch (e) {
author = "your-name";
}
// Update license year and author
try {
const license = `${projectName}/LICENSE`;
let newLicense = await readFile(license, { encoding: "utf8" });
newLicense = newLicense.replace(/<author>/, author);
newLicense = newLicense.replace(/<year>/, `${new Date().getFullYear()}-present`);
// Write a new license file
await writeFile(license, newLicense, { encoding: "utf8" });
} catch (err) {
console.log(`Failed to update LICENSE. ${err}`);
process.exit(1);
}
// Update package.json
try {
const packageJson = `${projectName}/package.json`;
const read = await readFile(packageJson, { encoding: "utf8" });
const parse = JSON.parse(read);
const newPackageJson = {
...parse,
name: projectName,
author
};
// Write a new package.json
await writeFile(packageJson, JSON.stringify(newPackageJson, null, 2), "utf8");
} catch (err) {
console.log(`Failed to update package.json. ${err}`);
process.exit(1);
}
// Initialize a fresh Git
try {
await rm(`${projectName}/.git`, { recursive: true });
await asyncExec(`git init && git add . && git commit -m "initial commit"`, { cwd: projectName });
} catch (err) {
console.log(`Failed to initialize git. ${err}`);
process.exit(1);
}
}
async function successfulMessage(projectName: string, dependencies: boolean) {
const start = dependencies ? "npm run start" : "npm i && npm run start";
const installed = dependencies ? `ALREADY` : `NOT`;
console.log(
`${chalk.green(`
.d8888b 888 888 .d8888b .d8888b .d88b. .d8888b .d8888b
88K 888 888 d88P" d88P" d8P Y8b 88K 88K
"Y8888b. 888 888 888 888 88888888 "Y8888b. "Y8888b.
X88 Y88b 888 Y88b. Y88b. Y8b. X88 X88
88888P' "Y88888 "Y8888P "Y8888P "Y8888 88888P' 88888P'
`)}\nNow just navigate to your ${chalk.cyan(projectName)} project and type ${chalk.cyan(
start
)}\n\nPlease note that all dependencies are ${chalk.yellow(
installed
)} installed!\n\nFor issues, questions please contact me at https://github.com/awran5/esbuild-create-react-app/issues\n\n${chalk.yellow(
`Happy Coding!`
)}\n`
);
}
// Create a new project
(async () => {
const args = process.argv.slice(2, process.argv.length);
const projectName = args[0];
const isValidName = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
// Check if name is provided
if (!projectName) {
console.log(
`\n${chalk.bgRed(` Error! `)} Please specify a project name e.g. ${chalk.cyan(
`npx react-esbuild-app`
)} ${chalk.green("my-app")}\n`
);
process.exit();
}
// Check if name is valid following npm name pattern
if (!isValidName.test(projectName)) {
console.log(
`\n${chalk.bgRed(
` npm naming restrictions! `
)} Please specify a project name with lowercase characters separated by hyphen (-) e.g. ${chalk.green(
"my-app"
)}\n`
);
process.exit();
}
// Check if project exists
if (existsSync(projectName)) {
const spinner = ora();
const confirmRemove = await confirmRemoveDir(projectName);
if (confirmRemove) {
spinner.start(`Removing ${chalk.cyan(projectName)}, please wait`);
try {
await rm(projectName, { recursive: true });
spinner.succeed("Done");
} catch (err) {
spinner.fail("Failed!");
console.log(err);
process.exit(1);
}
}
}
await welcomeMessage();
// Get selected templates
const selectedTemplate = await templatechoice();
// Ask about installing dependencies
const confirmDependencies = await isDependencies();
const tasks = new Listr(
[
{
title: `[1/5] 📂 Creating a New Project in ${chalk.cyan(projectName)}`,
task: async (_, task): Promise<void> => {
mkdir(projectName, { recursive: true }, (err) => {
if (err) throw err;
});
await setTimeout(500);
task.title = "[1/5] 📂 done!";
}
},
{
title: `[2/5] 🚀 Fetching Repositories...`,
task: async (_, task): Promise<void> => {
await fetchTemplateRepo(projectName, selectedTemplate);
await setTimeout(500);
task.title = "[2/5] 🚀 done!";
}
},
{
title: "[3/5] 🔗 Installing Dependencies...",
task: async (_, task): Promise<void> => {
if (!confirmDependencies) task.skip("skipping");
else await installDependencies(projectName);
await setTimeout(500);
task.title = `[3/5] 🔗 ${confirmDependencies ? "done!" : "skipping.."}`;
}
},
{
title: "[4/5] 🎁 Updating files...",
task: async (_, task): Promise<void> => {
await updateFiles(projectName);
await setTimeout(500);
task.title = "[4/5] 🎁 done!";
}
},
{
title: "[5/5] ✨ All done...",
task: async (_, task): Promise<void> => {
await setTimeout(500);
task.title = "[5/5] ✨ done!";
}
}
],
{
exitOnError: true
}
);
try {
await tasks.run();
await successfulMessage(projectName, confirmDependencies);
} catch (error) {
console.log(error);
process.exit(1);
}
})();