xxl
Version:
LOC statistics for multiple directories
144 lines (108 loc) • 4.36 kB
JavaScript
/* --
```
Usage: xxl [options]
Options:
-h, --help output usage information
-V, --version output the version number
--src [srcDirs] source directories (comma-separated)
-e, --exclude [excludeDirs] excluded path fragments (comma-separated)
```
-- */
;
var _path = _interopRequireDefault(require("path"));
var _fs = _interopRequireDefault(require("fs"));
var _commander = _interopRequireDefault(require("commander"));
var _diveSync = _interopRequireDefault(require("diveSync"));
var _sloc = _interopRequireDefault(require("sloc"));
var _storyboard = require("storyboard");
var _storyboardListenerConsole = _interopRequireDefault(require("storyboard-listener-console"));
var _package = _interopRequireDefault(require("../package.json"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _storyboard.addListener)(_storyboardListenerConsole.default);
const EXTENSION_MAPPING = {
'.coffee': 'coffee',
'.js': 'js',
'.jsx': 'jsx',
'.ts': 'ts',
'.tsx': 'tsx',
'.cjsx': 'coffee',
'.html': 'html',
'.sass': 'scss',
'.css': 'css'
};
const DEFAULT_SOURCES = 'src';
const DEFAULT_EXCLUDE = '';
_commander.default.version(_package.default.version).option('--src [srcDirs]', 'source directories (comma-separated)', DEFAULT_SOURCES).option('-e, --exclude [excludeDirs]', 'excluded path fragments (comma-separated)', DEFAULT_EXCLUDE).option('v, --verbose').parse(process.argv);
_commander.default.src = _commander.default.src.split(/\s*,\s*/);
_commander.default.exclude = _commander.default.exclude ? _commander.default.exclude.split(/\s*,\s*/) : [];
/* eslint-disable no-useless-escape */
_commander.default.exclude = _commander.default.exclude.map(o => o.replace(/[\/,\\]/g, _path.default.sep));
/* eslint-enable no-useless-escape */
const getInitialStats = () => ({
filesProcessed: 0,
total: 0,
source: 0,
comment: 0,
single: 0,
block: 0,
mixed: 0,
empty: 0,
todo: 0,
extensions: {}
});
const filterOutPaths = thePath => {
for (let i = 0; i < _commander.default.exclude.length; i++) {
if (thePath.indexOf(_commander.default.exclude[i]) >= 0) return true;
}
return false;
};
const totalStats = getInitialStats();
const addStats = (stats, newStats, ext) => {
/* eslint-disable no-param-reassign */
stats.filesProcessed += 1;
Object.keys(newStats).forEach(k => {
stats[k] += newStats[k];
});
stats.extensions[ext] = stats.extensions[ext] == null ? 1 : stats.extensions[ext] + 1;
/* eslint-enable no-param-reassign */
};
const logStats = (title, stats) => {
_storyboard.mainStory.info('xxl', `Stats: ${_storyboard.chalk.cyan.bold(title)}:`);
Object.keys(stats).forEach(key => {
if (key !== 'extensions') {
_storyboard.mainStory.info('xxl', ` ${key} ${_storyboard.chalk.blue.bold(stats[key])}`);
} else {
_storyboard.mainStory.info('xxl', ' extensions:');
Object.keys(stats.extensions).forEach(ext => {
_storyboard.mainStory.info('xxl', ` ${ext} ${_storyboard.chalk.blue.bold(stats.extensions[ext])}`);
});
}
});
};
const processDir = srcPath => {
_storyboard.mainStory.info('xxl', `Processing ${_storyboard.chalk.cyan.bold(srcPath)}...`);
const partialStats = getInitialStats();
(0, _diveSync.default)(_path.default.normalize(srcPath), (err, filePath0) => {
if (err) {
_storyboard.mainStory.info('xxl', _storyboard.chalk.red.bold(err.stack));
return;
}
const filePath = _path.default.normalize(filePath0);
if (filterOutPaths(filePath)) return;
const ext = _path.default.extname(filePath);
const processor = EXTENSION_MAPPING[ext];
if (!processor) return;
if (_commander.default.verbose) {
_storyboard.mainStory.debug('xxl', `Processing ${_storyboard.chalk.cyan.bold(filePath)}...`);
}
const code = _fs.default.readFileSync(filePath, 'utf8');
const stats = (0, _sloc.default)(code, processor);
addStats(totalStats, stats, ext);
addStats(partialStats, stats, ext);
});
if (_commander.default.src.length > 1) logStats(srcPath, partialStats);
};
_commander.default.src.forEach(processDir);
if (_commander.default.src.length > 1) _storyboard.mainStory.info('xxl', '');
logStats('TOTAL', totalStats);