-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·201 lines (177 loc) · 4.79 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
#!/usr/bin/env node
// modified from https://github.com/vitejs/vite/tree/main/packages/create-app
// @ts-check
const fs = require("fs");
const path = require("path");
const argv = require("minimist")(process.argv.slice(2));
const { prompt } = require("enquirer");
const {
yellow,
green,
cyan,
magenta,
lightRed,
red,
stripColors,
blue,
} = require("kolorist");
const cwd = process.cwd();
const TEMPLATES = [
yellow("cesium"),
yellow("cesium-ts"),
green("cesium-vue"),
green("cesium-vue-ts"),
cyan("cesium-react"),
cyan("cesium-react-ts"),
];
const renameFiles = {
_gitignore: ".gitignore",
};
async function init() {
let targetDir = argv._[0];
if (!targetDir) {
/**
* @type {{ projectName: string }}
*/
const { projectName } = await prompt({
type: "input",
name: "projectName",
message: `Project name:`,
initial: "cesium-project",
});
targetDir = projectName;
}
const packageName = await getValidPackageName(targetDir);
const root = path.join(cwd, targetDir);
console.log(`\nScaffolding project in ${root}...`);
if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true });
} else {
const existing = fs.readdirSync(root);
if (existing.length) {
/**
* @type {{ yes: boolean }}
*/
const { yes } = await prompt({
type: "confirm",
name: "yes",
initial: "Y",
message:
`Target directory ${targetDir} is not empty.\n` +
`Remove existing files and continue?`,
});
if (yes) {
emptyDir(root);
} else {
return;
}
}
}
// determine template
let template = argv.t || argv.template;
let message = "Select a template:";
let isValidTemplate = false;
// --template expects a value
if (typeof template === "string") {
const availableTemplates = TEMPLATES.map(stripColors);
isValidTemplate = availableTemplates.includes(template);
message = `${template} isn't a valid template. Please choose from below:`;
}
if (!template || !isValidTemplate) {
/**
* @type {{ t: string }}
*/
const { t } = await prompt({
type: "select",
name: "t",
message,
choices: TEMPLATES,
});
template = stripColors(t);
}
const templateDir = path.join(__dirname, `template-${template}`);
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 = require(path.join(templateDir, `package.json`));
pkg.name = packageName;
write("package.json", JSON.stringify(pkg, null, 2));
const pkgManager = /yarn/.test(process.env.npm_execpath) ? "yarn" : "npm";
console.log(green(`\nDone. Now run:\n`));
if (root !== cwd) {
console.log(` cd ${path.relative(cwd, root)}`);
}
console.log(` ${pkgManager === "yarn" ? `yarn` : `npm install`}`);
console.log(` ${pkgManager === "yarn" ? `yarn dev` : `npm run dev`}`);
console.log();
}
function copy(src, dest) {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
copyDir(src, dest);
} else {
fs.copyFileSync(src, dest);
}
}
async function getValidPackageName(projectName) {
const packageNameRegExp = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
if (packageNameRegExp.test(projectName)) {
return projectName;
} else {
const suggestedPackageName = projectName
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/^[._]/, "")
.replace(/[^a-z0-9-~]+/g, "-");
/**
* @type {{ inputPackageName: string }}
*/
const { inputPackageName } = await prompt({
type: "input",
name: "inputPackageName",
message: `Package name:`,
initial: suggestedPackageName,
validate: (input) =>
packageNameRegExp.test(input) ? true : "Invalid package.json name",
});
return inputPackageName;
}
}
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);
}
}
function emptyDir(dir) {
if (!fs.existsSync(dir)) {
return;
}
for (const file of fs.readdirSync(dir)) {
const abs = path.resolve(dir, file);
// baseline is Node 12 so can't use rmSync :(
if (fs.lstatSync(abs).isDirectory()) {
emptyDir(abs);
fs.rmdirSync(abs);
} else {
fs.unlinkSync(abs);
}
}
}
init().catch((e) => {
console.error(e);
});