Created
October 28, 2014 00:55
-
-
Save eldilibra/c65350fa01d95572be40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pardon the
var fs
at the top. Just trying to get my in-vim jslint to shut up.