Skip to content

Instantly share code, notes, and snippets.

@bafxyz
Last active February 3, 2016 11:57
Show Gist options
  • Save bafxyz/c3b291a3de8d8aff9b90 to your computer and use it in GitHub Desktop.
Save bafxyz/c3b291a3de8d8aff9b90 to your computer and use it in GitHub Desktop.
/**
* This file adds logger in each function for parsed js files.
* After that you can simply console.log(window.logFunc); in browser and will see how many functions has been affected while you opened different site sections.
* This helps us collect object with used js functions, so unused functions can be simply removed from project.
*
* Setup:
* - node.js should be installed
* - run `npm i glob` in jsm-mobile\application\static\web folder
* - run `node logger-func.js` in jsm-mobile\application\static\web\frontend_backbone and check js files for changes
*/
var glob = require('./../node_modules/glob'),
fs = require('fs');
config = {
loggerBootstrapFile: './mobile.js',
parsedFolder: './common/**/*.js',
// Pattern for function Declarations and Anonymous functions if needed
// pattern: /\t*\w*\s?function[^\{]*\{/ig
// Pattern for function Expressions
pattern: /\t*\w*\s?:\sfunction[^\{]*\{/ig
};
glob(config.loggerBootstrapFile, function(er, files) {
files.forEach(function(file) {
var fileData = fs.readFileSync(file, 'utf-8');
fs.writeFileSync(file, 'window.logFunc = {}; \n\n' + fileData, 'utf8', function (err) {
if (err) return console.log(err);
});
});
});
glob(config.parsedFolder, function(er, files) {
files.forEach(function(file) {
var fileData = fs.readFileSync(file, 'utf-8');
// Pattern for function Expressions
(fileData.match(config.pattern) || []).forEach(function(func) {
var tabs = (func.match(/\t/ig) || []).join(''),
funcName = func.split(' ')[0].replace(/\t+?|:/g, ''),
replacement = 'window.logFunc[\'' + file + '->' + funcName + '\'] ? window.logFunc[\'' + file + '->' + funcName + '\'] += 1 : window.logFunc[\'' + file + '->' + funcName + '\'] = 1;',
result = func + '\n' + tabs + '\t' + replacement;
fileData = fileData.replace(func, result);
});
fs.writeFileSync(file, fileData, 'utf8', function (err) {
if (err) return console.log(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment