-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
45 lines (39 loc) · 1.74 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
#!/usr/bin/env node
const glob = require('glob'),
fs = require('fs'),
path = require('path'),
prettier = require('prettier'),
keywords = JSON.parse(fs.readFileSync('./keywords.json'));
const getFiles = async () => new Promise((resolve, reject) =>
glob(process.cwd() + '/**/*.ws', { ignore: process.cwd() + '/node_modules/**'}, (err, matches) => err && reject(err) || matches && resolve(matches)));
const getContent = (path) => new Promise((resolve, reject) =>
fs.readFile(path, (err, data) => err && reject(err) || data && resolve(data)));
const transform = async (file) => ({
path: path.join(process.cwd(), 'dist', file.replace(process.cwd(), '').replace(/\.ws$/, '.js')),
file: file.replace(process.cwd(), ''),
content: String(await getContent(file))
});
const transpile = (file) => {
Object.entries(keywords).map(([key, value]) => (file = {
...file,
content: file.content.replace(new RegExp(key, 'g'), value)
}));
return file;
};
const saveFile = (file) => new Promise((resolve, reject) => {
fs.mkdir(file.path.substring(0, file.path.lastIndexOf(path.sep)), { recursive: true }, (err) => {
if (err) return reject(err);
console.info(`[WienerScript] DRAH DI DEPPATA ${file.file} ==> ${file.path.replace(process.cwd(), '')}`);
fs.writeFile(file.path, prettier.format(file.content, { parser: 'babel' }), (err, data) => err && reject(err) || data && resolve(data));
});
});
(async () => {
try {
const files = (await getFiles()).map(path.normalize);
console.info(`[WienerScript] HAWIDERE! DO HOBN MA ${files.length} GSCHICHTLN!`);
(await Promise.all(files.map(transform))).map(transpile).map(saveFile);
} catch(err) {
console.error('[WienerScript] DO IS WOS SCHIEF GRENNT:');
throw err;
}
})();