Skip to content

Commit ec78248

Browse files
committed
Change input schema
Manual file content is now done as just the first argument.
1 parent c8e0391 commit ec78248

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ const binaryData = JSON.parse(
2020

2121
async function analyse(path?: string, opts?: T.Options): Promise<T.Results>
2222
async function analyse(paths?: string[], opts?: T.Options): Promise<T.Results>
23-
async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Promise<T.Results> {
24-
const useRawContent = opts.fileContent !== undefined;
25-
const input = [rawPaths ?? []].flat();
26-
const manualFileContent = [opts.fileContent ?? []].flat();
23+
async function analyse(content?: Record<string, string>, opts?: T.Options): Promise<T.Results>
24+
async function analyse(rawInput?: string | string[] | Record<string, string>, opts: T.Options = {}): Promise<T.Results> {
25+
const inputs = {
26+
path: typeof rawInput === 'string' ? rawInput : null,
27+
paths: Array.isArray(rawInput) ? rawInput : null,
28+
content: typeof rawInput === 'object' && !Array.isArray(rawInput) ? rawInput : null,
29+
};
30+
const inputPaths = inputs.paths ?? (inputs.path ? [inputs.path] : null);
31+
const inputContent = inputs.content;
32+
const useRawContent = inputContent !== null;
33+
34+
const input = useRawContent ? Object.keys(inputContent) : inputPaths ?? [];
2735

2836
// Normalise input option arguments
2937
opts = {
@@ -233,7 +241,7 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
233241
// Check first line for readability
234242
let firstLine: string | null;
235243
if (useRawContent) {
236-
firstLine = manualFileContent[files.indexOf(file)]?.split('\n')[0] ?? null;
244+
firstLine = inputContent[file]?.split('\n')[0] ?? null;
237245
}
238246
else if (FS.existsSync(file) && !FS.lstatSync(file).isDirectory()) {
239247
firstLine = await readFileChunk(file, true).catch(() => null);
@@ -353,7 +361,7 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
353361
}
354362

355363
// Check file contents and apply heuristic patterns
356-
const fileContent = opts.fileContent ? manualFileContent[files.indexOf(file)] : await readFileChunk(file).catch(() => null);
364+
const fileContent = useRawContent ? inputContent[file] : await readFileChunk(file).catch(() => null);
357365

358366
// Skip if file read errors
359367
if (fileContent === null) continue;
@@ -423,11 +431,11 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
423431
for (const [file, lang] of Object.entries(results.files.results)) {
424432
if (lang && !langData[lang]) continue;
425433
// Calculate file size
426-
const fileSize = manualFileContent[files.indexOf(file)]?.length ?? FS.statSync(file).size;
434+
const fileSize = useRawContent ? inputContent[file]?.length : FS.statSync(file).size;
427435
// Calculate lines of code
428436
const loc = { total: 0, content: 0 };
429437
if (opts.calculateLines) {
430-
const fileContent = (manualFileContent[files.indexOf(file)] ?? FS.readFileSync(file).toString()) ?? '';
438+
const fileContent = useRawContent ? inputContent[file] : FS.readFileSync(file).toString();
431439
const allLines = fileContent.split(/\r?\n/gm);
432440
loc.total = allLines.length;
433441
loc.content = allLines.filter(line => line.trim().length > 0).length;

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type AbsFolder = string & {}
1111
export type FileGlob = string & {}
1212

1313
export interface Options {
14-
fileContent?: string | string[]
1514
ignoredFiles?: string[]
1615
ignoredLanguages?: Language[]
1716
categories?: Category[]

test/unit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function desc(text) {
88
}
99

1010
async function test([filename, fileContent = ''], [type, testVal]) {
11-
const actual = await linguist(filename, { fileContent, childLanguages: true });
11+
const actual = await linguist({ [filename]: fileContent }, { childLanguages: true });
1212
const testContent = {
1313
'files': actual.files.results[filename],
1414
'size': actual.files.bytes,

0 commit comments

Comments
 (0)