ui-migration-stats
Version:
Migration stats for frameworks
2 lines • 8.92 kB
JavaScript
;function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}var fs=require('fs');var chalk=require('chalk');var prettyjson=require('prettyjson');var sloc=require('sloc');var args=require('commander');var path=require('path');var _require=require('./utils/helpers.js'),getChildren=_require.getChildren,getFolderTree=_require.getFolderTree,getPercentage=_require.getPercentage,ifModuleNameIncludes=_require.ifModuleNameIncludes,ifPathExists=_require.ifPathExists,makeDir=_require.makeDir,print=_require.print,sortByKeyFunctionBuilder=_require.sortByKeyFunctionBuilder,statsBuilder=_require.statsBuilder;args.version(process.env.npm_package_version).option('-s, --subdirectories','Display subdirectories').option('-S, --silent','Be silent').option('-c, --config <path>','Set config path, defualts to ./stats.conf.js','./stats.conf.js').parse(process.argv);var configPath=path.isAbsolute(args.config)?args.config:path.join(process.cwd(),args.config);var config=require(configPath);var CONSTANTS=require('./stats-constants.js');var CONFIG_KEYS=CONSTANTS.CONFIG_KEYS;var OPTIONS_KEYS=CONSTANTS.CONFIG_KEYS.OPTIONS_KEYS;var STATS_KEYS=CONSTANTS.STATS_KEYS;var WEBPACK_KEYS=CONSTANTS.WEBPACK_KEYS;var COMMON_KEYS=CONSTANTS.COMMON_KEYS;var options=config[CONFIG_KEYS.OPTIONS]||'';var framework1Config=config[CONFIG_KEYS.FRAMEWORK1];var framework2Config=config[CONFIG_KEYS.FRAMEWORK2];var framework1Name=framework1Config[CONFIG_KEYS.NAME];var framework2Name=framework2Config[CONFIG_KEYS.NAME];var webpackStats={};var stats={};var hadWarning=false;var timestamp=new Date;var webpackInputFile={};if(options[OPTIONS_KEYS.WEBPACK_STATS]){webpackInputFile=require(path.join(process.cwd(),options[OPTIONS_KEYS.WEBPACK_STATS]))}var prettyjsonOptions={numberColor:COMMON_KEYS.NUMBER_COLOR};var silent=options[OPTIONS_KEYS.SILENT]||args.silent;var includeSubdirectories=options[OPTIONS_KEYS.SUBDIRECTORIES]||args.subdirectories;var headingText=chalk.cyan.underline('Comparing '+framework1Name+' with '+framework2Name+'\n');stats=Object.assign(_defineProperty({},STATS_KEYS.TIMESTAMP,timestamp),statsBuilder(CONFIG_KEYS.FRAMEWORK1,framework1Config,includeSubdirectories),statsBuilder(CONFIG_KEYS.FRAMEWORK2,framework2Config,includeSubdirectories));if(!silent){print(headingText)}Object.keys(config).forEach(function(frameworkNumber){if(frameworkNumber===CONFIG_KEYS.FRAMEWORK1||frameworkNumber===CONFIG_KEYS.FRAMEWORK2){Object.keys(config[frameworkNumber]).forEach(function(typeOfKey){if(typeOfKey===CONFIG_KEYS.SRC||typeOfKey===CONFIG_KEYS.TEST){var _getFolderTree;var framework=CONFIG_KEYS.FRAMEWORK1;var type=STATS_KEYS.SRC_FILES;var subtype=STATS_KEYS.SRC_SUBDIRECTORY;var _path=config[frameworkNumber][typeOfKey][CONFIG_KEYS.PATH];if(!ifPathExists(_path)){print('ui-migration-stats '+chalk.red('PATH ERROR!:')+' '+_path+' is not a valid path, also make sure you have access permissions!');process.exit(1)}if(frameworkNumber===CONFIG_KEYS.FRAMEWORK2){framework=CONFIG_KEYS.FRAMEWORK2}if(typeOfKey===CONFIG_KEYS.TEST){type=STATS_KEYS.TEST_FILES;subtype=STATS_KEYS.TEST_SUBDIRECTORY}var includeOnlyRegExp=config[frameworkNumber][typeOfKey][CONFIG_KEYS.INCLUDE_ONLY];var tree=getFolderTree(_path,(_getFolderTree={},_defineProperty(_getFolderTree,COMMON_KEYS.EXTENSIONS,config[frameworkNumber][typeOfKey][STATS_KEYS.FILETYPES]),_defineProperty(_getFolderTree,CONFIG_KEYS.EXCLUDE,config[frameworkNumber][typeOfKey][CONFIG_KEYS.EXCLUDE]),_getFolderTree),function(item){if(includeOnlyRegExp&&!includeOnlyRegExp.test(item[COMMON_KEYS.PATH])){return}stats[framework][type]++});var subdirectoryTree=getChildren(tree,{children:[],files:[]},includeOnlyRegExp);if(includeSubdirectories){stats[framework][subtype]=subdirectoryTree.children}var slocStats=sloc.keys.reduce(function(acc,cur){acc[cur]=0;return acc},{});subdirectoryTree.files.forEach(function(file){var code=fs.readFileSync(file[COMMON_KEYS.PATH],'utf8',function(err){if(err){print(chalk.red(err))}});if(sloc.extensions.indexOf(file[COMMON_KEYS.EXTENSION])!==-1){var resultSloc=sloc(code,file[COMMON_KEYS.EXTENSION]);for(var i in sloc.keys){if(sloc.keys.hasOwnProperty(i)){var k=sloc.keys[i];slocStats[k]+=resultSloc[k]}}}else{if(options[OPTIONS_KEYS.UNCONDITIONAL_LOC]){var line_count=0;slocStats[STATS_KEYS.UNRECOGNIZED]=0;code.toString().split('\n').forEach(function(){line_count++});slocStats[STATS_KEYS.TOTAL]+=line_count;slocStats[STATS_KEYS.UNRECOGNIZED]+=line_count}else{hadWarning=true;print('ui-migration-stats '+chalk.yellow('WARNING:')+' Unsupported language '+file.extension+', skipped '+file.path+'. Set unconditionalLoc in config to include this file.')}}});var typeOfLocKey=''+typeOfKey+[COMMON_KEYS.LOC_CAPS];stats[framework][typeOfLocKey]=slocStats}})}});if(!stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_LOC]){stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_LOC]=_defineProperty({},STATS_KEYS.TOTAL,0)}if(!stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_LOC]){stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_LOC]=_defineProperty({},STATS_KEYS.TOTAL,0)}var totalFiles=stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.SRC_FILES]+stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_FILES]+stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.SRC_FILES]+stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_FILES];var totalLoc=stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.SRC_LOC][STATS_KEYS.TOTAL]+stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_LOC][STATS_KEYS.TOTAL]+stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.SRC_LOC][STATS_KEYS.TOTAL]+stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_LOC][STATS_KEYS.TOTAL];stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.PERCENTAGE_FILES]=getPercentage(stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.SRC_FILES],stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_FILES],totalFiles);stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.PERCENTAGE_FILES]=getPercentage(stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.SRC_FILES],stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_FILES],totalFiles);stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.PERCENTAGE_LOC]=getPercentage(stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.SRC_LOC][STATS_KEYS.TOTAL],stats[CONFIG_KEYS.FRAMEWORK1][STATS_KEYS.TEST_LOC][STATS_KEYS.TOTAL],totalLoc);stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.PERCENTAGE_LOC]=getPercentage(stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.SRC_LOC][STATS_KEYS.TOTAL],stats[CONFIG_KEYS.FRAMEWORK2][STATS_KEYS.TEST_LOC][STATS_KEYS.TOTAL],totalLoc);if(!silent){print(prettyjson.render(stats,prettyjsonOptions)+'\n')}if(options[OPTIONS_KEYS.FILENAME]){var statsString=JSON.stringify(stats,null,2);var statsFilename=options[OPTIONS_KEYS.FILENAME];var statsDirectory=path.dirname(statsFilename);var callback=function callback(err){if(err){return print(chalk.red(err))}if(!silent){print(chalk.cyan(COMMON_KEYS.SAVED+' - '+statsFilename))}};makeDir(statsDirectory);fs.writeFile(statsFilename,statsString,callback)}if(options[OPTIONS_KEYS.WEBPACK_STATS]){webpackInputFile.modules.forEach(function(module){var should_include=false;module.reasons.forEach(function(reason){if(typeof reason.userRequest==='string'){if(ifModuleNameIncludes(reason.userRequest,options[OPTIONS_KEYS.MODULES]||[''])){should_include=true}else{should_include=false}if(should_include){if(webpackStats.hasOwnProperty(reason.userRequest)){webpackStats[reason.userRequest].count++;webpackStats[reason.userRequest][WEBPACK_KEYS.ISSUERS].push(reason.moduleName)}else{var _webpackStats$reason$;webpackStats[reason.userRequest]=(_webpackStats$reason$={},_defineProperty(_webpackStats$reason$,WEBPACK_KEYS.MODULE,reason.userRequest),_defineProperty(_webpackStats$reason$,WEBPACK_KEYS.ENTRY,module.name),_defineProperty(_webpackStats$reason$,WEBPACK_KEYS.EXPORTS,module.usedExports),_defineProperty(_webpackStats$reason$,WEBPACK_KEYS.COUNT,1),_defineProperty(_webpackStats$reason$,WEBPACK_KEYS.ISSUERS,[reason.moduleName]),_webpackStats$reason$)}}}})});var webpackArray=Object.keys(webpackStats).map(function(key){return webpackStats[key]});if(options[OPTIONS_KEYS.SORT]){webpackArray.sort(sortByKeyFunctionBuilder([WEBPACK_KEYS.COUNT]))}if(options[OPTIONS_KEYS.WEBPACK_STATS_OUTPUT]){var _JSON$stringify;var webpackString=JSON.stringify((_JSON$stringify={},_defineProperty(_JSON$stringify,STATS_KEYS.TIMESTAMP,timestamp),_defineProperty(_JSON$stringify,WEBPACK_KEYS.WEBPACK_STATS,webpackArray),_JSON$stringify),null,2);var webpackStatsFilename=options[OPTIONS_KEYS.WEBPACK_STATS_OUTPUT];var webpackStatsDirectory=path.dirname(webpackStatsFilename);var _callback=function _callback(err){if(err){return print(chalk.red(err))}if(!silent){print(chalk.cyan(COMMON_KEYS.SAVED+' - '+webpackStatsFilename))}};makeDir(webpackStatsDirectory);fs.writeFile(webpackStatsFilename,webpackString,_callback)}}if(hadWarning){print(chalk.yellow('Done with warnings! Scroll up for more info.'))}module.exports={stats:stats,webpackStats:webpackStats};