Skip to content

Commit

Permalink
fix(concurrency): handle environments where os.cpus().length returns 0
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadashy committed Aug 22, 2024
1 parent 41de870 commit 7bb6622
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/core/file/fileCollector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { isBinary } from 'istextorbinary';
import jschardet from 'jschardet';
import iconv from 'iconv-lite';
import pMap from 'p-map';
import { logger } from '../../shared/logger.js';
import { getProcessConcurrency } from '../../shared/processConcurrency.js';
import { RawFile } from './fileTypes.js';

export const collectFiles = async (filePaths: string[], rootDir: string): Promise<RawFile[]> => {
Expand All @@ -20,7 +20,7 @@ export const collectFiles = async (filePaths: string[], rootDir: string): Promis
return null;
},
{
concurrency: os.cpus().length,
concurrency: getProcessConcurrency(),
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/core/file/fileProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'node:os';
import pMap from 'p-map';
import { RepopackConfigMerged } from '../../config/configTypes.js';
import { getProcessConcurrency } from '../../shared/processConcurrency.js';
import { getFileManipulator } from './fileManipulater.js';
import { ProcessedFile, RawFile } from './fileTypes.js';

Expand All @@ -12,7 +12,7 @@ export const processFiles = async (rawFiles: RawFile[], config: RepopackConfigMe
content: await processContent(rawFile.content, rawFile.path, config),
}),
{
concurrency: os.cpus().length,
concurrency: getProcessConcurrency(),
},
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/packager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os from 'node:os';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import pMap from 'p-map';
import { RepopackConfigMerged } from '../config/configTypes.js';
import { getProcessConcurrency } from '../shared/processConcurrency.js';
import { generateOutput as defaultGenerateOutput } from './output/outputGenerator.js';
import { SuspiciousFileResult, runSecurityCheck as defaultRunSecurityCheck } from './security/securityCheckRunner.js';
import { searchFiles as defaultSearchFiles } from './file/fileSearcher.js';
Expand Down Expand Up @@ -73,7 +73,7 @@ export const pack = async (
return { path: file.path, charCount, tokenCount };
},
{
concurrency: os.cpus().length,
concurrency: getProcessConcurrency(),
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/core/security/securityCheckRunner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os from 'node:os';
import type { SecretLintCoreConfig, SecretLintCoreResult } from '@secretlint/types';
import { lintSource } from '@secretlint/core';
import { creator } from '@secretlint/secretlint-rule-preset-recommend';
import pMap from 'p-map';
import { logger } from '../../shared/logger.js';
import { RawFile } from '../file/fileTypes.js';
import { getProcessConcurrency } from '../../shared/processConcurrency.js';

export interface SuspiciousFileResult {
filePath: string;
Expand All @@ -27,7 +27,7 @@ export const runSecurityCheck = async (rawFiles: RawFile[]): Promise<SuspiciousF
return null;
},
{
concurrency: os.cpus().length,
concurrency: getProcessConcurrency(),
},
);

Expand Down
13 changes: 13 additions & 0 deletions src/shared/processConcurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os from 'node:os';

export const getProcessConcurrency = () => {
const cpuCount = os.cpus().length;

// Fallback for environments where os.cpus().length returns 0
// see: https://github.com/yamadashy/repopack/issues/56
if (cpuCount === 0) {
return 1;
}

return cpuCount;
};

0 comments on commit 7bb6622

Please sign in to comment.