Avoid analyzing excluded language files for line counting

This change passes in a list of file types to the line counting
analysis. These are the languages for the databases being analyzed.
Line count analysis is restricted to these files.
This commit is contained in:
Andrew Eisenberg 2021-04-28 14:57:44 -07:00
parent 5c0a38d7e4
commit ee2346270d
31 changed files with 436 additions and 49 deletions

View file

@ -10,6 +10,7 @@ const path_1 = __importDefault(require("path"));
// @ts-ignore
const slash2_1 = __importDefault(require("slash2"));
const file_1 = require("./file");
const languages_1 = require("./languages");
const defaultInfo = {
total: 0,
code: 0,
@ -24,7 +25,77 @@ const defaultExclude = [
'**/dist/**',
'**/*.snap',
// java
'**/target'
'**/target',
"**/*.class",
"**/*.o",
"**/bin",
"**/*.map",
// python
"**/*.pyc",
"**/*.pyo",
// other
"**/*.dil",
"**/*.ra",
// images
'**/*.png',
'**/*.jpg',
'**/*.jpeg',
'**/*.gif',
'**/*.ico',
'**/*.bmp',
'**/*.webp',
'**/*.tiff',
'**/*.psd',
'**/*.ai',
'**/*.ps',
'**/*.eps',
// fonts
'**/*.ttf',
'**/*.otf',
'**/*.woff',
'**/*.woff2',
'**/*.eot',
'**/*.ttc',
// audio
'**/*.mp3',
'**/*.wav',
'**/*.ogg',
'**/*.flac',
'**/*.aac',
'**/*.m4a',
'**/*.aif*',
// video
'**/*.mp4',
'**/*.mkv',
'**/*.avi',
'**/*.mov',
'**/*.wmv',
'**/*.mpg',
'**/*.mpeg',
'**/*.m2v',
'**/*.m4v',
// office
'**/*.doc',
'**/*.docx',
'**/*.docm',
'**/*.dot',
'**/*.dotx',
'**/*.xls',
'**/*.xlsx',
// documents
'**/*.pdf',
'**/*.epub',
'**/*.mobi',
// archives
'**/*.rar',
'**/*.zip',
'**/*.7z',
'**/*.tar',
'**/*.gz',
'**/*.bz2',
'**/*.bz',
'**/*.tbz',
'**/*.tgz',
];
/**
* Collect the info of a directory.
@ -49,6 +120,18 @@ class LocDir {
writable: true,
value: void 0
});
Object.defineProperty(this, "analysisLanguages", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "allLanguages", {
enumerable: true,
configurable: true,
writable: true,
value: new languages_1.Languages()
});
// ensure all excludes are globstar. Note that '**/*.ts/**' matches files
// that end in .ts because the globstar indicates 0 or more directory paths.
this.exclude = ensureArray(options.exclude)
@ -60,6 +143,7 @@ class LocDir {
.map(item => item.startsWith('./') ? item.substring(2) : item)
.map(item => item.endsWith('**') ? item : `${item}/**`);
this.cwd = options.cwd || process.cwd();
this.analysisLanguages = options.analysisLanguages;
}
/**
* Calculate directory info.
@ -76,6 +160,7 @@ class LocDir {
await Promise.all(paths.map(async (pathItem) => {
const fullPath = slash2_1.default(path_1.default.join(this.cwd, pathItem));
if (!pathItem ||
this.ignoreLanguage(pathItem) ||
!(await fs_extra_1.default.pathExists(fullPath)) ||
(await fs_extra_1.default.stat(fullPath)).isDirectory()) {
return;
@ -103,6 +188,13 @@ class LocDir {
languages,
};
}
/**
* Ignore analyzing this file if analysis languages are specified
* and this language is not one of them.
*/
ignoreLanguage(pathItem) {
return this.analysisLanguages && !this.analysisLanguages.includes(this.allLanguages.getType(pathItem));
}
}
exports.LocDir = LocDir;
function ensureArray(arr, dfault) {