Skip to content

Commit

Permalink
Load package.json files synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez committed Nov 14, 2023
1 parent ba281a1 commit 620abc7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/bridge/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/dependencies/package-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/tests/analysis/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
18 changes: 9 additions & 9 deletions packages/jsts/tests/dependencies/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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'],
Expand All @@ -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([
Expand All @@ -138,22 +138,22 @@ 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);
expect(
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();
Expand All @@ -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}`,
);
Expand Down

0 comments on commit 620abc7

Please sign in to comment.