Created
June 26, 2016 15:51
-
-
Save deanshub/3033de21505db8c17d1fdd8fa3a8f110 to your computer and use it in GitHub Desktop.
passing through files doing whatever
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'); | |
var pathModule = require('path'); | |
var Q = require('q'); | |
var replace = require('replace'); | |
function init(path, refactorFiles, destPath) { | |
// console.log(path); | |
findPattern(path, /language\.js$/).then(function (filesPaths) { | |
refactorFiles(filesPaths, destPath); | |
}).catch(console.log); | |
} | |
function findPattern(path, pattern) { | |
return Q.nfcall(fs.stat, path).then(function (stat) { | |
if (stat.isFile() && pattern.test(path)) { | |
return path; | |
}else if(stat.isDirectory()) { | |
return Q.nfcall(fs.readdir, path).then(function (files) { | |
var promises = files.map(function (file) { | |
return findPattern(pathModule.join(path, file), pattern); | |
}); | |
return Q.allSettled(promises).then(function (results) { | |
return results.filter(function (result) { | |
if (result.state==='fulfilled'){ | |
return true; | |
}else{ | |
console.log('Error:',result.reason); | |
return false; | |
} | |
}).map(function (result) { | |
return result.value; | |
}).filter(function (result) { | |
return result!==undefined; | |
}); | |
}).then(function (results) { | |
return results.reduce(function (result, curr) { | |
if(Array.isArray(curr)){ | |
return result.concat(curr); | |
} | |
result.push(curr); | |
return result; | |
}, []); | |
}); | |
}); | |
} | |
}); | |
} | |
function refactorFiles(filesPaths, destPath) { | |
filesPaths.forEach(function(filePath) { | |
if (destPath){ | |
var result = /\\([^\\\.]+)\.module/.exec(filePath); | |
if (result && result[1]){ | |
var fileName = result[1] + '.js'; | |
var fileDestPath = pathModule.join(destPath, fileName); | |
fs.createReadStream(filePath).pipe(fs.createWriteStream(fileDestPath)).on('close',function () { | |
console.log('done with', fileDestPath); | |
replace({ | |
regex:/mod.translation\(\s?(\'|\")en(\'|\")\s?\,/, | |
replacement:'module.exports=(', | |
paths: [fileDestPath], | |
recursive: false, | |
silent: true, | |
}); | |
}); | |
} | |
}else { | |
console.log(filePath); | |
} | |
}); | |
} | |
init(process.argv[2], refactorFiles, process.argv[3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment