2021-05-11 19:38:55 -04:00
|
|
|
import createAst from './createAst.js'
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import debug from 'debug';
|
|
|
|
|
const log = debug('vogue:module');
|
2021-05-02 17:42:04 -04:00
|
|
|
|
|
|
|
|
export default class Module {
|
|
|
|
|
links = {
|
|
|
|
|
required: {
|
|
|
|
|
single: [],
|
|
|
|
|
arrays: []
|
|
|
|
|
},
|
|
|
|
|
optional: {
|
|
|
|
|
single: [],
|
|
|
|
|
arrays: []
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
globals = [];
|
|
|
|
|
functions = [];
|
|
|
|
|
identifiers = {};
|
|
|
|
|
name = {
|
|
|
|
|
space: '',
|
|
|
|
|
last: '',
|
|
|
|
|
full: ''
|
2021-05-06 23:05:36 -04:00
|
|
|
};
|
2021-05-08 23:06:31 -04:00
|
|
|
imports = {};
|
|
|
|
|
variables = {
|
|
|
|
|
cold: [],
|
|
|
|
|
warm: []
|
|
|
|
|
}
|
2021-05-11 19:38:55 -04:00
|
|
|
singleton = false;
|
|
|
|
|
keepalive = false;
|
|
|
|
|
|
|
|
|
|
async directive({value}) {
|
|
|
|
|
this[value] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async link({required, array, name}) {
|
|
|
|
|
this.links
|
|
|
|
|
[required ? 'required' : 'optional']
|
|
|
|
|
[array ? 'arrays' : 'single']
|
|
|
|
|
.push(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async namespace({namespace}) {
|
|
|
|
|
this.name.space = namespace;
|
|
|
|
|
this.name.full = this.name.space + '.' + this.name.last;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function({name, block}) {
|
|
|
|
|
this.functions[name] = block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async import({importName, name}) {
|
|
|
|
|
const imported = await import(importName);
|
|
|
|
|
if('default' in imported) this.imports[name] = imported.default;
|
|
|
|
|
else this.imports[name] = imported;
|
|
|
|
|
}
|
2021-05-10 23:13:17 -04:00
|
|
|
|
2021-05-11 19:38:55 -04:00
|
|
|
async variable({persist, name}) {
|
|
|
|
|
this.variables[persist ? 'cold' : 'warm'].push(name);
|
2021-05-10 23:13:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async create(location) {
|
|
|
|
|
const module = new Module();
|
|
|
|
|
const ast = createAst(location);
|
2021-05-11 19:38:55 -04:00
|
|
|
const name = path.parse(location).name;
|
2021-05-10 23:13:17 -04:00
|
|
|
|
|
|
|
|
module.name.last = name;
|
|
|
|
|
module.name.full = name;
|
|
|
|
|
|
2021-05-11 19:38:55 -04:00
|
|
|
// move module whole loop ass bitch into module.
|
|
|
|
|
for (const item of ast) {
|
|
|
|
|
if ('name' in item) {
|
|
|
|
|
if(item.name in module.identifiers)
|
2021-05-10 23:13:17 -04:00
|
|
|
throw new Error('Identifier ' + item.name + ' already declared!');
|
2021-05-11 19:38:55 -04:00
|
|
|
else module.identifiers[item.name] = item.type;
|
|
|
|
|
}
|
2021-05-10 23:13:17 -04:00
|
|
|
|
2021-05-11 19:38:55 -04:00
|
|
|
if(item.type in module) {
|
|
|
|
|
await module[item.type](item);
|
2021-05-10 23:13:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 19:38:55 -04:00
|
|
|
log('='.repeat(80));
|
|
|
|
|
log(location);
|
|
|
|
|
log(module);
|
|
|
|
|
|
|
|
|
|
return module;
|
2021-05-10 23:13:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|