Skip to content

Commit

Permalink
Check for package.json exists before reading (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 10, 2024
1 parent 9ce31c8 commit df5b3ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-mugs-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Check for package.json exists before reading
12 changes: 6 additions & 6 deletions src/utils/getJsCodeshiftParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// @ts-nocheck

import { readFileSync } from "fs";
import { existsSync, readFileSync } from "fs";
import { dirname, join } from "path";
import { DEFAULT_EXTENSIONS } from "@babel/core";
import argsParser from "jscodeshift/dist/argsParser";
Expand All @@ -15,11 +15,11 @@ const requirePackage = (name: string) => {
const entry = require.resolve(name);
let dir = dirname(entry);
while (dir !== "/") {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require(join(dir, "package.json"));
return pkg.name === name ? pkg : {};
} catch (error) {} // eslint-disable-line no-empty
const packageJsonPath = join(dir, "package.json");
if (existsSync(packageJsonPath)) {
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
return packageJson.name === name ? packageJson : {};
}
dir = dirname(dir);
}
return {};
Expand Down

0 comments on commit df5b3ab

Please sign in to comment.