import {readFileSync} from "fs"; import * as path from "path"; const RE_INCLUDE = /^include\s+"((?:[^"]|\\")+)"\s*$/mg; export function expandIncludes( scriptPath: string, excludedPaths: Set = new Set() ): string { const script = String(readFileSync(scriptPath)); const scriptDir = path.dirname(scriptPath); return script.replace(RE_INCLUDE, (match, location) => { const includePath = path.join(scriptDir, location); if (!excludedPaths.has(includePath)) { excludedPaths.add(includePath); return expandIncludes(includePath, excludedPaths); } else { return ""; } }); }