Skip to content

Instantly share code, notes, and snippets.

@mientjan
Created January 4, 2017 01:17
Show Gist options
  • Save mientjan/d9d92242bceb2a2029891d5cedd04903 to your computer and use it in GitHub Desktop.
Save mientjan/d9d92242bceb2a2029891d5cedd04903 to your computer and use it in GitHub Desktop.
Quick Fix script make all paths relative to each other
var glob = require('glob');
var path = require('path');
var fs = require('fs');
var base = './deploy/htdocs/version/src/inc/script/app/';
var root = path.normalize(base + '../');
glob(root + '/**/*.ts', {}, (err, files) => {
console.log(files)
files.forEach(value => {
fs.readFile(value, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var r = /from\s\'((?!.)|([\w\/])+)\'/g;
var baseRelative = path.dirname(value.replace(root, ''));
data = data.replace(r, function(match, location){
var locationDir = path.dirname(location);
var resolved = path.relative(baseRelative, locationDir);
// console.log(value.replace(root, ''), location);
// console.log(path.relative(value.replace(root, ''), location));
console.log('--------------');
// console.log(value.replace(root, ''), location);
// console.log(match.replace(location, resolved));
console.log(baseRelative);
console.log(resolved.substr(0, 1), resolved, location);
var result = match.replace(location, (resolved ? resolved : '.') + '/' + path.basename(location));
console.log(result);
return result;
})
// var res = r.exec(data);
fs.writeFile(value, data, 'utf8', function (err) {
if (err) return console.log(err);
});
});
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment