-
Notifications
You must be signed in to change notification settings - Fork 0
/
getRecursiveSource.js
30 lines (26 loc) · 969 Bytes
/
getRecursiveSource.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
const path = require("path");
const fs = require("fs");
function getRecursiveSource(module, source) {
const REGEX_INCLUDE = /^([\t ])#Include\s+(.*)$/gim;
let replaceNewLine = function(s) {
return s.replace(/\r\n/gm, '\n');
};
source = replaceNewLine(source).replace(/\\/g, '\\\\').replace(/`/g, '\\`');
let result = REGEX_INCLUDE.exec(source);
while (result != null) {
let match = result[0];
let indent = result[1];
let includePath = path.resolve(path.join(module.context, result[2]));
if (!fs.existsSync(includePath)) {
throw new Error("File does not exist: " + includePath);
}
module.addDependency(includePath);
let includeSource = fs.readFileSync(includePath, "utf-8");
includeSource = getRecursiveSource(module, includeSource);
includeSource = includeSource.replace(/^/gm, indent);
source = source.replace(match, includeSource);
result = REGEX_INCLUDE.exec(source);
}
return source;
}
module.exports = getRecursiveSource;