-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
57 lines (48 loc) · 1.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// loads processor list and can build namespace object for items on list
import { readFileSync } from "fs";
// Will contain trailing slash
// const __dirname = new URL('.', import.meta.url).pathname;
type NSlibrary ={[key: string]:{}};
type Profile={
id:string,
description:string,
modules:[string]
};
// cache last used
var lastName:string="";
var lastNS:NSlibrary={};
function library(name:string):NSlibrary {
if(lastName=== name) {return lastNS;}
const profile = loadjson("./profiles.json").find((e:Profile)=>e.id===name);
const ns = {};
if (profile) {
//console.time("namespaces: "+name)
const mods = profile.modules;
mods.forEach( function (uri:string) {
const mod = loadjson(uri);
//console.log("procmod: ",uri)
loadpackage(ns, mod);
});
//console.timeEnd("namespaces: "+name)
};
lastNS=ns;
lastName=name;
return ns;
};
function profiles() :[Profile]{
return loadjson("./profiles.json");
};
// for every namespace key in package create module entry in namespaces
function loadpackage(namespaces:{[key: string]:{}}, pkg:{}) {
for (const [ns, value] of (Object.entries(pkg) as [string, object][])) {
// if (namespaces.hasOwnProperty(ns)) console.log("existing: " + ns);
namespaces[ns] = value;
}
};
// path relative to run location
// https://github.com/eslint/eslint/discussions/15305#discussioncomment-2400923
function loadjson(path:string) {
const p = __dirname + "/" + path;
return JSON.parse(readFileSync(p, 'utf8'));
};
export { profiles, library };