Skip to content

Commit 6798cfe

Browse files
authored
Add minSize option which moves small languages into other result (#35)
An implementation of a `minSize` option (proposed in #32). This allows filtering the output to only show languages with a resulting size greater than what the user inputs. This is CLI-only and doesn't affect the outputted schema.
1 parent 5890693 commit 6798cfe

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/cli.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ program
2222
.option('-j|--json [bool]', 'Display the output as JSON', false)
2323
.option('-t|--tree <traversal>', 'Which part of the output JSON to display (dot-delimited)')
2424
.option('-F|--listFiles [bool]', 'Whether to list every matching file under the language results', false)
25+
.option('-m|--minSize <size>', 'Minimum file size to show language results for (must have a unit: b, kb, mb, %)')
2526
.option('-q|--quick [bool]', 'Skip complex language analysis (alias for -{A|I|H|S}=false)', false)
2627
.option('-o|--offline [bool]', 'Use packaged data files instead of fetching latest from GitHub', false)
2728
.option('-L|--calculateLines [bool]', 'Calculate lines of code totals', true)
@@ -61,12 +62,41 @@ if (args.analyze) (async () => {
6162
const { files, languages, unknown } = data;
6263
// Print output
6364
if (!args.json) {
64-
const sortedEntries = Object.entries(languages.results).sort((a, b) => a[1].bytes < b[1].bytes ? +1 : -1);
65+
// Ignore languages with a bytes/% size less than the declared min size
66+
if (args.minSize) {
67+
const totalSize = languages.bytes;
68+
const minSizeAmt = parseFloat(args.minSize.replace(/[a-z]+$/i, '')); // '2KB' -> 2
69+
const minSizeUnit = args.minSize.replace(/^\d+/, '').toLowerCase(); // '2KB' -> 'kb'
70+
const conversionFactors: Record<string, (n: number) => number> = {
71+
'b': n => n,
72+
'kb': n => n * 1e3,
73+
'mb': n => n * 1e6,
74+
'%': n => n * totalSize / 100,
75+
};
76+
const minBytesSize = conversionFactors[minSizeUnit](+minSizeAmt);
77+
const other = { bytes: 0, lines: { total: 0, content: 0, code: 0 } };
78+
// Apply specified minimums: delete language results that do not reach the threshold
79+
for (const [lang, data] of Object.entries(languages.results)) {
80+
if (data.bytes < minBytesSize) {
81+
// Add to 'other' count
82+
other.bytes += data.bytes;
83+
other.lines.total += data.lines.total;
84+
other.lines.content += data.lines.content;
85+
other.lines.code += data.lines.code;
86+
// Remove language result
87+
delete languages.results[lang];
88+
}
89+
}
90+
languages.results['Other'] = { ...other, type: null! };
91+
}
92+
93+
const sortedEntries = Object.entries(languages.results).sort((a, b) => (a[1].bytes < b[1].bytes ? +1 : -1));
6594
const totalBytes = languages.bytes;
6695
console.log(`\n Analysed ${files.bytes.toLocaleString()} B from ${files.count} files with linguist-js`);
6796
console.log(`\n Language analysis results: \n`);
6897
let count = 0;
6998
if (sortedEntries.length === 0) console.log(` None`);
99+
70100
// Collate files per language
71101
const filesPerLanguage: Record<string, string[]> = {};
72102
if (args.listFiles) {

0 commit comments

Comments
 (0)