-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.js
201 lines (183 loc) · 7.18 KB
/
configure.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
// TODO: Add an argument that lets us specify a custom package name
const path = require("path");
const fs = require("fs");
const cp = require("child_process");
const types = ["vanilla", "express"];
const times = [];
if(process.argv.length < 3) {
printUsage();
process.exit(1);
}
const type = process.argv[2];
if(types.includes(type) === false) {
console.log("Invalid type: " + type);
printUsage();
process.exit(2);
}
let npm = "npm";
if(process.argv.includes("--use-pnpm")) npm = "pnpm";
console.log("Cheers for choosing BWS! - Making server: " + type);
time(); // Start the timer
process.stdout.write("> Renaming files and folders...");
const pwd = fs.realpathSync(path.resolve(__dirname, "."));
fs.renameSync(path.join(pwd, `package-${type}.json`), path.join(pwd, "package.json"));
fs.renameSync(path.join(pwd, `app-${type}`), path.join(pwd, "app"));
fs.renameSync(path.join(pwd, `server-${type}.js`), path.join(pwd, "server.js"));
process.stdout.write(done() + "\n");
let outline = "> Removing redundant files and folders...";
let lineSize = outline.length;
process.stdout.write(outline + "\n");
let rmFiles = [
"package-#.json",
"app-#/",
"server-#.js",
];
let lineCount = 1;
types.forEach(t => {
rmFiles.forEach(f => {
f = f.replace("#", t);
try {
process.stdout.write(` > Removing ${f}...`);
fs.rmSync(path.join(pwd, f), {recursive: true, force: true});
process.stdout.write(`${done()}\n`);
} catch(e) {
process.stderr.write(error(`Failed to remove ${f}. Do this manually.`, false));
lineCount += 1; // bc we print a message on a new line!
}
lineCount += 1; // bc we're adding a line anyway
});
});
// Move back to the end of the first line
process.stdout.write(`\x1b[${lineSize}C\x1b[${lineCount}A`);
let d = done(rmFiles.length * types.length)
process.stdout.write(d);
process.stdout.write(`\x1b[${lineSize + d.length}D\x1b[${lineCount}B`); // +5 == "Done!".length
Promise.resolve()
.then(() => new Promise((resolve, reject) => {
process.stdout.write(`> Installing dependencies...`);
let proc = cp.spawn(npm, ["install", "-P", "-D"], {
shell: true,
windowsHide: true,
});
proc.on("close", code => {
if(code === 0) {
process.stdout.write(`${done()}\n`);
resolve();
} else {
process.stderr.write(error(`Failed to install dependencies - run '${npm} i' and diagnose.`, true));
reject(code);
}
});
}))
.then(() => new Promise((resolve, reject) => {
process.stdout.write(`> Updating dependencies to the latest versions...`);
let proc = cp.spawn(npm, ["update", "-P", "-D"], {
shell: true,
windowsHide: true,
});
proc.on("close", code => {
if(code === 0) {
process.stdout.write(`${done()}\n`);
resolve();
} else {
process.stderr.write(error(`Failed to update dependencies - run '${npm} update' and diagnose.`, true));
reject(code);
}
});
}))
.then(() => new Promise((resolve, reject) => {
process.stdout.write(`> Deleting git remote reference...`);
let proc = cp.spawn("git", ["remote", "remove", "origin"], {
shell: true,
windowsHide: true,
});
proc.on("close", code => {
if(code === 0) {
process.stdout.write(`${done()}\n`);
resolve();
} else {
process.stderr.write(error(`Failed to delete remote reference - run 'git remote remove origin' and diagnose.`));
resolve();
}
});
}))
.then(() => new Promise(async (resolve, reject) => {
outline = `> Initialising git submodules...`;
lineSize = outline.length;
process.stdout.write(outline + "\n");
let submods = JSON.parse(fs.readFileSync(path.join(pwd, "gitmodules.json"), {encoding: "utf8"}));
lineCount = 1;
for(let submod of submods) {
await new Promise((innerResolve, reject) => {
process.stdout.write(` > Adding ${submod.name}...`);
let proc = cp.spawn(`git submodule add --name "${submod.name}" "${submod.url}" ${submod.path}`, {
shell: true,
windowsHide: true,
});
let stderr = "";
proc.stderr.on("data", (chunk) => stderr = stderr + chunk.toString());
proc.on("close", code => {
if(code === 0) {
process.stdout.write(`${done()}\n`);
innerResolve();
} else {
stderr = stderr.split("\n").map(l => " " + l).join("\n");
process.stderr.write(error(`Failed to initialise submodules. Stderr is below:`));
lineCount += 1;
innerResolve();
}
});
lineCount += 1; // bc we're adding a line anyway
});
}
// Clean the gitmod json
fs.rmSync(path.join(pwd, "gitmodules.json"), {recursive: true, force: true});
// Move back to the end of the first line
process.stdout.write(`\x1b[${lineSize}C\x1b[${lineCount}A`);
let d = done(submods.length)
process.stdout.write(d);
process.stdout.write(`\x1b[${lineSize + d.length}D\x1b[${lineCount}B`);
resolve();
}))
.then(() => {
process.stdout.write(`> Deleting configure script...`);
fs.rmSync(path.join(pwd, "configure.js"), {recursive: true, force: true});
process.stdout.write(`${done()}\n`);
})
.then(() => {
let total = prettyTime(time(-1));
process.stdout.write(`\x1b[32mAll done in ${total}!\x1b[0m\n`);
});
function printUsage() {
console.log(`Usage: node configure.js <${types.join("|")}> [--use-pnpm]`);
}
function prettyTime(time) {
let ms = time % 1000;
time = (time - ms) / 1000;
let s = time % 60;
time = (time - s) / 60;
let m = time % 60;
time = (time - m) / 60;
let h = time;
let builder = "";
if(h > 0) builder += `${h}h `;
if(m > 0) builder += `${m}m `;
if(s > 0) builder += `${s}s `;
if(ms > 0) builder += `${Math.round(ms)}ms`;
return builder.trim();
}
function time(furtherBack = 0) {
let now = performance.now();
times.push(now);
let diff = now - times[0];
if(furtherBack !== -1) diff = times.length >= 2 + furtherBack ? now - times[times.length - (2 + furtherBack)] : 0;
return diff;
}
function done(furtherBack) {
let timeTaken = prettyTime(time(furtherBack));
return ` \x1b[32mDone in ${timeTaken}!\x1b[0m`;
}
function error(message, breaking) {
let timeTaken = prettyTime(time());
return ` \x1b[31mError in ${timeTaken}!\n ! ${message}${!!breaking ? "\n ! Breaking..." : ""}\x1b[0m\n`;
}