refactor: better REPL api
This commit is contained in:
parent
8c8c879283
commit
0bb90808f4
|
@ -47,7 +47,7 @@ User.saltRounds = 10;
|
||||||
export default User;
|
export default User;
|
||||||
|
|
||||||
registerHooks({
|
registerHooks({
|
||||||
replCommands: (socket) => ({
|
replCommands: () => ({
|
||||||
createUser: (spec) => {
|
createUser: (spec) => {
|
||||||
const [email, maybePassword] = spec.split(' ', 2);
|
const [email, maybePassword] = spec.split(' ', 2);
|
||||||
const password = maybePassword || randomBytes(8).toString('hex');
|
const password = maybePassword || randomBytes(8).toString('hex');
|
||||||
|
@ -58,13 +58,11 @@ registerHooks({
|
||||||
const password = randomBytes(8).toString('hex');
|
const password = randomBytes(8).toString('hex');
|
||||||
const user = await User.findOne({where: {email}});
|
const user = await User.findOne({where: {email}});
|
||||||
if (user) {
|
if (user) {
|
||||||
user.addHashedPassword(password)
|
return user.addHashedPassword(password)
|
||||||
.then(() => user.save())
|
.then(() => user.save())
|
||||||
// eslint-disable-next-line no-console
|
.then(() => `\nNew password: ${password}\n\n`);
|
||||||
.then(() => socket.write(`New password:\n\n${password}\n`));
|
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line no-console
|
return 'User not found.\n';
|
||||||
console.log('User not found.');
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}, module.id);
|
}, module.id);
|
||||||
|
|
|
@ -16,9 +16,17 @@ export function createReplServer() {
|
||||||
replServer.context[key] = value;
|
replServer.context[key] = value;
|
||||||
});
|
});
|
||||||
Object.entries(
|
Object.entries(
|
||||||
invokeHookFlat('replCommands', socket).reduce((r, commands) => ({...r, ...commands}), {}),
|
invokeHookFlat('replCommands').reduce((r, commands) => ({...r, ...commands}), {}),
|
||||||
).forEach(([key, value]) => {
|
).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`);
|
netServer.listen(`/tmp/reddichat-${Date.now()}.sock`);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user