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

@@ -2,9 +2,6 @@
import chalk from 'chalk';
import program from 'commander';
// @ts-ignore
import slash from 'slash2';
import path from 'path';
import { LocDir } from './directory';
import { LocFile, LineInfo } from './file';

View File

@@ -5,6 +5,7 @@ import path from 'path';
import slash from 'slash2';
import { LineInfo, LocFile } from './file';
import { Languages } from './languages';
const defaultInfo: LineInfo = {
total: 0,
@@ -16,6 +17,7 @@ export interface LocDirOptions {
cwd?: string;
include?: string[] | string;
exclude?: string[] | string;
analysisLanguages?: string[];
}
export interface LocResult {
@@ -38,7 +40,86 @@ const defaultExclude = [
'**/*.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',
];
/**
@@ -48,6 +129,8 @@ export class LocDir {
private cwd: string;
private include: string[];
private exclude: string[];
private analysisLanguages?: string[];
private allLanguages = new Languages();
constructor(options: LocDirOptions) {
@@ -63,6 +146,7 @@ export 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;
}
/**
@@ -86,6 +170,7 @@ export class LocDir {
const fullPath = slash(path.join(this.cwd, pathItem));
if (
!pathItem ||
this.ignoreLanguage(pathItem) ||
!(await fs.pathExists(fullPath)) ||
(await fs.stat(fullPath)).isDirectory()
) {
@@ -115,6 +200,14 @@ export class LocDir {
languages,
};
}
/**
* Ignore analyzing this file if analysis languages are specified
* and this language is not one of them.
*/
private ignoreLanguage(pathItem: string): boolean {
return this.analysisLanguages && !this.analysisLanguages.includes(this.allLanguages.getType(pathItem));
}
}
function ensureArray(arr?: string[] | string, dfault?: string) {

View File

@@ -42,7 +42,7 @@ export class LocFile {
public path: string;
private rawPath: string;
private language = new Languages();
private languages = new Languages();
/**
* Creates an instance of LocFile.
@@ -52,14 +52,6 @@ export class LocFile {
this.rawPath = rawPath;
}
/**
* get file type through a path
*/
private getType(path: string): string {
const fileExtension = `.${path.split('.').pop()}`;
return this.language.extensionMap[fileExtension] || '';
}
private filterData = (data: string, regexes: Regexes): LineInfo => {
const lines = data.split(/\n/);
let commentLength = 0;
@@ -155,12 +147,12 @@ export class LocFile {
newData = data || await fs.readFile(this.path, 'utf-8');
info.name = name;
info.size = (stat && stat.size) || 0;
info.languages = this.getType(this.path);
info.languages = this.languages.getType(this.path);
if (!info.languages) {
return info;
}
if (newData) {
const regexes = this.language.getRegexes(info.languages);
const regexes = this.languages.getRegexes(info.languages);
info.lines = this.filterData(newData, regexes);
}
} catch (err) {
@@ -172,8 +164,8 @@ export class LocFile {
public getFileInfoByContent(name: string, data: string): FileInfo {
const info: FileInfo = Object.assign({}, DefaultFileInfo);
info.name = name;
info.languages = this.getType(name);
info.lines = this.filterData(data, this.language.getRegexes(info.languages));
info.languages = this.languages.getType(name);
info.lines = this.filterData(data, this.languages.getRegexes(info.languages));
return info;
}
}

View File

@@ -77,6 +77,14 @@ export class Languages {
public getExtensionMap() {
return this.extensionMap;
}
/**
* get file type through a path
*/
public getType(path: string): string {
const fileExtension = `.${path.split('.').pop()}`;
return this.extensionMap[fileExtension] || '';
}
}
export interface Regexes {