Created
May 20, 2016 09:03
-
-
Save tom2strobl/07f81bf27d212df609b8c415136381b1 to your computer and use it in GitHub Desktop.
Quick script that reads all files on given paths and writes the occurences of arguments to a __() function to a JSON and CSV file. Useful for localization and internationalization.
This file contains hidden or 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
'use strict'; | |
const Promise = require("bluebird"); | |
const _ = require("lodash"); | |
const glob = require("glob"); | |
const async = require("async"); | |
const fs = require("fs"); | |
const jsonfile = require("jsonfile"); | |
const json2csv = require("json2csv"); | |
Promise.promisifyAll(fs); | |
jsonfile.spaces = 2; | |
const paths = ['components/**/*.js', 'services/**/*.js', 'controllers/**/*.js', 'config/**/*.js', 'lib/**/*.js']; | |
const writeToPathJson = `${__dirname}/../config/locales/en.json`; | |
const writeToPathCsv = `${__dirname}/../config/locales/en.csv`; | |
const getAllFiles = () => { | |
return new Promise((fulfill, reject) => { | |
async.map(paths, (path, callback) => { | |
glob(path, (err, files) => { | |
if (err) { | |
callback(err); | |
} | |
callback(null, files); | |
}); | |
}, (err, result) => { | |
if (err) { | |
reject(err); | |
} | |
fulfill(_.flatten(result)); | |
}); | |
}); | |
}; | |
const parseCalls = strings => { | |
return new Promise((fulfill, reject) => { | |
async.map(strings, (string, callback) => { | |
// clear all \n's since they cause problems | |
string = string.replace(/(\r\n|\n|\r)/gm, ""); | |
// do the actual matches | |
const match = string.match(/__\((.*?)\)/g); | |
callback(null, match); | |
}, (err, result) => { | |
if (err) { | |
reject(err); | |
} | |
fulfill(result); | |
}); | |
}); | |
}; | |
const cleanCalls = paths => { | |
return new Promise((fulfill, reject) => { | |
// const occurences = _.compact(_.flatten(paths)); | |
const occurences = _.uniq(_.compact(_.flatten(paths))); | |
async.map(occurences, (string, callback) => { | |
// single quote | |
string = string.replace("__(\'", "").replace("\')", ""); | |
// double quote | |
string = string.replace('__(\"', "").replace('\")', ""); | |
// template strings | |
string = string.replace("__(`", "").replace("`)", ""); | |
callback(null, string); | |
}, (err, result) => { | |
if (err) { | |
reject(err); | |
} | |
fulfill(result); | |
}); | |
}); | |
}; | |
const saveStringsJSON = strings => { | |
return new Promise((fulfill, reject) => { | |
const obj = {}; | |
strings.forEach(string => { | |
obj[string] = string; | |
}); | |
jsonfile.writeFile(writeToPathJson, obj, err => { | |
if (err) { | |
reject(err); | |
} | |
fulfill(strings); | |
}); | |
}); | |
}; | |
const saveStringsCSV = strings => { | |
return new Promise((fulfill, reject) => { | |
const data = strings.map(string => { | |
return { | |
original: string, | |
improved: string | |
}; | |
}); | |
json2csv({data: data, fields: ['original', 'improved']}, (err, csv) => { | |
if (err) { | |
reject(err); | |
} | |
fs.writeFile(writeToPathCsv, csv, err => { | |
if (err) { | |
reject(err); | |
} | |
fulfill(csv); | |
}); | |
}); | |
}); | |
}; | |
const strings = []; | |
Promise.try(() => { | |
return getAllFiles(); | |
}).then(paths => { | |
return Promise.map(paths, fileName => { | |
return fs.readFileAsync(fileName, "utf8"); | |
}); | |
}).then(fileStrings => { | |
return parseCalls(fileStrings); | |
}).then(calls => { | |
return cleanCalls(calls); | |
}).then(strings => { | |
return saveStringsJSON(strings); | |
}).then(strings => { | |
return saveStringsCSV(strings); | |
}).then(() => { | |
console.log(`Successfully written to JSON and CSV!`); | |
}).catch(err => { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment