In software development, accurately measuring the size of a codebase is essential for project management, resource allocation, and progress tracking. However, traditional tools often include auto-generated and data files, such as package-lock.json, yarn.lock, and data.json, etc. in their line counts. This leads to inflated metrics because technically, you didn't add the lines of code by yourself. To address this challenge, I developed git-repo-lines-of-code, an open-source project designed to provide precise line counts by excluding specified files.
The Motivation Behind git-repo-lines-of-code
While tools like cloc are popular for counting lines of code, they don't offer straightforward mechanisms to exclude specific files or patterns, especially when dealing with auto-generated files that can skew the analysis. This limitation prompted the creation of a tool that allows developers and managers to:
- Exclude Unwanted Files: Omit files that do not contribute to the actual codebase, ensuring more accurate metrics.
- Automate the Counting Process: Eliminate the need for manual adjustments, reducing errors and saving time.
- Enhance Reusability: Provide a solution that is easily integrated into various projects and workflows.
How to Use git-repo-lines-of-code
To get started with git-repo-lines-of-code, follow these steps:
- Installation: Install the package globally using npm:
npm install -g git-repo-lines-of-code
- Basic Usage:
import getRepoLinesOfCode from 'git-repo-lines-of-code';
const owner = 'octocat';
const repo = 'Hello-World';
const excludeFilePaths = ['path-to-file.ts', 'path-to-auto-generated-code.json'];
getRepoLinesOfCode(owner, repo, excludeFilePaths)
.then((linesOfCode) => {
console.log(`Total lines of code: ${linesOfCode}`);
})
.catch((error) => {
console.error(`Error: ${error}`);
});
Running this function will execute the line count with the specified exclusions.
Conclusion
git-repo-lines-of-code offers a streamlined approach to accurately measuring your codebase by excluding non-essential files. Its flexibility and ease of use make it a valuable tool for developers and managers seeking precise code metrics.
The package is open-source, and contributions are welcome. You can access the npm package here and the GitHub repository here.
Please, feel free to explore, contribute, and integrate it into your projects to maintain accurate code metrics.
Happy coding!
Top comments (0)