This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
vogue/src/Module.ts

111 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-05-20 00:19:25 -04:00
import { createAst, DirectiveRule, DirectiveValue, FunctionRule, ImportRule, LinkRule, NamespaceRule, VariableRule } from './createAst'
import path from 'path';
import debug from 'debug';
2021-05-16 20:41:45 -04:00
import { createRequire } from 'module';
import { pathToFileURL } from 'url';
const log = debug('vogue:module');
2021-05-02 17:42:04 -04:00
2021-05-20 00:19:25 -04:00
type Link = {
name: string,
array: boolean,
required: boolean
}
2021-05-02 17:42:04 -04:00
export default class Module {
2021-05-20 00:19:25 -04:00
links: Link[] = [];
2021-05-02 17:42:04 -04:00
globals = [];
2021-05-20 00:19:25 -04:00
functions: {
[name: string]: {
code: string,
parameters: string[]
}
} = {};
2021-05-02 17:42:04 -04:00
identifiers = {};
name = {
space: '',
last: '',
full: ''
2021-05-06 23:05:36 -04:00
};
imports = {};
variables = {
cold: [],
warm: []
2021-05-20 00:19:25 -04:00
};
directives: {
[key: string]: DirectiveValue
} = {
'singleton': false,
'keepalive': false,
'static': ''
};
rootDir: string = '';
2021-05-20 00:19:25 -04:00
async directive({directive, value}: DirectiveRule) {
this.directives[directive] = value;
}
2021-05-20 00:19:25 -04:00
async link({required, array, name}: LinkRule) {
this.links.push({
name,
required,
array
});
}
2021-05-20 00:19:25 -04:00
async namespace({namespace}: NamespaceRule) {
this.name.space = namespace;
this.name.full = this.name.space + '.' + this.name.last;
}
2021-05-20 00:19:25 -04:00
async function({name, block, parameters}: FunctionRule) {
this.functions[name] = {
code: block,
parameters
};
}
2021-05-20 00:19:25 -04:00
async import({importName, name}: ImportRule) {
2021-05-16 20:41:45 -04:00
const nodePath = path.resolve(this.rootDir, 'node_module');
log('#'.repeat(80));
log(nodePath);
const __require__ = createRequire(nodePath);
const imported = __require__(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-20 00:19:25 -04:00
async variable({persist, name}: VariableRule) {
this.variables[persist ? 'cold' : 'warm'].push(name);
2021-05-10 23:13:17 -04:00
}
2021-05-20 00:19:25 -04:00
static async create(location: string, rootDir: string) {
2021-05-10 23:13:17 -04:00
const module = new Module();
const ast = createAst(location);
const name = path.parse(location).name;
2021-05-10 23:13:17 -04:00
module.name.last = name;
module.name.full = name;
2021-05-16 20:41:45 -04:00
module.rootDir = rootDir;
2021-05-10 23:13:17 -04:00
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!');
else module.identifiers[item.name] = item.type;
}
2021-05-10 23:13:17 -04:00
if(item.type in module) {
await module[item.type](item);
2021-05-10 23:13:17 -04:00
}
}
log('='.repeat(80));
log(location);
log(module);
return module;
2021-05-10 23:13:17 -04:00
}
}