basic cache writing

master
Marcus 2019-05-26 04:14:53 -04:00
parent eeb2775981
commit 6a589ad991
6 changed files with 123 additions and 0 deletions

View File

@ -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) {

View File

@ -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));
}
}

26
core/compiler.js 100644
View File

@ -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]);
}
}

View File

@ -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());
})
})
}

View File

@ -0,0 +1,14 @@
module.exports.entity = class module {
async OnStart() {
console.log('starting...');
await new Promise(res => {
setTimeout(_ => {
console.log('done');
res();
}, 1000);
})
}
}

View File

@ -0,0 +1,7 @@
{
"Name": "module",
"From": "./../modules/",
"Data": {
"Thing": 5
}
}