diff --git a/core/aqua.js b/core/aqua.js index 761c4c9..be5b803 100644 --- a/core/aqua.js +++ b/core/aqua.js @@ -1,11 +1,24 @@ #!/usr/bin/env node const args = require('yargs').argv const path = require('path'); +const {compile} = require('./compiler.js'); args.index = args.index || path.join(process.cwd(), 'index.js'); args.cache = args.cache || path.join(process.cwd(), '.cache'); +console.log('a') let index = platformPrecompile(); +console.log('a') index = compileParameters(index); +console.log('a') +index = compileLinks(index); +console.log('a') + +compile({ + index: index, + cache: args.cache +}) +console.log('a') + /// Do all platform related things to the index file, like substituting /// CLI arguments, and converting from a filepath to an actual index object. @@ -40,6 +53,20 @@ function compileParameters (index) { }; } +function compileLinks (index) { + // TODO implement links + + let entities = index.Entities; + + for(const key in index.Parameters) { + entities = recursiveReplace(entities, `\$${key}`, index.Parameters[key]); + } + + return { + Entities: entities + }; +} + function recursiveReplace(obj, find, replace) { switch(typeof obj) { diff --git a/core/cache.js b/core/cache.js index e69de29..e1e97cd 100644 --- a/core/cache.js +++ b/core/cache.js @@ -0,0 +1,36 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports.Cache = class Cache { + constructor (basePath) { + this.paths = { + base: basePath, + code: path.join(basePath, 'code'), + instances: path.join(basePath, 'instances') + } + + try { + fs.mkdirSync(this.paths.base); + fs.mkdirSync(this.paths.code); + fs.mkdirSync(this.paths.instances); + } catch (e) {}; + + this.loadCache(); + } + + loadCache() { + for(let file of fs.readdirSync(this.paths.code)) { + // TODO like... do this + } + } + + addEntity(name, code) { + console.log(`writing ${name}...`); + fs.writeFileSync(path.join(this.paths.code, `${name}.js`), code); + } + + addInstance(instance) { + console.log(`writing ${instance}...`); + fs.writeFileSync(path.join(this.paths.instances, `${instance._id}.json`), JSON.stringify(instance, null, 2)); + } +} \ No newline at end of file diff --git a/core/compiler.js b/core/compiler.js new file mode 100644 index 0000000..f2f24cd --- /dev/null +++ b/core/compiler.js @@ -0,0 +1,26 @@ +const {Cache} = require('./cache.js'); +const {retrieveModule} = require('./retrieveModule.js'); + + +module.exports.compile = compile; + + +/// cache is the (likely path) string to describe where the cache is store +/// on the web this might be a database name for example. +/// index, is the post pre compiled index. +async function compile({cache: cachePath, index}) { + const cache = new Cache(cachePath); + // const modules = {}; + + for(const symbol in index.Entities) { + console.log(symbol) + const module = index.Entities[symbol]; + let code = await retrieveModule(module.From, module.Name); + // modules[module.Name] = code; + cache.addEntity(module.Name, code); + } + + for(const symbol in index.Entities) { + cache.addInstance(index.Entities[symbol]); + } +} \ No newline at end of file diff --git a/core/retrieveModule.js b/core/retrieveModule.js new file mode 100644 index 0000000..88699c1 --- /dev/null +++ b/core/retrieveModule.js @@ -0,0 +1,13 @@ +const path = require('path'); +const fs = require('fs'); + + +module.exports.retrieveModule = retrieveModule; + +function retrieveModule(source, name) { + return new Promise (res => { + fs.readFile(path.join(source, `${name}.js`), (err, data) => { + res(data.toString()); + }) + }) +} \ No newline at end of file diff --git a/tests/system/.cache/code/module.js b/tests/system/.cache/code/module.js new file mode 100644 index 0000000..a639630 --- /dev/null +++ b/tests/system/.cache/code/module.js @@ -0,0 +1,14 @@ + + +module.exports.entity = class module { + async OnStart() { + console.log('starting...'); + await new Promise(res => { + setTimeout(_ => { + console.log('done'); + res(); + }, 1000); + }) + } +} + diff --git a/tests/system/.cache/instances/undefined.json b/tests/system/.cache/instances/undefined.json new file mode 100644 index 0000000..facd745 --- /dev/null +++ b/tests/system/.cache/instances/undefined.json @@ -0,0 +1,7 @@ +{ + "Name": "module", + "From": "./../modules/", + "Data": { + "Thing": 5 + } +} \ No newline at end of file