-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add commandTransform utility (#2736)
- Loading branch information
1 parent
afcf11c
commit 8c3bf1f
Showing
11 changed files
with
1,337 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ coverage | |
.vscode | ||
package-lock.json | ||
.nyc_output | ||
rawScripts | ||
lib/scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
const { ScriptLoader } = require('./lib/commands'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
|
||
const writeFile = promisify(fs.writeFile); | ||
|
||
class RawScriptLoader extends ScriptLoader { | ||
/** | ||
* Transpile lua scripts in one file, specifying an specific directory to be saved | ||
* @param pathname - the path to the directory containing the scripts | ||
* @param writeDir - the path to the directory where scripts will be saved | ||
*/ | ||
async transpileScripts(pathname, writeDir) { | ||
const writeFilenamePath = path.normalize(writeDir); | ||
|
||
if (!fs.existsSync(writeFilenamePath)) { | ||
fs.mkdirSync(writeFilenamePath); | ||
} | ||
|
||
const paths = new Set(); | ||
if (!paths.has(pathname)) { | ||
paths.add(pathname); | ||
const scripts = await this.loadScripts(pathname); | ||
for (const command of scripts) { | ||
const { | ||
name, | ||
options: { numberOfKeys, lua }, | ||
} = command; | ||
await writeFile( | ||
path.join(writeFilenamePath, `${name}-${numberOfKeys}.lua`), | ||
lua, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const scriptLoader = new RawScriptLoader(); | ||
|
||
scriptLoader.transpileScripts( | ||
path.join(__dirname, './lib/commands'), | ||
path.join(__dirname, './rawScripts'), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,8 @@ | ||
/** | ||
* Load redis lua scripts. | ||
* The name of the script must have the following format: | ||
* | ||
* cmdName-numKeys.lua | ||
* | ||
* cmdName must be in camel case format. | ||
* | ||
* For example: | ||
* moveToFinish-3.lua | ||
* | ||
*/ | ||
'use strict'; | ||
const { ScriptLoader } = require('./script-loader'); | ||
|
||
const fsAsync = require('fs').promises; | ||
const path = require('path'); | ||
const scriptLoader = new ScriptLoader(); | ||
|
||
module.exports = (function() { | ||
let scripts; | ||
|
||
return async function(client) { | ||
scripts = await (scripts || loadScripts()); | ||
|
||
return scripts.map(({ name, options }) => | ||
client.defineCommand(name, options) | ||
); | ||
}; | ||
})(); | ||
|
||
async function loadScripts() { | ||
const scriptsDir = await fsAsync.readdir(__dirname); | ||
const luaFiles = scriptsDir.filter(file => path.extname(file) === '.lua'); | ||
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!'); | ||
} | ||
return Promise.all( | ||
luaFiles.map(async file => { | ||
const lua = await fsAsync.readFile(path.join(__dirname, file)); | ||
const longName = path.basename(file, '.lua'); | ||
|
||
return { | ||
name: longName.split('-')[0], | ||
options: { | ||
numberOfKeys: parseInt(longName.split('-')[1]), | ||
lua: lua.toString() | ||
} | ||
}; | ||
}) | ||
); | ||
module.exports = { | ||
ScriptLoader, scriptLoader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.