Skip to content

Commit

Permalink
fix: error when .lua scripts missing in built bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelnov authored Jul 16, 2021
1 parent b3f0e2d commit 85307c3
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,32 @@ module.exports = (function() {
await utils.isRedisReady(client);
scripts = await (scripts || loadScripts(__dirname));

return scripts.map(command => {
return client.defineCommand(command.name, command.options);
});
return scripts.map(({ name, options }) => client.defineCommand(name, options));
};
})();

function loadScripts(dir) {
return _fs
.readdirAsync(dir)
.then(files => {
return files.filter(file => {
return path.extname(file) === '.lua';
});
})
.then(files => {
return Promise.all(
files.map(file => {
const longName = path.basename(file, '.lua');
const name = longName.split('-')[0];
const numberOfKeys = parseInt(longName.split('-')[1]);

return _fs.readFileAsync(path.join(dir, file)).then(lua => {
return {
name: name,
options: { numberOfKeys: numberOfKeys, lua: lua.toString() }
};
});
})
);
});
async function loadScripts(dir) {
const scriptsDir = await _fs.readdirAsync(dir);
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 _fs.readFileAsync(path.join(dir, file));
const longName = path.basename(file, '.lua');

return {
name: longName.split('-')[0],
options: {
numberOfKeys: parseInt(longName.split('-')[1]),
lua: lua.toString(),
},
};
}),
);
}

0 comments on commit 85307c3

Please sign in to comment.