node-sloc
Version:
A small tool for counting SLOC.
92 lines (91 loc) • 4.53 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 });
exports.walkAndCount = void 0;
const graceful_fs_1 = __importDefault(require("graceful-fs"));
const utils = __importStar(require("./utils"));
/**
* @typedef {Object} Options
* @property {string} path The path to walk.
* @property {Array} extensions File extensions to look for.
* @property {Array} [ignorePaths] A list of directories to ignore.
* @property {boolean} [ignoreDefault] Whether or not to ignore the default extensions provided.
* @param {function} [logger] Outputs extra information to this function if specified.
*/
/**
* Async walks and counts SLOC in files in the given path.
* @param {Options} options Options passed to the function.
* @return {Promise} Resolves to an object containing SLOC and filepaths.
* Returns null if no files matched the given path.
*/
const walkAndCount = (options) => {
return new Promise((resolve, reject) => {
var _a;
if (!graceful_fs_1.default.existsSync(options.path)) {
reject('No such file or path. Exiting.');
}
// Make sure all extensions start with a dot
const normalizedExtensions = (_a = options.extensions) === null || _a === void 0 ? void 0 : _a.map((extension) => extension.startsWith('.') ? extension : `.${extension}`);
if (graceful_fs_1.default.statSync(options.path).isFile()) {
// If the file extension should be ignored, resolve with sloc: 0 and ignore the path
if (!utils.fileExtensionAllowed(options.path, normalizedExtensions || [])) {
resolve(null);
}
utils.countSloc(options.path).then((res) => {
// If the path argument is a file, count it
resolve(Object.assign({ paths: [options.path], files: 1 }, res));
});
}
else if (graceful_fs_1.default.statSync(options.path).isDirectory()) {
utils
.walk(options)
.then((res) => {
const promises = [];
const filteredPaths = utils.filterFiles(res, normalizedExtensions || []);
if (!filteredPaths.length) {
reject('No files found matching the specified extensions and path. Exiting.');
}
filteredPaths.forEach((fpath) => {
promises.push(utils.countSloc(fpath));
});
Promise.all(promises)
.then((values) => {
const totFiles = filteredPaths.length;
const slocSum = values.reduce((acc, curr) => {
return {
sloc: acc.sloc + curr.sloc,
loc: acc.loc + curr.loc,
blank: acc.blank + curr.blank,
comments: acc.comments + curr.comments,
};
});
resolve(Object.assign(Object.assign({ paths: filteredPaths }, slocSum), { files: totFiles }));
})
.catch((err) => reject(`Error when walking directory: ${err}`));
})
.catch((err) => reject(`Error when walking directory: ${err}`));
}
});
};
exports.walkAndCount = walkAndCount;