private scope and typings! :)
parent
4a74b36961
commit
a539626667
|
|
@ -24,8 +24,10 @@ export default async function create(module: string, name: string, id: string) {
|
||||||
const functions = 'default' in imported ? imported.default : imported;
|
const functions = 'default' in imported ? imported.default : imported;
|
||||||
id ??= uuid.v4().replace(/-/g, '').toUpperCase();
|
id ??= uuid.v4().replace(/-/g, '').toUpperCase();
|
||||||
system.instances.set(id, {
|
system.instances.set(id, {
|
||||||
config: {},
|
privateScope: {
|
||||||
ram: {},
|
config: {},
|
||||||
|
ram: {}
|
||||||
|
},
|
||||||
module: module,
|
module: module,
|
||||||
functions
|
functions
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ export default function ls(flags: any) {
|
||||||
console.log(' ' + autoColorString(id.substring(0, 8)) + chalk.ansi256(242)(' (' + instance.module + ')'));
|
console.log(' ' + autoColorString(id.substring(0, 8)) + chalk.ansi256(242)(' (' + instance.module + ')'));
|
||||||
}
|
}
|
||||||
|
|
||||||
for(const [configKey, configVal] of Object.entries(instance.config)) {
|
for(const [configKey, configVal] of Object.entries(instance.privateScope.config)) {
|
||||||
console.log(' ' + chalk.ansi256(240)(configKey + ': ' + configVal));
|
console.log(' ' + chalk.ansi256(240)(configKey + ': ' + configVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ export default function save() {
|
||||||
};
|
};
|
||||||
for(const [id, info] of system.instances.entries()) {
|
for(const [id, info] of system.instances.entries()) {
|
||||||
obj.instances[id] = {
|
obj.instances[id] = {
|
||||||
config: info.config,
|
config: info.privateScope.config,
|
||||||
module: info.module
|
module: info.module
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,42 @@
|
||||||
declare module "serverline";
|
declare module "serverline";
|
||||||
declare module "telnet";
|
declare module "telnet";
|
||||||
declare module "@kernel:base";
|
declare module "@kernel:base" {
|
||||||
|
function exec(command: string, echo?: boolean): any;
|
||||||
|
function autoColorString(str: string): string;
|
||||||
|
function reverseAliasMap(): Map<string, string>
|
||||||
|
type Instance = {
|
||||||
|
module: string;
|
||||||
|
functions: {
|
||||||
|
[name: string]: (...args: any[]) => any
|
||||||
|
};
|
||||||
|
privateScope: {
|
||||||
|
config: { [name: string]: any };
|
||||||
|
ram: { [name: string]: any };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
type Id = string;
|
||||||
|
type ParsedSystemState = {
|
||||||
|
handoff: string,
|
||||||
|
instances: {
|
||||||
|
[id: Id]: {
|
||||||
|
config: any,
|
||||||
|
module: string
|
||||||
|
}
|
||||||
|
},
|
||||||
|
aliases: {
|
||||||
|
[alias: string]: Id
|
||||||
|
}
|
||||||
|
};
|
||||||
|
type System = {
|
||||||
|
aliases: Map<string, string>;
|
||||||
|
instances: Map<string, Instance>;
|
||||||
|
handoff: string;
|
||||||
|
}
|
||||||
|
const system: System;
|
||||||
|
}
|
||||||
declare module "@kernel:log-hook";
|
declare module "@kernel:log-hook";
|
||||||
declare module "@commands:executor";
|
declare module "@commands:executor";
|
||||||
declare module "@commands:create";
|
declare module "@commands:create";
|
||||||
declare module "@commands:ls";
|
declare module "@commands:ls";
|
||||||
declare module "@commands:save";
|
declare module "@commands:save";
|
||||||
declare module "@builtin:systemd";
|
|
||||||
declare module "@builtin:sshd";
|
|
||||||
declare module "@echo off";
|
declare module "@echo off";
|
||||||
21
src/index.ts
21
src/index.ts
|
|
@ -1,5 +1,6 @@
|
||||||
/// <reference path="./externals.d.ts" />
|
/// <reference path="./externals.d.ts" />
|
||||||
|
|
||||||
|
import { Instance, ParsedSystemState } from '@kernel:base';
|
||||||
import '@kernel:log-hook';
|
import '@kernel:log-hook';
|
||||||
import createExecutor from '@commands:executor';
|
import createExecutor from '@commands:executor';
|
||||||
import create from '@commands:create';
|
import create from '@commands:create';
|
||||||
|
|
@ -15,12 +16,7 @@ import md5 from 'md5';
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
const [ startupFile ] = args;
|
const [ startupFile ] = args;
|
||||||
|
|
||||||
type Instance = {
|
export type Id = string;
|
||||||
config: any;
|
|
||||||
ram: any;
|
|
||||||
module: string;
|
|
||||||
functions: any;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const system = {
|
export const system = {
|
||||||
instances: new Map<string, Instance>(),
|
instances: new Map<string, Instance>(),
|
||||||
|
|
@ -80,7 +76,7 @@ export const kernel = {
|
||||||
if(!(fn in instance.functions)) {
|
if(!(fn in instance.functions)) {
|
||||||
throw new Error('FUNCTION_DOES_NOT_EXIST_ON_INVOCATION_TARGET');
|
throw new Error('FUNCTION_DOES_NOT_EXIST_ON_INVOCATION_TARGET');
|
||||||
}
|
}
|
||||||
const bound = instance.functions[fn].bind(instance);
|
const bound = instance.functions[fn].bind(instance.privateScope);
|
||||||
await bound(...args);
|
await bound(...args);
|
||||||
},
|
},
|
||||||
async script(path: string) {
|
async script(path: string) {
|
||||||
|
|
@ -100,15 +96,12 @@ const executor = createExecutor(kernel);
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
||||||
if(existsSync('.system')) {
|
if(existsSync('.system')) {
|
||||||
const state: any = JSON.parse(readFileSync('.system').toString());
|
const state: ParsedSystemState = JSON.parse(readFileSync('.system').toString());
|
||||||
system.handoff = state.handoff;
|
system.handoff = state.handoff;
|
||||||
for(const [id, info] of Object.entries<any>(state.instances)) {
|
for(const [id, info] of Object.entries(state.instances)) {
|
||||||
const [alias] =
|
const [alias] = Object.entries(state.aliases).find(([,tryId]) => tryId === id) ?? [undefined];
|
||||||
Object.entries(state.aliases)
|
|
||||||
.find(([,tryId]) => tryId === id)
|
|
||||||
?? [undefined];
|
|
||||||
await kernel.create(info.module, alias, id);
|
await kernel.create(info.module, alias, id);
|
||||||
system.instances.get(id).config = info.config;
|
system.instances.get(id).privateScope.config = info.config;
|
||||||
}
|
}
|
||||||
checkpoint('System State Restored');
|
checkpoint('System State Restored');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,13 @@ import { EventEmitter } from 'events'
|
||||||
|
|
||||||
const events = new EventEmitter();
|
const events = new EventEmitter();
|
||||||
|
|
||||||
process.stdout.write = (function(write): any {
|
type WriteFunction = typeof process.stdout.write;
|
||||||
return function(string: string, encoding: any, fileDescriptor: any) {
|
|
||||||
// const ansiCodes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
process.stdout.write = (function(write: WriteFunction): WriteFunction {
|
||||||
// appendFileSync('.log', string);
|
return function(string: string): boolean {
|
||||||
events.emit('data', string);
|
events.emit('data', string);
|
||||||
write.apply(process.stdout, arguments);
|
|
||||||
|
return write.apply(process.stdout, arguments);
|
||||||
};
|
};
|
||||||
})(process.stdout.write);
|
})(process.stdout.write);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import telnet from 'telnet';
|
import telnet from 'telnet';
|
||||||
import { exec } from '@kernel:base';
|
import { exec, Instance } from '@kernel:base';
|
||||||
import log from '@kernel:log-hook';
|
import log from '@kernel:log-hook';
|
||||||
|
|
||||||
export function start() {
|
|
||||||
|
|
||||||
|
export function start(this: Instance["privateScope"]) {
|
||||||
telnet.createServer(function (client: any) {
|
telnet.createServer(function (client: any) {
|
||||||
|
|
||||||
// make unicode characters work properly
|
// make unicode characters work properly
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
import { exec } from '@kernel:base';
|
import { exec } from '@kernel:base';
|
||||||
|
|
||||||
|
// export default {
|
||||||
|
// config: {
|
||||||
|
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
export function boot() {
|
export function boot() {
|
||||||
for(const [name, script] of Object.entries(this.config)) {
|
for(const [name, script] of Object.entries(this.config)) {
|
||||||
exec(script);
|
exec(script as string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue