refactor: better REPL api

This commit is contained in:
cha0s 2020-07-12 19:00:10 -05:00
parent 8c8c879283
commit 0bb90808f4
2 changed files with 14 additions and 8 deletions

View File

@ -47,7 +47,7 @@ User.saltRounds = 10;
export default User;
registerHooks({
replCommands: (socket) => ({
replCommands: () => ({
createUser: (spec) => {
const [email, maybePassword] = spec.split(' ', 2);
const password = maybePassword || randomBytes(8).toString('hex');
@ -58,13 +58,11 @@ registerHooks({
const password = randomBytes(8).toString('hex');
const user = await User.findOne({where: {email}});
if (user) {
user.addHashedPassword(password)
return user.addHashedPassword(password)
.then(() => user.save())
// eslint-disable-next-line no-console
.then(() => socket.write(`New password:\n\n${password}\n`));
.then(() => `\nNew password: ${password}\n\n`);
}
// eslint-disable-next-line no-console
console.log('User not found.');
return 'User not found.\n';
},
}),
}, module.id);

View File

@ -16,9 +16,17 @@ export function createReplServer() {
replServer.context[key] = value;
});
Object.entries(
invokeHookFlat('replCommands', socket).reduce((r, commands) => ({...r, ...commands}), {}),
invokeHookFlat('replCommands').reduce((r, commands) => ({...r, ...commands}), {}),
).forEach(([key, value]) => {
replServer.defineCommand(key, value);
replServer.defineCommand(key, async (arg) => {
const result = await value(arg);
if (result) {
socket.write(result, () => replServer.displayPrompt());
}
else {
replServer.displayPrompt();
}
});
});
});
netServer.listen(`/tmp/reddichat-${Date.now()}.sock`);