some shit for module loading

stable
Bronwen 2021-12-12 11:23:03 -05:00
parent 956ac726e1
commit 4e9debfa25
5 changed files with 31 additions and 21 deletions

View File

@ -1,12 +1,12 @@
export default function createExecutor(functions: any) { export default function createExecutor(functions: any) {
return function execute(...options: string[]) { return async function execute(...options: string[]) {
// console.log('λ', line); // console.log('λ', line);
const [cmd, ...args] = options; const [cmd, ...args] = options;
if(cmd in functions) { if(cmd in functions) {
const toInvoke: (...args: any[]) => void = functions[cmd as keyof typeof functions]; const toInvoke: (...args: any[]) => void | Promise<void> = functions[cmd as keyof typeof functions];
toInvoke(...args); await toInvoke(...args);
} else { } else {
console.log('Unknown command', cmd); console.log('Unknown command', cmd);
} }

3
src/externals.d.ts vendored
View File

@ -1 +1,2 @@
declare module "serverline"; declare module "serverline";
declare module "telnet";

View File

@ -20,9 +20,9 @@ const system = {
handoff: '' handoff: ''
} }
export const exec = (s: string, echo = true) => { export const exec = async (s: string, echo = true) => {
if(echo) console.log(chalk.cyan('@ ') + chalk.ansi256(242)(s)); if(echo) console.log(chalk.cyan('@ ') + chalk.ansi256(242)(s));
executor(...(s.split(' '))); await executor(...(s.split(' ')));
}; };
serverline.init({ serverline.init({
@ -31,8 +31,14 @@ serverline.init({
// serverline.setCompletion(['help', 'command1', 'command2', 'login', 'check', 'ping']) // serverline.setCompletion(['help', 'command1', 'command2', 'login', 'check', 'ping'])
const executor = createExecutor({ const executor = createExecutor({
create(module: string, name: string) { async create(module: string, name: string) {
console.log(s); try {
const functions = await import('./modules/' + module + '.js');
console.log(functions)
} catch(e) {
console.log(e);
e.trace();
}
}, },
quit() { quit() {
console.log('Shutting down'); console.log('Shutting down');
@ -74,10 +80,13 @@ serverline.on('SIGINT', () => {
exec('quit'); exec('quit');
}); });
if(startupFile) {
const fullPath = resolve(startupFile); (async () => {
const lines = readFileSync(fullPath).toString().split('\n').map(v => v.trim()); if(startupFile) {
for(const line of lines) { const fullPath = resolve(startupFile);
exec(line); const lines = readFileSync(fullPath).toString().split('\n').map(v => v.trim());
for(const line of lines) {
await exec(line);
}
} }
} })();

View File

@ -4,9 +4,9 @@ import { exec } from '@kernel';
export default { export default {
start() { start() {
var telnet = require('telnet') var telnet = require('telnet');
telnet.createServer(function (client) { telnet.createServer(function (client: any) {
// make unicode characters work properly // make unicode characters work properly
client.do.transmit_binary() client.do.transmit_binary()
@ -15,14 +15,14 @@ export default {
client.do.window_size() client.do.window_size()
// listen for the window size events from the client // listen for the window size events from the client
client.on('window size', function (e) { client.on('window size', function (e: any) {
if (e.command === 'sb') { if (e.command === 'sb') {
console.log('telnet window resized to %d x %d', e.width, e.height) console.log('telnet window resized to %d x %d', e.width, e.height)
} }
}) })
// listen for the actual data from the client // listen for the actual data from the client
client.on('data', function (b) { client.on('data', function (b: any) {
exec(b); exec(b);
client.write(b) client.write(b)
}) })

View File

@ -4,12 +4,12 @@
"downlevelIteration": true, "downlevelIteration": true,
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Node", "moduleResolution": "Node",
"baseUrl": "./", "baseUrl": "./src",
"paths": { "paths": {
"@kernel": [ "src/index.ts" ] "@kernel": [ "./" ]
} }
}, },
"include": [ "include": [
"src/index.ts" "src/**/*.ts"
] ]
} }