Skip to content

Instantly share code, notes, and snippets.

@tom2strobl
Created May 20, 2016 09:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
'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