Created
February 16, 2018 15:08
-
-
Save ernstwi/574a3cdf4779bf5efb1ac23691db9eb4 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
let fs = require("fs"); | |
let path = require("path"); | |
// From https://gist.github.com/lovasoa/8691344 | |
function walk(dir, callback) { | |
fs.readdir(dir, function(err, files) { | |
if (err) throw err; | |
files.forEach(function(file) { | |
var filepath = path.join(dir, file); | |
fs.stat(filepath, function(err,stats) { | |
if (stats.isDirectory()) { | |
walk(filepath, callback); | |
} else if (stats.isFile()) { | |
callback(dir, filepath); | |
} | |
}); | |
}); | |
}); | |
} | |
walk('.', function(dir, filepath) { | |
let file = fs.readFileSync(filepath, 'utf8'); | |
for (let line of file.split('\n')) { | |
let m = line.match(/require\('(.*?)'\)/); | |
if (m == null) | |
continue; | |
let rp = m[1]; | |
if (!rp.startsWith('.')) | |
continue; | |
let ap = path.normalize(`${dir}/${rp}`); | |
if (ap.startsWith('.')) | |
continue; | |
console.log(`${rp} -> ${ap}`); | |
file = file.replace(`require('${rp}')`, `require('${ap}')`); | |
fs.writeFileSync(filepath, file); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment