-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
123 lines (110 loc) · 3.51 KB
/
bin.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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { exit } from 'process';
import { createInterface } from 'readline';
import { fileURLToPath } from 'url';
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
function mkdir(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (e) {
if (e.code === 'EEXIST') return;
throw e;
}
}
function copyRec(from, to, replacements, nameChanges) {
if(fs.statSync(from).isDirectory()) {
for(const file of fs.readdirSync(from)) {
let destFile = file;
for(const change of nameChanges) {
destFile = destFile.replace(change.regex, change.value);
}
copyRec(path.join(from, file), path.join(to, destFile), replacements, nameChanges);
}
} else {
const dirname = path.dirname(to);
const filename = path.basename(to);
mkdir(dirname);
if(replacements && replacements.has(filename)) {
const rules = replacements.get(filename);
let data = fs.readFileSync(from, {encoding: 'utf-8'});
for(const rule of rules) {
data = data.replace(rule.regex, rule.value);
}
fs.writeFileSync(to, data, { encoding: 'utf-8' });
} else {
fs.copyFileSync(from, to);
}
}
}
function dist() {
return fileURLToPath(new URL(`./dist`, import.meta.url).href);
}
async function input(question) {
return new Promise((resolve, reject) => {
try {
readline.question(question + " ", resolve)
} catch(err) {
reject(err);
}
});
}
const main = async () => {
// Get CWD:
const cwd = process.argv[2] || '.';
// Check installation directory:
if(fs.existsSync(cwd) && fs.readdirSync(cwd).length > 0) {
console.log("Target directory is not empty. Please try again with an empty directory.");
exit(1);
}
// Make working dir
mkdir(cwd);
// Get setup configuration input:
const title = await input(`What is the title of your site? (My Site)`) || 'My Site';
const description = await input(`What is your site description?`);
const outDir = await input(`Where should your site be built? (docs)`) || 'docs';
console.log(title, description, outDir);
let packageName = title.toLowerCase().replace(/\s/g, '-').replace(/[^0-9a-zA-Z-_]+/g, '');
if(packageName.match(/$[0-9]+/)) packageName = "sss-" + packageName;
// Copy project files to installation directory:
console.log(`Copying project files to: ${cwd}`);
// Copy files from dist and replace placeholder values
copyRec(dist(), cwd,
new Map([
["rollup.config.js", [
{ regex: /\$OUT_DIR/g, value: outDir },
{ regex: /\$SITE_TITLE/g, value: title },
{ regex: /\$SITE_DESCRIPTION/g, value: description }
]],
["package.json", [
{ regex: /\$OUT_DIR/g, value: outDir },
{ regex: /\$PACKAGE_NAME/g, value: packageName },
{ regex: /\$SITE_DESCRIPTION/g, value: description }
]],
["index.html", [
{ regex: /\$SITE_TITLE/g, value: title },
{ regex: /\$SITE_DESCRIPTION/g, value: description }
]],
[".gitignore", [{ regex: /\$OUT_DIR/g, value: outDir }]],
["README.md", [{ regex: /\$OUT_DIR/g, value: outDir }]]
]),
[
{ regex: /DOT\-/, value: "." },
{ regex: /static/, value: outDir }
]
);
console.log("Done!\n\nNext steps:\n\n1) run `npm install` in the project directory\n2) start the development server with `npm run dev`");
};
main()
.then(() => {
readline.close();
exit(0);
})
.catch(err => {
console.error(err);
exit(1);
});