@mutasim77/loc-counter
Version:
A powerful lines of code counter with detailed statistics
95 lines (94 loc) • 3.77 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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.getFilePaths = getFilePaths;
exports.readFileContent = readFileContent;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const util_1 = require("util");
const ignore_1 = __importDefault(require("ignore"));
const minimatch_1 = __importDefault(require("minimatch"));
const readFileAsync = (0, util_1.promisify)(fs.readFile);
const readDirAsync = (0, util_1.promisify)(fs.readdir);
async function getFilePaths(directory, options) {
const absoluteDir = path.resolve(directory);
const ignoreRules = (0, ignore_1.default)();
ignoreRules.add(options.exclude);
// Add .gitignore
if (options.useGitignore) {
const gitignorePath = path.join(absoluteDir, '.gitignore');
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8');
ignoreRules.add(gitignoreContent);
}
}
// Ignore node_modules and .git
ignoreRules.add(['node_modules', '.git', '.DS_Store']);
const allFiles = await getAllFiles(absoluteDir);
const relativeFiles = allFiles.map(file => path.relative(absoluteDir, file));
const filteredFiles = relativeFiles.filter(file => {
if (ignoreRules.ignores(file)) {
return false;
}
if (options.include.length > 0) {
return options.include.some(pattern => (0, minimatch_1.default)(file, pattern) || (0, minimatch_1.default)(path.basename(file), pattern));
}
return true;
});
return filteredFiles.map(file => path.join(absoluteDir, file));
}
// Recursively get all files
async function getAllFiles(dir) {
const entries = await readDirAsync(dir, { withFileTypes: true });
const files = await Promise.all(entries.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
return entry.isDirectory()
? getAllFiles(fullPath)
: fullPath;
}));
return files.flat();
}
async function readFileContent(filePath) {
try {
return await readFileAsync(filePath, 'utf8');
}
catch (error) {
console.error(`Error reading file ${filePath}:`, error);
return '';
}
}