From 620abc7def8bfcb81da28370142577be526299c2 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 14 Nov 2023 11:13:21 +0100 Subject: [PATCH] Load package.json files synchronously --- packages/bridge/src/worker.js | 2 +- .../src/dependencies/package-json/index.ts | 4 ++-- .../package-json/project-package-json.ts | 14 +++++++------- packages/jsts/tests/analysis/analyzer.test.ts | 2 +- packages/jsts/tests/dependencies/index.test.ts | 18 +++++++++--------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/bridge/src/worker.js b/packages/bridge/src/worker.js index 69bd44b4651..56beeb1f491 100644 --- a/packages/bridge/src/worker.js +++ b/packages/bridge/src/worker.js @@ -139,7 +139,7 @@ if (parentPort) { case 'on-init-linter': { const { rules, environments, globals, linterId, baseDir, exclusions } = data; initializeLinter(rules, environments, globals, linterId); - await searchPackageJsonFiles(baseDir, exclusions); + searchPackageJsonFiles(baseDir, exclusions); parentThread.postMessage({ type: 'success', result: 'OK!' }); break; } diff --git a/packages/jsts/src/dependencies/package-json/index.ts b/packages/jsts/src/dependencies/package-json/index.ts index 0e1a7d233d9..fc8dcf2fc1d 100644 --- a/packages/jsts/src/dependencies/package-json/index.ts +++ b/packages/jsts/src/dependencies/package-json/index.ts @@ -22,8 +22,8 @@ import { PackageJsons } from './project-package-json'; const PackageJsonsByBaseDir = new PackageJsons(); -async function searchPackageJsonFiles(baseDir: string, exclusions: string[]) { - await PackageJsonsByBaseDir.searchPackageJsonFiles(baseDir, exclusions); +function searchPackageJsonFiles(baseDir: string, exclusions: string[]) { + PackageJsonsByBaseDir.searchPackageJsonFiles(baseDir, exclusions); } function getNearestPackageJsons(file: string) { diff --git a/packages/jsts/src/dependencies/package-json/project-package-json.ts b/packages/jsts/src/dependencies/package-json/project-package-json.ts index fbcafcdc5a7..cdfd6ea1ccc 100644 --- a/packages/jsts/src/dependencies/package-json/project-package-json.ts +++ b/packages/jsts/src/dependencies/package-json/project-package-json.ts @@ -17,9 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import fs from 'fs/promises'; +import fs from 'fs'; import path from 'path'; -import { toUnixPath, debug, error, readFile } from '@sonar/shared/helpers'; +import { toUnixPath, debug, error, readFileSync } from '@sonar/shared/helpers'; import { PackageJson as PJ } from 'type-fest'; import { Minimatch } from 'minimatch'; @@ -48,25 +48,25 @@ export class PackageJsons { const patterns = exclusions .concat(IGNORED_PATTERNS) .map(exclusion => new Minimatch(exclusion)); - await this.walkDirectory(path.posix.normalize(toUnixPath(dir)), patterns); + this.walkDirectory(path.posix.normalize(toUnixPath(dir)), patterns); } catch (e) { error(`Error while searching for package.json files: ${e}`); } } - async walkDirectory(dir: string, ignoredPatterns: Minimatch[]) { - const files = await fs.readdir(dir, { withFileTypes: true }); + walkDirectory(dir: string, ignoredPatterns: Minimatch[]) { + const files = fs.readdirSync(dir, { withFileTypes: true }); for (const file of files) { const filename = path.posix.join(dir, file.name); if (ignoredPatterns.some(pattern => pattern.match(filename))) { continue; // is ignored pattern } if (file.isDirectory()) { - await this.walkDirectory(filename, ignoredPatterns); + this.walkDirectory(filename, ignoredPatterns); } else if (file.name.toLowerCase() === PACKAGE_JSON && !file.isDirectory()) { try { debug(`Found package.json: ${filename}`); - const contents = JSON.parse(await readFile(filename)); + const contents = JSON.parse(readFileSync(filename)); this.db.set(dir, { filename, contents }); } catch (e) { debug(`Error reading file ${filename}: ${e}`); diff --git a/packages/jsts/tests/analysis/analyzer.test.ts b/packages/jsts/tests/analysis/analyzer.test.ts index 4c018a6a6e7..75705973ad0 100644 --- a/packages/jsts/tests/analysis/analyzer.test.ts +++ b/packages/jsts/tests/analysis/analyzer.test.ts @@ -899,7 +899,7 @@ describe('analyzeJSTS', () => { it('package.json should be available in rule context', async () => { const baseDir = path.join(__dirname, 'fixtures', 'package-json'); - await searchPackageJsonFiles(baseDir, []); + searchPackageJsonFiles(baseDir, []); const linter = new Linter(); linter.defineRule('custom-rule-file', { diff --git a/packages/jsts/tests/dependencies/index.test.ts b/packages/jsts/tests/dependencies/index.test.ts index c210e78b22c..e33cb725313 100644 --- a/packages/jsts/tests/dependencies/index.test.ts +++ b/packages/jsts/tests/dependencies/index.test.ts @@ -32,9 +32,9 @@ describe('initialize package.json files', () => { getAllPackageJsons().clear(); }); - it('should find all package.json files', async () => { + it('should find all package.json files', () => { const baseDir = path.posix.join(toUnixPath(__dirname), 'fixtures'); - await searchPackageJsonFiles(baseDir, []); + searchPackageJsonFiles(baseDir, []); expect(getAllPackageJsons().size).toEqual(7); const basePJList = getNearestPackageJsons(path.posix.join(baseDir, 'index.js')); @@ -105,10 +105,10 @@ describe('initialize package.json files', () => { expect(fakeFilePJList[0].filename).toEqual(moduleBsubmoduleBPJ); }); - it('should ignore package.json files from ignored patterns', async () => { + it('should ignore package.json files from ignored patterns', () => { const baseDir = path.posix.join(toUnixPath(__dirname), 'fixtures'); - await searchPackageJsonFiles(baseDir, ['**/moduleA/**']); + searchPackageJsonFiles(baseDir, ['**/moduleA/**']); expect(getAllPackageJsons().size).toEqual(4); const packageJsons = [ ['package.json'], @@ -126,7 +126,7 @@ describe('initialize package.json files', () => { ); getAllPackageJsons().clear(); - await searchPackageJsonFiles(baseDir, ['**/module*/**']); + searchPackageJsonFiles(baseDir, ['**/module*/**']); expect(getAllPackageJsons().size).toEqual(1); expect(getAllPackageJsons()).toEqual( new Map([ @@ -138,7 +138,7 @@ describe('initialize package.json files', () => { ); }); - it('should return empty array when no package.json are in the DB or none exist in the file tree', async () => { + it('should return empty array when no package.json are in the DB or none exist in the file tree', () => { const baseDir = path.posix.join(toUnixPath(__dirname), 'fixtures'); expect(getAllPackageJsons().size).toEqual(0); @@ -146,14 +146,14 @@ describe('initialize package.json files', () => { getNearestPackageJsons(path.posix.join(baseDir, '..', 'another-module', 'index.js')), ).toHaveLength(0); - await searchPackageJsonFiles(baseDir, ['']); + searchPackageJsonFiles(baseDir, ['']); expect(getAllPackageJsons().size).toEqual(7); expect( getNearestPackageJsons(path.posix.join(baseDir, '..', 'another-module', 'index.js')), ).toHaveLength(0); }); - it('should log error when cannot access baseDir', async () => { + it('should log error when cannot access baseDir', () => { const baseDir = path.posix.join(toUnixPath(__dirname), 'fixtures'); console.error = jest.fn(); @@ -162,7 +162,7 @@ describe('initialize package.json files', () => { throw Error(`Cannot access ${dir}`); }); - await searchPackageJsonFiles(baseDir, ['']); + searchPackageJsonFiles(baseDir, ['']); expect(console.error).toHaveBeenCalledWith( `Error while searching for package.json files: Error: Cannot access ${baseDir}`, );