node-sloc
Version:
A small tool for counting SLOC.
144 lines (135 loc) • 5.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const sloc = __importStar(require("./sloc"));
const utils = __importStar(require("./utils"));
const file_extensions_1 = require("./file-extensions");
const minimist_1 = __importDefault(require("minimist"));
const allowedExtensions = file_extensions_1.extensions.map((x) => x.lang);
const args = minimist_1.default(process.argv.slice(2));
const info = chalk_1.default.bold.blue;
const output = chalk_1.default.bold.gray;
const error = chalk_1.default.bold.red;
const result = chalk_1.default.bold.green;
const { version } = require('../package.json');
const helpText = `
node-sloc v. ${version}
usage:
node-sloc [path] [options]
options:
-h, --help Prints usage information
-l, --list-extensions Lists all default file extensions
-e, --include-extensions <list> Include non-default file extensions,
specified by a comma separated string of extensions
-i, --ignore-extensions <list> Include list of file extensions to ignore,
specified by a comma separated string of extensions
-x, --ignore-paths <list> Include a list of folders to exclude. Supports glob patterns
-d, --ignore-default Ignore the default file extensions
-v, --verbose Output extra information during execution
examples:
node-sloc ../app
node-sloc ../app --include-extensions "aaa, bbb, ccc" --ignore-extensions "xml, yaml"
node-sloc ../app --ignore-paths "node_modules, **/*.test.js"
node-sloc file.js
`;
let extensions = allowedExtensions;
let extraExtensions = [];
let ignoreExtensions = [];
let ignorePaths = [];
const handleExtensionString = (param, shorthand) => {
const arg = args[param] || args[shorthand];
if (!arg) {
console.log(error(`Missing argument for --${param}`));
process.exit(0);
}
// Strip spaces, slashes and dots and split on comma
return arg.replace(/ |\.|\\|\//g, '').split(',');
};
const handlePathString = (param, shorthand) => {
const arg = args[param] || args[shorthand];
if (!arg) {
console.log(error(`Missing argument for --${param}`));
process.exit(0);
}
// Strip spaces and split on comma
return arg.replace(/ /g, '').split(',');
};
// Help
if (args.help || args.h) {
console.log(helpText);
process.exit(0);
}
// List extensions
if (args['list-extensions'] || args.l) {
console.log('Supported file extensions:\n', `.${allowedExtensions.join('\n .')}`);
process.exit(0);
}
// Path
if (args._.length === 0) {
console.log(error('No file or directory specified.\n'), helpText);
process.exit(0);
}
// Extra extensions
if (args['include-extensions'] || args.e) {
extraExtensions = handleExtensionString('include-extensions', 'e');
}
// Ignore extensions
if (args['ignore-extensions'] || args.i) {
ignoreExtensions = handleExtensionString('ignore-extensions', 'i');
}
if (args['ignore-paths'] || args.x) {
ignorePaths = handlePathString('ignore-paths', 'x');
}
// Custom extensions
if (args['ignore-default'] || args.d) {
if (extraExtensions.length === 0) {
console.log(error('Error: flag --ignore-default was set but no file extensions were specified.'));
process.exit(0);
}
extensions = [];
}
const filepath = args._[0]; // Directory or file path
console.log(info('Reading file(s)...'));
const filteredExtensions = [...extensions, ...extraExtensions].filter((e) => !ignoreExtensions.includes(e));
const options = {
path: filepath,
extensions: filteredExtensions,
logger: args.verbose || args.v ? (v) => console.log(output(v)) : undefined,
ignorePaths: ignorePaths,
};
sloc
.walkAndCount(options)
.then((res) => {
// res is null if no files were read
if (!res) {
console.log(error('Unknown file extension. Use flag --extra-extensions to include extensions.'));
process.exit(0);
}
console.log(result(utils.prettyPrint(res)));
})
.catch((err) => {
console.log(error(err));
process.exit(0);
});