Created
August 7, 2013 18:26
-
-
Save dncrews/6176961 to your computer and use it in GitHub Desktop.
I'm writing this for the purpose of copying all of my local `locale` files from all of my node projects that are using `strong` translation library (https://github.com/fs-webdev/strong) or (https://github.com/timshadel/strong) to a centralized place so that I can create a "translation memory".
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
/** | |
* I'm writing this for the purpose of copying all of my local | |
* locale files from all of my node projects that are using | |
* `strong` translation library (https://github.com/fs-webdev/strong) | |
* or (https://github.com/timshadel/strong) to a centralized place | |
* so that I can create a "translation memory". | |
* | |
* Usage: | |
* | |
* node localeCopier.js # from path defaults to "."; to path defaults to "~/Documents/locales" | |
* | |
* node localeCopier.js <where you want your locale files> # from path defaults to "." | |
* | |
* node localeCopier.js <some projects root path> <where you want your locale files> # trailing slashes optional | |
* | |
*/ | |
'use strict'; | |
var dryRun = false; | |
var exec, args, fromDir, toDir | |
, defaultFrom = '.' | |
, defaultTo = '~/Documents/locales'; | |
exec = require('child_process').exec; | |
args = process.argv; | |
if (args.length === 3) { | |
args[3] = args[2]; | |
args[2] = '.'; | |
} | |
fromDir = args[2] || defaultFrom; | |
toDir = args[3] || defaultTo; | |
function addSlash(path) { | |
var lastChar = path.charAt(path.length-1); | |
if ((lastChar !== '.') && (lastChar !== '/')) path += '/'; | |
return path; | |
} | |
fromDir = addSlash(fromDir); | |
toDir = addSlash(toDir); | |
// We want to ignore node_modules and build directories. If there's anything else you want to ignore, do so here | |
exec('find ' + fromDir + ' -type d | grep locales | grep -v node_modules | grep -v build', function(err,stdout){ | |
var dirs, i, l, _rel, names, moduleName, newName, command; | |
dirs = stdout.split("\n"); | |
for (i=0,l=dirs.length; i<l; i++) { | |
_rel = dirs[i]; | |
// We don't care about empty ones | |
if (!_rel.length) continue; | |
names = _rel.split('/'); | |
// We don't want subdirectories, since we're copying the whole directory | |
if (names[names.length - 1] !== 'locales') continue; | |
names.shift(); // Removes '.' | |
newName = toDir + names.shift(); | |
moduleName = names[names.indexOf('locales') - 1]; | |
if (moduleName) newName += '/' + moduleName; | |
names.splice(0, names.indexOf('locales') + 1); | |
command = "mkdir -p " + newName + " && cp -r " + _rel + "/* " + newName + "/"; | |
if (dryRun) { | |
console.log(command); | |
continue; | |
} | |
exec(command); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment