linguist-js
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -57,3 +57,3 @@ "use strict"; | ||
// Get file count | ||
let totalFiles = (0, walk_tree_1.default)(root).files.length; | ||
const totalFiles = (0, walk_tree_1.default)(root).files.length; | ||
// Print output | ||
@@ -60,0 +60,0 @@ if (!args.json) { |
@@ -17,4 +17,4 @@ "use strict"; | ||
async function analyse(input, opts = {}) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j; | ||
var _k, _l, _m; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q; | ||
var _r, _s, _t; | ||
const langData = await (0, load_data_1.default)('languages.yml').then(js_yaml_1.default.load); | ||
@@ -38,3 +38,12 @@ const vendorData = await (0, load_data_1.default)('vendor.yml').then(js_yaml_1.default.load); | ||
].flat(); | ||
let { files, folders } = (0, walk_tree_1.default)(input !== null && input !== void 0 ? input : '.', ignoredFiles); | ||
let files, folders; | ||
if (opts.fileContent) { | ||
opts.fileContent = Array.isArray(opts.fileContent) ? opts.fileContent : [opts.fileContent]; | ||
files = [`${input}`]; | ||
folders = ['']; | ||
} | ||
else { | ||
const data = (0, walk_tree_1.default)(input !== null && input !== void 0 ? input : '.', ignoredFiles); | ||
({ files, folders } = data); | ||
} | ||
// Apply aliases | ||
@@ -55,3 +64,3 @@ opts = { checkIgnored: !opts.quick, checkAttributes: !opts.quick, checkHeuristics: !opts.quick, checkShebang: !opts.quick, ...opts }; | ||
const customText = []; | ||
if (!opts.quick) { | ||
if (!opts.fileContent && !opts.quick) { | ||
for (const folder of folders) { | ||
@@ -103,3 +112,3 @@ // Skip if folder is marked in gitattributes | ||
// Check vendored files | ||
if (!opts.keepVendored) { | ||
if (!opts.fileContent && !opts.keepVendored) { | ||
// Filter out any files that match a vendor file path | ||
@@ -122,5 +131,11 @@ const matcher = (match) => RegExp(match.replace(/\/$/, '/.+$').replace(/^\.\//, '')); | ||
for (const file of files) { | ||
if (!fs_1.default.existsSync(file) || fs_1.default.lstatSync(file).isDirectory()) | ||
continue; | ||
const firstLine = await (0, read_file_1.default)(file, true).catch(() => null); | ||
let firstLine; | ||
if (opts.fileContent) { | ||
firstLine = (_f = (_e = (_d = opts.fileContent) === null || _d === void 0 ? void 0 : _d[files.indexOf(file)]) === null || _e === void 0 ? void 0 : _e.split('\n')[0]) !== null && _f !== void 0 ? _f : null; | ||
} | ||
else { | ||
if (!fs_1.default.existsSync(file) || fs_1.default.lstatSync(file).isDirectory()) | ||
continue; | ||
firstLine = await (0, read_file_1.default)(file, true).catch(() => null); | ||
} | ||
// Skip if file is unreadable | ||
@@ -140,3 +155,3 @@ if (firstLine === null) | ||
// Check override for manual language classification | ||
if (!opts.quick && opts.checkAttributes) { | ||
if (!opts.fileContent && !opts.quick && opts.checkAttributes) { | ||
const match = overridesArray.find(item => RegExp(item[0]).test(file)); | ||
@@ -153,3 +168,3 @@ if (match) { | ||
// Check if filename is a match | ||
const matchesName = (_d = langData[lang].filenames) === null || _d === void 0 ? void 0 : _d.some(name => path_1.default.basename(file.toLowerCase()) === name.toLowerCase()); | ||
const matchesName = (_g = langData[lang].filenames) === null || _g === void 0 ? void 0 : _g.some(name => path_1.default.basename(file.toLowerCase()) === name.toLowerCase()); | ||
if (matchesName) { | ||
@@ -163,3 +178,3 @@ addResult(file, lang); | ||
// Check if extension is a match | ||
const matchesExt = (_e = langData[lang].extensions) === null || _e === void 0 ? void 0 : _e.some(ext => file.toLowerCase().endsWith(ext.toLowerCase())); | ||
const matchesExt = (_h = langData[lang].extensions) === null || _h === void 0 ? void 0 : _h.some(ext => file.toLowerCase().endsWith(ext.toLowerCase())); | ||
if (matchesExt) | ||
@@ -175,3 +190,3 @@ addResult(file, lang); | ||
// Skip binary files | ||
if (!opts.keepBinary) { | ||
if (!opts.fileContent && !opts.keepBinary) { | ||
const isCustomText = customText.some(path => RegExp(path).test(file)); | ||
@@ -220,6 +235,6 @@ const isCustomBinary = customBinary.some(path => RegExp(path).test(file)); | ||
// If no heuristics, assign a language | ||
(_f = (_k = results.files.results)[file]) !== null && _f !== void 0 ? _f : (_k[file] = fileAssociations[file][0]); | ||
(_j = (_r = results.files.results)[file]) !== null && _j !== void 0 ? _j : (_r[file] = fileAssociations[file][0]); | ||
} | ||
// Skip specified categories | ||
if ((_g = opts.categories) === null || _g === void 0 ? void 0 : _g.length) { | ||
if ((_k = opts.categories) === null || _k === void 0 ? void 0 : _k.length) { | ||
const categories = ['data', 'markup', 'programming', 'prose']; | ||
@@ -242,3 +257,3 @@ const hiddenCategories = categories.filter(cat => !opts.categories.includes(cat)); | ||
// Convert paths to relative | ||
if (opts.relativePaths) { | ||
if (!opts.fileContent && opts.relativePaths) { | ||
const newMap = {}; | ||
@@ -257,3 +272,3 @@ for (const [file, lang] of Object.entries(results.files.results)) { | ||
continue; | ||
const fileSize = fs_1.default.statSync(file).size; | ||
const fileSize = (_o = (_m = (_l = opts.fileContent) === null || _l === void 0 ? void 0 : _l[files.indexOf(file)]) === null || _m === void 0 ? void 0 : _m.length) !== null && _o !== void 0 ? _o : fs_1.default.statSync(file).size; | ||
results.files.bytes += fileSize; | ||
@@ -265,3 +280,3 @@ // If no language found, add extension in other section | ||
const name = ext === '' ? path_1.default.basename(file) : ext; | ||
(_h = (_l = results.unknown[unknownType])[name]) !== null && _h !== void 0 ? _h : (_l[name] = 0); | ||
(_p = (_s = results.unknown[unknownType])[name]) !== null && _p !== void 0 ? _p : (_s[name] = 0); | ||
results.unknown[unknownType][name] += fileSize; | ||
@@ -273,3 +288,3 @@ results.unknown.bytes += fileSize; | ||
const { type } = langData[lang]; | ||
(_j = (_m = results.languages.results)[lang]) !== null && _j !== void 0 ? _j : (_m[lang] = { type, bytes: 0, color: langData[lang].color }); | ||
(_q = (_t = results.languages.results)[lang]) !== null && _q !== void 0 ? _q : (_t[lang] = { type, bytes: 0, color: langData[lang].color }); | ||
if (opts.childLanguages) | ||
@@ -276,0 +291,0 @@ results.languages.results[lang].parent = langData[lang].group; |
@@ -8,2 +8,3 @@ export declare type LanguageResult = string | null; | ||
export interface Options { | ||
fileContent?: string | string[]; | ||
ignoredFiles?: string[]; | ||
@@ -10,0 +11,0 @@ ignoredLanguages?: Language[]; |
{ | ||
"name": "linguist-js", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "Analyse languages used in a folder. Powered by GitHub Linguist, although it doesn't need to be installed.", | ||
@@ -48,8 +48,8 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/glob-to-regexp": "ts4.4", | ||
"@types/js-yaml": "ts4.4", | ||
"@types/node": "ts4.4", | ||
"@types/glob-to-regexp": "ts4.6", | ||
"@types/js-yaml": "ts4.6", | ||
"@types/node": "ts4.6", | ||
"deep-object-diff": "^1.1.7", | ||
"typescript": "~4.5.5" | ||
"typescript": "~4.6.1-rc" | ||
} | ||
} |
@@ -80,2 +80,3 @@ [![Latest version](https://img.shields.io/github/v/release/Nixinova/Linguist?label=latest%20version&style=flat-square)](https://github.com/Nixinova/Linguist/releases) | ||
- File paths in the output use only forward slashes as delimiters, even on Windows. | ||
- This tool does not work when offline. | ||
- Do not rely on any language classification output from Linguist being unchanged between runs. | ||
@@ -103,2 +104,4 @@ Language data is fetched each run from the latest classifications of [`github-linguist`](https://github.com/github/linguist). | ||
An object containing analyser options. | ||
- `fileContent` (string or string array): | ||
Provides the file content associated with the file name(s) given as `entry` to analyse instead of reading from a folder on disk. | ||
- `ignoredFiles` (string array): | ||
@@ -105,0 +108,0 @@ A list of file path globs to explicitly ignore. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38072
605
177