-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
97 lines (86 loc) · 2.49 KB
/
start.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
#!/usr/bin/env node
import readline from "node:readline";
import { cp, readFile, writeFile } from "node:fs/promises";
import jsdom from "jsdom";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "ICT> ",
});
const sourceFolder = "2_base";
const question = "Project name: ";
async function handleInput(input) {
if (!input) {
console.error("No name provided");
rl.question(question, handleInput);
return;
}
try {
await copyFolder(input);
await updateChallengeIndex(input);
await updateIndex(input);
console.log(`Good Luck and have fun developing the challenge ${input}!`);
rl.close();
} catch (err) {
console.error(err.message);
rl.question(question, handleInput);
}
}
async function copyFolder(path) {
try {
await cp(sourceFolder, path, {
errorOnExist: true,
force: false,
recursive: true,
});
console.log("Folder copied!");
} catch (err) {
throw new Error("Folder already exists");
}
}
async function updateChallengeIndex(path) {
try {
const indexContent = await readFile(`${path}/index.html`, {
encoding: "utf8",
});
const dom = new jsdom.JSDOM(indexContent);
const document = dom.window.document;
const title = document.querySelector("title");
title.textContent = formatPath(path) + " - iCodeThis";
const h1 = document.querySelector("h1");
h1.textContent = formatPath(path);
await writeFile(`${path}/index.html`, dom.serialize(), {
encoding: "utf8",
});
console.log("Challenge Index Updated!");
} catch (err) {
console.error(err);
}
}
async function updateIndex(path) {
try {
const indexContent = await readFile(`index.html`, { encoding: "utf8" });
const dom = new jsdom.JSDOM(indexContent);
const document = dom.window.document;
const list = document.querySelector("#challenge-list");
const li = document.createElement("li");
const a = document.createElement("a");
a.href = `${path}/`;
a.textContent = formatPath(path);
li.appendChild(a);
list.appendChild(li);
// Does not apply linebreaks when write to file.
await writeFile(`index.html`, dom.serialize(), { encoding: "utf8" });
console.log("Main Index Updated!");
} catch (err) {
console.error(err);
}
}
function formatPath(path) {
let arr = path.split("-");
arr = arr.map(
(item) => (item = item.charAt(0).toUpperCase() + item.slice(1))
);
return arr.join(" ");
}
rl.question(question, handleInput);