diff --git a/core/aqua.js b/core/aqua.js index 3b2460f..6bfb498 100644 --- a/core/aqua.js +++ b/core/aqua.js @@ -3,7 +3,6 @@ const {Signale} = require('signale'); const log = new Signale({ scope: 'CLI' }); -const interactive = new Signale({interactive: true}); const path = require('path'); const fs = require('fs'); @@ -24,19 +23,35 @@ require('yargs') }) }, cliCompile) + .command('run [paramaters]', 'executes a cache', (yargs) => { + yargs.option('cache', { + type: 'string', + default: '.cache', + describe: 'path of the cache' + }) + }, cliRun) + .help() .argv; return; +async function cliRun(args) { + const {Cache} = require('./cache.js'); + const {System} = require('./system.js') + if(!path.isAbsolute(args.cache)) args.cache = path.join(process.cwd(), args.cache); + const cache = new Cache(args.cache); + new System(cache); +} + /// this is the base compile function, that the CLI directly calls. async function cliCompile(args) { const {compile} = require('./compiler.js'); - log.debug(path.isAbsolute(args.index)); - log.debug(args.index); - log.debug(process.cwd()); + // log.debug(path.isAbsolute(args.index)); + // log.debug(args.index); + // log.debug(process.cwd()); if(!path.isAbsolute(args.index)) args.index = path.join(process.cwd(), args.index); if(!path.isAbsolute(args.cache)) args.cache = path.join(process.cwd(), args.cache); diff --git a/core/cache.js b/core/cache.js index c84d329..bb4758e 100644 --- a/core/cache.js +++ b/core/cache.js @@ -23,7 +23,7 @@ module.exports.Cache = class Cache { } loadCache() { - log.info(this.paths) + // log.debug(this.paths) // if(fs.existsSync(this.paths.code)) for(let file of fs.readdirSync(this.paths.code)) { // TODO like... do this @@ -37,4 +37,20 @@ module.exports.Cache = class Cache { addInstance(instance) { fs.writeFileSync(path.join(this.paths.instances, `${instance._id}.json`), JSON.stringify(instance, null, 2)); } + + getInstances() { + return fs.readdirSync(this.paths.instances).map((val) => { + return val.split('.')[0]; + }); + } + + getEntityCodePathFromUuid(uuid) { + let instancePath = path.join(this.paths.instances, `${uuid}.json`); + let codePath = path.join(this.paths.code, `${require(instancePath).Name}.js`); + return codePath; + } + getDataFromUuid(uuid) { + let instancePath = path.join(this.paths.instances, `${uuid}.json`); + return require(instancePath).Data; + } } \ No newline at end of file diff --git a/core/compiler.js b/core/compiler.js index 1ac6c2d..7254393 100644 --- a/core/compiler.js +++ b/core/compiler.js @@ -9,7 +9,7 @@ const log = new Signale({ module.exports.compile = compile; -function compile({cache: cachePath, index}) { +async function compile({cache: cachePath, index}) { index = compileParameters(index); log.info('parameters injected'); @@ -18,10 +18,12 @@ function compile({cache: cachePath, index}) { log.info('entity links created') - createCache({ + await createCache({ index: index, cache: cachePath - }) + }); + + log.info('cache created') } /// cache is the (likely path) string to describe where the cache is store diff --git a/core/system.js b/core/system.js index e69de29..a3a49f6 100644 --- a/core/system.js +++ b/core/system.js @@ -0,0 +1,53 @@ +const {Signale} = require('signale'); +const log = new Signale({ + scope: 'EXEC' +}); +const path = require('path'); +const fs = require('fs'); +const uuid = require('uuid'); + +const timeout = 3000; +const root = (typeof window === 'undefined' ? global : window) + +module.exports.System = class System { + constructor(cache) { + this.cache = cache; + this.entities = {}; + for(const uuid of cache.getInstances()) { + this.send('Start', uuid) + } + } + + async send(name, destination, options) { + // log.debug(`sending ${name}`); + + if(!(destination in this.entities)) + this.loadEntity(destination); + + let a = require('./../tests/modules/module.js').entity + let b = new a({}); + + if(name in this.entities[destination]) + this.entities[destination][name](options); + + else if('*' in this.entities[destination]) + this.entities[destination]['*'](options); + + else log.warn(`method ${name} does not exist for ${destination}`); + } + + loadEntity(uuid) { + // log.debug(`spinning up ${uuid}`); + if(typeof require === 'function') { + let codePath = this.cache.getEntityCodePathFromUuid(uuid); + // log.debug(codePath) + let data = this.cache.getDataFromUuid(uuid); + let entityProto = require(codePath).entity; + this.entities[uuid] = new entityProto(data); + } else { + //TODO COMPLICATED EVAL SHIT, MIRRORING REQUIRE FUNCTIONALITY + } + } + + +} \ No newline at end of file diff --git a/tests/modules/module.js b/tests/modules/module.js index a639630..afd8424 100644 --- a/tests/modules/module.js +++ b/tests/modules/module.js @@ -1,7 +1,7 @@ module.exports.entity = class module { - async OnStart() { + async Start() { console.log('starting...'); await new Promise(res => { setTimeout(_ => {