diff --git a/src/commands/executor.ts b/src/commands/executor.ts index 56dc525..3e61867 100644 --- a/src/commands/executor.ts +++ b/src/commands/executor.ts @@ -1,12 +1,12 @@ export default function createExecutor(functions: any) { - return function execute(...options: string[]) { + return async function execute(...options: string[]) { // console.log('λ', line); const [cmd, ...args] = options; if(cmd in functions) { - const toInvoke: (...args: any[]) => void = functions[cmd as keyof typeof functions]; - toInvoke(...args); + const toInvoke: (...args: any[]) => void | Promise = functions[cmd as keyof typeof functions]; + await toInvoke(...args); } else { console.log('Unknown command', cmd); } diff --git a/src/externals.d.ts b/src/externals.d.ts index edcd57b..b2ddfc3 100644 --- a/src/externals.d.ts +++ b/src/externals.d.ts @@ -1 +1,2 @@ -declare module "serverline"; \ No newline at end of file +declare module "serverline"; +declare module "telnet"; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index ebf6e17..db6d04f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,9 +20,9 @@ const system = { 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)); - executor(...(s.split(' '))); + await executor(...(s.split(' '))); }; serverline.init({ @@ -31,8 +31,14 @@ serverline.init({ // serverline.setCompletion(['help', 'command1', 'command2', 'login', 'check', 'ping']) const executor = createExecutor({ - create(module: string, name: string) { - console.log(s); + async create(module: string, name: string) { + try { + const functions = await import('./modules/' + module + '.js'); + console.log(functions) + } catch(e) { + console.log(e); + e.trace(); + } }, quit() { console.log('Shutting down'); @@ -74,10 +80,13 @@ serverline.on('SIGINT', () => { exec('quit'); }); -if(startupFile) { - const fullPath = resolve(startupFile); - const lines = readFileSync(fullPath).toString().split('\n').map(v => v.trim()); - for(const line of lines) { - exec(line); + +(async () => { + if(startupFile) { + const fullPath = resolve(startupFile); + const lines = readFileSync(fullPath).toString().split('\n').map(v => v.trim()); + for(const line of lines) { + await exec(line); + } } -} \ No newline at end of file +})(); \ No newline at end of file diff --git a/src/modules/sshd.ts b/src/modules/sshd.ts index 74104d9..d551f00 100644 --- a/src/modules/sshd.ts +++ b/src/modules/sshd.ts @@ -4,9 +4,9 @@ import { exec } from '@kernel'; export default { start() { - var telnet = require('telnet') + var telnet = require('telnet'); - telnet.createServer(function (client) { + telnet.createServer(function (client: any) { // make unicode characters work properly client.do.transmit_binary() @@ -15,14 +15,14 @@ export default { client.do.window_size() // 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') { console.log('telnet window resized to %d x %d', e.width, e.height) } }) // listen for the actual data from the client - client.on('data', function (b) { + client.on('data', function (b: any) { exec(b); client.write(b) }) diff --git a/tsconfig.json b/tsconfig.json index b790eb1..875302f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,12 +4,12 @@ "downlevelIteration": true, "module": "ESNext", "moduleResolution": "Node", - "baseUrl": "./", + "baseUrl": "./src", "paths": { - "@kernel": [ "src/index.ts" ] + "@kernel": [ "./" ] } }, "include": [ - "src/index.ts" + "src/**/*.ts" ] } \ No newline at end of file