forked from taskforcesh/bullmq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandTransform.js
64 lines (49 loc) · 1.75 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
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 = '';
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 += `export * from './${longName}';\n`;
await loadCommand(completedFilename, longName, writeFilenamePath);
}
await writeFile(path.join(writeFilenamePath, 'index.ts'), 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 = `const content = \`${content}\`;
export const ${name} = {
name: '${name}',
content,${
numberOfKeys
? `
keys: ${numberOfKeys},`
: ''
}
};
`;
await writeFile(path.join(writeFilenamePath, longName + '.ts'), newContent);
};
loadScripts(argv[2], argv[3]);