-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (185 loc) · 5.95 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
#!/usr/bin/env node
import {
intro,
outro,
isCancel,
cancel,
text,
confirm,
spinner,
select,
} from "@clack/prompts";
import child_process from "child_process";
import fs from "fs-extra";
import path from "path";
import chalk from "chalk";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { promisify } from "util";
import figlet from "figlet";
import gradient from "gradient-string";
const exec = promisify(child_process.exec);
const figletPromise = (text) => {
return new Promise((resolve, reject) => {
figlet(text, (err, data) => {
if (err) {
reject(err);
} else {
console.log(gradient.pastel.multiline(data));
resolve();
}
});
});
};
async function main() {
try {
await figletPromise("create - default - app");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
intro(chalk.blueBright("create-default-app"));
let projectName = await text({
message: "What is the name of your project?",
placeholder: "my-project",
initialValue: "my-project",
validate(value) {
if (value.length === 0) return `project name is required!`;
if (value.match(" ") !== null) return `invalid project name!`;
const resolvedPath = path.resolve("./", value);
const normalizedPath = path.normalize(resolvedPath);
if (!normalizedPath.startsWith(path.resolve("./"))) {
return `invalid project name!`;
}
},
});
if (isCancel(projectName)) {
cancel("create-default-app scaffold cancelled.");
process.exit(0);
}
const styleFramework = await select({
message: "Which CSS framework will you use?",
options: [
{ value: "none", label: "none" },
{ value: "bootstrap", label: "bootstrap" },
{ value: "tailwindcss", label: "tailwindcss" },
],
});
if (isCancel(styleFramework)) {
cancel("create-default-app scaffold cancelled.");
process.exit(0);
}
const gitInit = await confirm({
message: "Do you want to initialize Git?",
initialValue: false,
});
async function handleScaffold() {
try {
const spin = spinner();
spin.start("Initializing project");
try {
const folder = await fs.readdir(path.join("./"));
if (projectName === ".") {
projectName = path.join("./");
} else if (folder.includes(projectName)) {
cancel("Directory already exists");
process.exit(0);
}
if (projectName !== path.join("./")) {
await fs.mkdir(path.join("./", projectName));
}
} catch (err) {
console.log(chalk.redBright(" An error occurred: ", err));
cancel("create-default-app scaffold cancelled.");
process.exit(0);
}
const files = await fs.readdir(path.join(__dirname, "files"));
const existingFiles = await fs.readdir(path.join("./", projectName));
const conflicts = files.filter((file) => existingFiles.includes(file));
if (conflicts.length > 0) {
console.error(
` The following files already exist in the directory: ${conflicts.join(
", "
)}`
);
const overwrite = await confirm({
message: "Do you want to overwrite the existing files?",
initialValue: false,
});
if (isCancel(overwrite) || !overwrite) {
cancel("create-default-app scaffold cancelled.");
process.exit(0);
}
}
if (styleFramework == "none") {
await fs.copy(
path.join(__dirname, "files", "default"),
path.join("./", projectName)
);
} else if (styleFramework == "bootstrap") {
await fs.copy(
path.join(__dirname, "files", "bootstrap"),
path.join("./", projectName)
);
} else if (styleFramework == "tailwindcss") {
await fs.copy(
path.join(__dirname, "files", "tailwindcss"),
path.join("./", projectName)
);
}
await fs.copy(
path.join(__dirname, "files", "app.js"),
path.join("./", projectName, "app.js")
);
await exec("npm init -y", {
cwd: path.join("./", projectName),
});
spin.message("Installing tailwindcss via npm");
if (styleFramework == "tailwindcss") {
await exec("npm install -D tailwindcss", {
cwd: path.join("./", projectName),
});
}
if (gitInit) {
spin.message("Initializing Git");
await exec("git init", {
cwd: path.join("./", projectName),
});
}
spin.stop("Initialization complete");
outro(chalk.blueBright(`Project scaffolded successfully!`));
console.log(
chalk.magentaBright(" use command"),
chalk.greenBright.bold(`cd ${projectName}\n`)
);
if (styleFramework == "tailwindcss") {
console.log(
chalk.magentaBright(` use command`),
chalk.greenBright.bold(
`npx tailwindcss -i ${path.join(
"./",
"style.css"
)} -o ${path.join("./", "output.css")} --watch`
),
chalk.magentaBright(`to process tailwindcss\n`)
);
}
} catch (error) {
console.log(chalk.redBright("An error occurred: ", error));
cancel(" create-default-app scaffold was cancelled.");
process.exit(0);
}
}
await handleScaffold();
} catch (error) {
if (isCancel(error)) {
console.log(
chalk.redBright(" create-default-app scaffold was cancelled. ")
);
process.exit(0);
} else {
console.log(chalk.redBright(" An error occurred: ", error));
}
}
}
main().catch((error) => {
console.error(chalk.redBright("An error occurred:", error));
});