Files
codeql-action/node_modules/github-linguist/dist/directory.js
Andrew Eisenberg c4a84a93d4 Add the github-linguist package
This commit only adds a single package and all of its transitive
dependencies. The github-linguist package will be used for counting
lines of code as a baseline for databases we are analyzing.
2021-04-22 15:59:49 -07:00

116 lines
3.7 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocDir = void 0;
const globby_1 = __importDefault(require("globby"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
// @ts-ignore
const slash2_1 = __importDefault(require("slash2"));
const file_1 = require("./file");
const defaultInfo = {
total: 0,
code: 0,
comment: 0,
};
const defaultExclude = [
// javascript
'**/*.map',
'**/yarn**',
'**/.github',
'**/node_modules/**',
'**/dist/**',
'**/*.snap',
// java
'**/target'
];
/**
* Collect the info of a directory.
*/
class LocDir {
constructor(options) {
Object.defineProperty(this, "cwd", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "include", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "exclude", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// 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)
.concat(defaultExclude)
.map(item => item.endsWith('**') ? item : `${item}/**`);
// remove all leading './' since this messes up globstar matches in the
// excludes.
this.include = ensureArray(options.include, '**')
.map(item => item.startsWith('./') ? item.substring(2) : item)
.map(item => item.endsWith('**') ? item : `${item}/**`);
this.cwd = options.cwd || process.cwd();
}
/**
* Calculate directory info.
*/
async loadInfo() {
const paths = await globby_1.default(this.include, {
cwd: this.cwd,
ignore: this.exclude,
nodir: true
});
const files = [];
const info = { ...defaultInfo };
let languages = {};
await Promise.all(paths.map(async (pathItem) => {
const fullPath = slash2_1.default(path_1.default.join(this.cwd, pathItem));
if (!pathItem ||
!(await fs_extra_1.default.pathExists(fullPath)) ||
(await fs_extra_1.default.stat(fullPath)).isDirectory()) {
return;
}
const file = new file_1.LocFile(fullPath);
const fileLineInfo = await file.getFileInfo();
const { lines } = fileLineInfo;
info.total += lines.total;
info.code += lines.code;
info.comment += lines.comment;
const language = { ...languages[fileLineInfo.languages] };
language.code = lines.code + (language.code || 0);
language.sum = (language.sum || 0) + 1;
language.comment = lines.comment + (language.comment || 0);
language.total = lines.total + (language.total || 0);
languages = {
...languages,
[fileLineInfo.languages]: language,
};
files.push(fullPath);
}));
return {
files,
info,
languages,
};
}
}
exports.LocDir = LocDir;
function ensureArray(arr, dfault) {
if (!arr) {
return dfault ? [dfault] : [];
}
return Array.isArray(arr)
? arr
: [arr];
}
//# sourceMappingURL=directory.js.map