AquaTin/core/cache.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-05-26 04:14:53 -04:00
const fs = require('fs');
const path = require('path');
2019-05-27 01:32:55 -04:00
const {Signale} = require('signale');
const log = new Signale({
scope: 'CACHE'
});
2019-05-26 04:14:53 -04:00
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() {
// log.debug(this.paths)
2019-05-27 01:32:55 -04:00
// if(fs.existsSync(this.paths.code))
2019-05-26 04:14:53 -04:00
for(let file of fs.readdirSync(this.paths.code)) {
// TODO like... do this
}
}
addEntity(name, code) {
fs.writeFileSync(path.join(this.paths.code, `${name}.js`), code);
}
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;
}
2019-05-26 04:14:53 -04:00
}