Skip to content

Instantly share code, notes, and snippets.

@eldilibra
Created October 28, 2014 00:55
Show Gist options
  • Save eldilibra/c65350fa01d95572be40 to your computer and use it in GitHub Desktop.
Save eldilibra/c65350fa01d95572be40 to your computer and use it in GitHub Desktop.
var fs = require('fs');
function resolve (path, visited) {
visited.push(path);
var fileString = fs.readStringFromFile(path);
if (fileString.contains("<include")) {
var includeIdxs = fileString.indexesOf("<include");
includeIdxs.forEach(function (includeIdx, i) {
var secondPath = fileString.readTilNext(includeIdx, '>');
if (visited.indexOf(secondPath) >= -1) {
throw new Error('Invalid cyclical dependency');
}
var secondResolve = resolve(secondPath, visited);
visited.pop();
var includeLen = secondPath.length + 10;
fileString.replace(includeIdx, includeLen, secondResolve);
if (i < includeIdxs.length - 1) {
includeIdxs[i + 1] += secondResolve.length - includeLen;
}
});
}
return fileString;
}
var visited = [];
resolve('a.script', visited);
@eldilibra
Copy link
Author

Pardon the var fs at the top. Just trying to get my in-vim jslint to shut up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment