-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (67 loc) · 1.94 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const SandboxedModule = require("sandboxed-module");
function recFindByExt(base, ext, files, result) {
files = files || fs.readdirSync(base);
result = result || [];
files.forEach(function (file) {
var newbase = path.join(base, file);
if (fs.statSync(newbase).isDirectory()) {
result = recFindByExt(newbase, ext, fs.readdirSync(newbase), result);
} else {
if (file.substr(-1 * (ext.length + 1)) == "." + ext) {
result.push(newbase);
}
}
});
return result;
}
function moduleMap(options) {
const { source } = options;
const base = path.resolve(source);
const files = recFindByExt(base, "js");
let map = new Map();
files.forEach((file) => {
const fileMetaData = fs.statSync(file);
let mapObject = map.get(file);
if (mapObject) {
const newMapItemWithFileContent = Object.assign(mapObject, {
metaData: fileMetaData,
filePath: file,
});
map.delete(file);
map.set(file, newMapItemWithFileContent);
return;
}
map.set(file, {
metaData: fileMetaData,
});
const fileContent = fs.readFileSync(file, "utf8");
mapObject = map.get(file);
if (mapObject) {
const newMapItemWithFileContent = Object.assign(mapObject, {
content: fileContent,
filePath: file,
});
map.delete(file);
map.set(file, newMapItemWithFileContent);
return;
}
map.set(file, {
content: fileContent,
});
});
let newMap = new Map();
map.forEach((mapItem) => {
const code = mapItem.content;
const mod = SandboxedModule.load(mapItem.filePath, {});
let mapObject = map.get(mapItem.filePath);
map.delete(mapObject.filePath);
mapObject.id = mod.locals.id;
mapObject.exports = mod.locals.exports;
newMap.set(mapObject.filePath, mapObject);
});
return Object.fromEntries(newMap);
}
module.exports = moduleMap;