Skip to content

Instantly share code, notes, and snippets.

@sheutettz
Created August 18, 2015 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheutettz/840bd4cb8741f419cd84 to your computer and use it in GitHub Desktop.
Save sheutettz/840bd4cb8741f419cd84 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// =================================================================================
// Copyright (c) 2014 sheutettz. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// =================================================================================
'use strict';
var _ = require('lodash');
var fs = require('fs');
var glob = require('glob');
var uglifyjs = require('uglify-js');
var args = process.argv.slice(2);
var filePatterns = [];
var skip = false;
var len = args.length;
args.forEach(function (val, index) {
if (skip) {
skip = false;
return;
}
if (_.isString(val) && val.length > 0) {
if (val.match(/^-+[a-zA-Z0-9][-a-zA-Z0-9]*$/)) {
console.warn('unknown command line option: ' + val);
}
else {
filePatterns.push(val);
}
}
});
if (filePatterns.length === 0) {
console.error('Error! No filename patterns specified!\n');
console.error('Usage: find-ttt.js filename-patterns [filename-patterns ...]');
console.error();
}
var filesMatched = [];
_.each(filePatterns, function (filenamePattern) {
var ff = glob.sync(filenamePattern, {});
filesMatched = filesMatched.concat(ff);
});
_.each(filesMatched, findTextToTranslate);
function findTextToTranslate(srcFile) {
var code = fs.readFileSync(srcFile, "utf8");
var toplevel = uglifyjs.parse(code);
var count = 0;
var walker = new uglifyjs.TreeWalker(function (node, godown) {
if (node instanceof uglifyjs.AST_Call) {
var expr = node.expression;
if (expr.name === 'T') {
if (count === 0) {
console.log(srcFile + ':');
}
count++;
var text = escapedString(node.args[0].value);
if (node.args.length > 1) {
console.log(' ' + text + " // used with " + (node.args.length - 1) + " paramter(s)");
}
else {
console.log(' ' + text);
}
return true;
}
}
});
toplevel.walk(walker);
if (count > 0) {
console.log();
}
}
function escapedString(s) {
var json = JSON.stringify(["" + s]);
return json.substr(1, json.length - 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment