-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
commandTransform.js
67 lines (52 loc) · 1.84 KB
/
commandTransform.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
'use strict';
const path = require('path');
const fs = require('fs');
const { argv } = require('process');
const readFile = fs.promises.readFile;
const writeFile = fs.promises.writeFile;
const readdir = fs.promises.readdir;
const loadScripts = async (readDir, writeDir) => {
const normalizedDir = path.normalize(readDir);
const files = await readdir(normalizedDir);
const luaFiles = files.filter(file => path.extname(file) === '.lua');
const writeFilenamePath = path.normalize(writeDir);
if (!fs.existsSync(writeFilenamePath)) {
fs.mkdirSync(writeFilenamePath);
}
let indexContent = "'use strict';\nmodule.exports = {\n";
if (luaFiles.length === 0) {
/**
* To prevent unclarified runtime error "updateDelayset is not a function
* @see https://github.com/OptimalBits/bull/issues/920
*/
throw new Error('No .lua files found!');
}
for (let i = 0; i < luaFiles.length; i++) {
const completedFilename = path.join(normalizedDir, luaFiles[i]);
const longName = path.basename(luaFiles[i], '.lua');
indexContent += ` ["${longName}"]: require('./${longName}'),\n`;
await loadCommand(completedFilename, longName, writeFilenamePath);
}
indexContent += `}\n`;
await writeFile(path.join(writeFilenamePath, 'index.js'), indexContent);
};
const loadCommand = async (filename, longName, writeFilenamePath) => {
const filenamePath = path.resolve(filename);
const content = (await readFile(filenamePath)).toString();
const [name, num] = longName.split('-');
const numberOfKeys = num && parseInt(num, 10);
const newContent = `'use strict';
const content = \`${content}\`;
module.exports = {
name: '${name}',
content,${
numberOfKeys
? `
keys: ${numberOfKeys},`
: ''
}
};
`;
await writeFile(path.join(writeFilenamePath, longName + '.js'), newContent);
};
loadScripts(argv[2], argv[3]);