generated from me-julian/acm-widget-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
78 lines (60 loc) · 1.99 KB
/
init.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
import fs from 'fs/promises';
import readline from 'node:readline/promises';
async function main() {
const [widgetName, packageName] = await requestPackageName();
const pkgJSON = await fs.readFile('./package.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
return data;
});
const lockfile = await fs.readFile(
'./package-lock.json',
'utf8',
(err, data) => {
if (err) {
console.error(err);
return;
}
return data;
}
);
const updatedPkgJSON = pkgJSON.replace(/REPLACEME/g, packageName);
const updatedLockfile = lockfile.replace(/REPLACEME/g, packageName);
await fs.writeFile('./package.json', updatedPkgJSON);
await fs.writeFile('./package-lock.json', updatedLockfile);
const config = await fs.readFile('./config.js', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
return data;
});
const updatedConfig = config.replace(/REPLACEME/g, widgetName);
await fs.writeFile('config.js', updatedConfig);
}
async function requestPackageName() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const widgetName = await rl.question(
'What would you like to call your widget?\n' +
'Please use only lowercase alphabetical characters.\n'
);
const lowerAlphaRegex = new RegExp('^[a-z]+$');
if (!lowerAlphaRegex.test(widgetName)) {
throw new Error(
`Widget name "${widgetName}" doesn't pass regex '^[a-z]+$'\n` +
'Try a lowercase name containing only the characters a-z.'
);
}
const packageName = '@acm-widgets/' + widgetName;
console.log(
`Your widget will be called "${widgetName}" with NPM package "${packageName}"`
);
rl.close();
return [widgetName, packageName];
}
main();