refactor: better REPL api
This commit is contained in:
parent
8c8c879283
commit
0bb90808f4
|
@ -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);
|
||||
|
|
|
@ -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`);
|
||||
|
|
Loading…
Reference in New Issue
Block a user