feat: REPL
This commit is contained in:
parent
0973001840
commit
432dc81415
1
packages/repl/.eslintrc.js
Normal file
1
packages/repl/.eslintrc.js
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports = require('../../config/.eslintrc');
|
0
packages/repl/.gitignore
vendored
Normal file
0
packages/repl/.gitignore
vendored
Normal file
5
packages/repl/.neutrinorc.js
Normal file
5
packages/repl/.neutrinorc.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
const copy = require('@neutrinojs/copy');
|
||||
|
||||
const config = require('../../config/.neutrinorc');
|
||||
|
||||
module.exports = config;
|
31
packages/repl/package.json
Normal file
31
packages/repl/package.json
Normal 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"
|
||||
}
|
||||
}
|
11
packages/repl/src/index.js
Normal file
11
packages/repl/src/index.js
Normal 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),
|
||||
},
|
||||
};
|
41
packages/repl/src/server.js
Normal file
41
packages/repl/src/server.js
Normal 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();
|
||||
}
|
6
packages/repl/webpack.config.js
Normal file
6
packages/repl/webpack.config.js
Normal 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
5347
packages/repl/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user