-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
272 lines (241 loc) · 6.68 KB
/
index.js
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
#!/usr/bin/env node
// @ts-nocheck
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import minimist from "minimist";
import prompts from "prompts";
import {
blue,
cyan,
green,
lightRed,
magenta,
red,
reset,
yellow,
} from "kolorist";
const argv = minimist(process.argv.slice(2), { string: ["_"] });
const cwd = process.cwd();
const FRAMEWORKS = [
{
name: "mern",
color: magenta,
},
];
const TEMPLATES = FRAMEWORKS.map(
(f) => (f.variants && f.variants.map((v) => v.name)) || [f.name]
).reduce((a, b) => a.concat(b), []);
const renameFiles = {
_gitignore: ".gitignore",
};
const promptsConfig = [
{
type: "text",
name: "projectName",
message: reset("Enter the name of your project:"),
initial: "panda-template",
},
{
type: "text",
name: "packageName",
message: reset("Package name:"),
initial: "panda-magic",
validate: (dir) =>
isValidPackageName(dir) || "Invalid package name",
},
{
type: (_, { template }) =>
TEMPLATES.includes(template) ? null : "select",
name: "framework",
message: reset("Select a framework:"),
initial: 0,
choices: FRAMEWORKS.map((framework) => {
const frameworkColor = framework.color;
return {
title: frameworkColor(framework.name),
value: framework,
};
}),
},
{
type: (_, { framework }) =>
framework && framework.variants ? "select" : null,
name: "variant",
message: reset("Select a variant:"),
choices: (framework) =>
framework.variants.map((variant) => {
const variantColor = variant.color;
return {
title: variantColor(variant.name),
value: variant.name,
};
}),
},
{
type: "text",
name: "description",
message: reset("Description:"),
},
{
type: "text",
name: "author",
message: reset("Author:"),
},
];
async function init() {
const result = await prompts(promptsConfig, {
onCancel: () => {
throw new Error(red("✖") + " Operation cancelled");
},
});
const { framework, overwrite, packageName, variant, description, author } = result;
const targetDir = formatTargetDir(result.projectName);
const root = path.join(cwd, targetDir);
if (overwrite) {
emptyDir(root);
} else if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true });
}
const template = variant || framework || template || argv.t;
console.log(`\nCreating a new project in directory: ${cyan(root)}...`);
const templateDir = path.resolve(
fileURLToPath(import.meta.url),
"..",
`template-backend`
);
const write = (file, content) => {
const targetPath = renameFiles[file]
? path.join(root, renameFiles[file])
: path.join(root, file);
if (content) {
fs.writeFileSync(targetPath, content);
} else {
copy(path.join(templateDir, file), targetPath);
}
};
const files = fs.readdirSync(templateDir);
for (const file of files.filter((f) => f !== "package.json")) {
write(file);
}
const pkg = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), "utf-8")
);
pkg.name = packageName || targetDir;
pkg.description = description; // Set the description
pkg.author = author; // Set the author
write("package.json", JSON.stringify(pkg, null, 2));
const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent);
const pkgManager = pkgInfo ? pkgInfo.name : "npm";
console.log(lightRed(`\nDone. Now run the following commands:\n`));
if (root !== cwd) {
console.log(` ${green("cd")} ${path.relative(cwd, root)}\n`);
}
switch (pkgManager) {
case "yarn":
console.log(green(" yarn"));
console.log(" Install the dependencies 📔\n");
console.log(green(" yarn prisma"));
console.log(" Run Prisma migrations to set up your database schema 🏗️\n");
console.log(green(" yarn dev"));
console.log(" Start the development server 🛠️\n");
console.log(green(" yarn build"));
console.log(" Bundle the app for production 📦\n");
console.log(green(" yarn start"));
console.log(" Launch your application in production mode 🚀\n");
break;
default:
console.log(green(` ${pkgManager} install`));
console.log(" Install the dependencies 📔\n");
console.log(green(` ${pkgManager} run dev`));
console.log(" Start the development server 🛠️ \n");
console.log(green(` ${pkgManager} prisma`));
console.log(" Run Prisma migrations to set up your database schema 🏗️\n");
console.log(green(` ${pkgManager} run build`));
console.log(" Bundles the app for production 📦\n");
console.log(green(` ${pkgManager} start`));
console.log(" Launch your application in production mode 🚀\n");
console.log(cyan(" Happy Panda Coding 🐼⚡️"));
break;
}
console.log();
}
/**
* @param {string | undefined} targetDir
*/
function formatTargetDir(targetDir) {
return targetDir?.trim().replace(/\/+$/g, "");
}
function copy(src, dest) {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
copyDir(src, dest);
} else {
fs.copyFileSync(src, dest);
}
}
/**
* @param {string} projectName
*/
function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
projectName
);
}
/**
* @param {string} projectName
*/
function toValidPackageName(projectName) {
return projectName
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/^[._]/, "")
.replace(/[^a-z0-9-~]+/g, "-");
}
/**
* @param {string} srcDir
* @param {string} destDir
*/
function copyDir(srcDir, destDir) {
fs.mkdirSync(destDir, { recursive: true });
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file);
const destFile = path.resolve(destDir, file);
copy(srcFile, destFile);
}
}
/**
* @param {string} path
*/
function isEmpty(path) {
const files = fs.readdirSync(path);
return files.length === 0 || (files.length === 1 && files[0] === ".git");
}
/**
* @param {string} dir
*/
function emptyDir(dir) {
if (!fs.existsSync(dir)) {
return;
}
for (const file of fs.readdirSync(dir)) {
fs.rmSync(path.resolve(dir, file), { recursive: true, force: true });
}
}
/**
* @param {string | undefined} userAgent process.env.npm_config_user_agent
* @returns object | undefined
*/
function pkgFromUserAgent(userAgent) {
if (!userAgent) return undefined;
const pkgSpec = userAgent.split(" ")[0];
const pkgSpecArr = pkgSpec.split("/");
return {
name: pkgSpecArr[0],
version: pkgSpecArr[1],
};
}
init().catch((e) => {
console.error(e);
});