feat: REPL

This commit is contained in:
cha0s 2020-12-01 15:36:02 -06:00
parent 0973001840
commit 432dc81415
8 changed files with 5442 additions and 0 deletions

View File

@ -0,0 +1 @@
module.exports = require('../../config/.eslintrc');

0
packages/repl/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,5 @@
const copy = require('@neutrinojs/copy');
const config = require('../../config/.neutrinorc');
module.exports = config;

View File

@ -0,0 +1,31 @@
{
"name": "@latus/repl",
"version": "1.0.0",
"main": "index.js",
"author": "cha0s",
"license": "MIT",
"scripts": {
"build": "NODE_PATH=./node_modules webpack --mode production",
"lint": "NODE_PATH=./node_modules eslint --format codeframe --ext mjs,js .",
"test": "NODE_PATH=./node_modules mocha --config ../../config/.mocharc.js",
"watch": "NODE_PATH=./node_modules webpack --watch --mode development"
},
"files": [
"index.js",
"index.js.map"
],
"dependencies": {},
"devDependencies": {
"@neutrinojs/airbnb-base": "^9.4.0",
"@neutrinojs/copy": "9.4.0",
"@neutrinojs/library": "^9.4.0",
"@neutrinojs/mocha": "^9.4.0",
"chai": "4.2.0",
"eslint": "^7",
"eslint-import-resolver-webpack": "0.13.0",
"mocha": "^8",
"neutrino": "^9.4.0",
"webpack": "^4",
"webpack-cli": "^3"
}
}

View File

@ -0,0 +1,11 @@
import {createReplServer} from './server';
// eslint-disable-next-line import/prefer-default-export
export const $$latus = {
hooks: {
'@latus/core/config': () => ({
id: 'latus',
}),
'@latus/core/up': (plugins) => createReplServer(plugins),
},
};

View File

@ -0,0 +1,41 @@
import net from 'net';
import repl from 'repl';
export function createReplServer(plugins) {
const {config: {'@latus/repl': {id = 'latus'}}} = plugins;
const netServer = net.createServer((socket) => {
const replServer = repl.start({
prompt: `${id}> `,
input: socket,
output: socket,
});
replServer.on('exit', () => socket.end());
Object.entries(
plugins.invokeFlat('@latus/repl/context').reduce((r, vars) => ({...r, ...vars}), {}),
).forEach(([key, value]) => {
replServer.context[key] = value;
});
Object.entries(
plugins.invokeFlat('@latus/repl/commands').reduce((r, commands) => ({...r, ...commands}), {}),
).forEach(([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/${id}-${Date.now()}.sock`);
return netServer;
}
export function destroyReplServer(replServer) {
if (!replServer) {
return;
}
replServer.close();
}

View File

@ -0,0 +1,6 @@
// Whilst the configuration object can be modified here, the recommended way of making
// changes is via the presets' options or Neutrino's API in `.neutrinorc.js` instead.
// Neutrino's inspect feature can be used to view/export the generated configuration.
const neutrino = require('neutrino');
module.exports = neutrino(require(`${__dirname}/.neutrinorc`)).webpack();

5347
packages/repl/yarn.lock Normal file

File diff suppressed because it is too large Load Diff