serverline-sim/src/index.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-12-11 23:03:55 -05:00
/// <reference path="./externals.d.ts" />
2021-12-12 10:53:26 -05:00
import './logHook.js';
import serverline from 'serverline';
import createExecutor from './commands/executor.js';
import { create } from './commands/create.js';
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path'
import chalk from 'chalk';
2021-12-11 23:03:55 -05:00
console.clear();
2021-12-12 10:53:26 -05:00
const args = process.argv.slice(2);
const [ startupFile ] = args;
type Instance = any;
const system = {
instances: new Map<string, Instance>(),
handoff: ''
}
2021-12-12 11:23:03 -05:00
export const exec = async (s: string, echo = true) => {
2021-12-12 10:53:26 -05:00
if(echo) console.log(chalk.cyan('@ ') + chalk.ansi256(242)(s));
2021-12-12 11:23:03 -05:00
await executor(...(s.split(' ')));
2021-12-12 10:53:26 -05:00
};
2021-12-11 23:03:55 -05:00
serverline.init({
2021-12-12 10:53:26 -05:00
prompt: chalk.cyan('λ ')
2021-12-11 23:03:55 -05:00
});
// serverline.setCompletion(['help', 'command1', 'command2', 'login', 'check', 'ping'])
const executor = createExecutor({
2021-12-12 11:23:03 -05:00
async create(module: string, name: string) {
try {
const functions = await import('./modules/' + module + '.js');
console.log(functions)
} catch(e) {
console.log(e);
e.trace();
}
2021-12-12 10:53:26 -05:00
},
2021-12-11 23:03:55 -05:00
quit() {
console.log('Shutting down');
2021-12-12 10:53:26 -05:00
serverline.close();
process.exit(0);
},
ls(flags: any) {
if(flags) console.log(flags)
console.log('Instances (' + system.instances.size + ')');
for(const [k, v] of system.instances) {
console.log(' ' + k + ':', v);
}
},
save() {
const timeStart = new Date().getTime();
const systemString = JSON.stringify(system, null, 2);
const fullPath = resolve('.system');
writeFileSync(fullPath, systemString);
const elapsed = new Date().getTime() - timeStart;
console.log('System saved to ' + fullPath + ' in ' + elapsed + ' ms')
},
reset() {
system.handoff = '';
system.instances = new Map();
console.log('System has been reset.');
},
exec: exec,
invoke(...a: any[]) {
console.log(a);
2021-12-11 23:03:55 -05:00
}
});
2021-12-12 10:53:26 -05:00
serverline.on('line', (a: string) => {
exec(a, false);
})
2021-12-11 23:03:55 -05:00
serverline.on('SIGINT', () => {
2021-12-12 10:53:26 -05:00
exec('quit');
2021-12-11 23:03:55 -05:00
});
2021-12-12 10:53:26 -05:00
2021-12-12 11:23:03 -05:00
(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);
}
2021-12-12 10:53:26 -05:00
}
2021-12-12 11:23:03 -05:00
})();