Skip to content

Commit afdb8fe

Browse files
authored
fix: better errors from reading and parskign package.json (derberg#2)
1 parent 6e3b985 commit afdb8fe

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

dist/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11452,8 +11452,14 @@ async function run() {
1145211452

1145311453
for (const {path: filepath, repository: { name, html_url, node_id }} of reposList) {
1145411454
if (ignoredRepositories.includes(name)) continue;
11455+
//Sometimes there might be files like package.json.js or similar as the repository might contain some templated package.json files that cannot be parsed from string to JSON
11456+
//Such files must be ignored
11457+
if (filepath.substring(filepath.lastIndexOf('/') + 1) !== 'package.json') {
11458+
core.info(`Ignoring ${filepath} from ${name} repo as only package.json files are supported`);
11459+
continue;
11460+
}
1145511461

11456-
const cloneDir = path.join(process.cwd(), './clones', name);
11462+
const cloneDir = __webpack_require__.ab + "clones/" + name;
1145711463
await mkdir(cloneDir, {recursive: true});
1145811464

1145911465
const branchName = `bot/bump-${dependencyName}-${dependencyVersion}`;
@@ -12111,9 +12117,19 @@ module.exports = { readPackageJson, parseCommaList, verifyDependencyType, instal
1211112117
* @returns parsed package.json
1211212118
*/
1211312119
async function readPackageJson(path) {
12114-
return JSON.parse(
12115-
await readFile(path, 'utf8')
12116-
);
12120+
let packageFile, parsedFile;
12121+
12122+
try {
12123+
packageFile = await readFile(path, 'utf8');
12124+
} catch (e) {
12125+
throw new Error(`There was a problem reading the package.json file from ${path}`, e);
12126+
}
12127+
try {
12128+
parsedFile = JSON.parse(packageFile);
12129+
} catch (e) {
12130+
throw new Error(`There was a problem parsing the package.json file from ${path} with the following content: ${packageFile}`);
12131+
}
12132+
return parsedFile;
1211712133
}
1211812134

1211912135
/**

lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ async function run() {
4040

4141
for (const {path: filepath, repository: { name, html_url, node_id }} of reposList) {
4242
if (ignoredRepositories.includes(name)) continue;
43+
//Sometimes there might be files like package.json.js or similar as the repository might contain some templated package.json files that cannot be parsed from string to JSON
44+
//Such files must be ignored
45+
if (filepath.substring(filepath.lastIndexOf('/') + 1) !== 'package.json') {
46+
core.info(`Ignoring ${filepath} from ${name} repo as only package.json files are supported`);
47+
continue;
48+
}
4349

4450
const cloneDir = path.join(process.cwd(), './clones', name);
4551
await mkdir(cloneDir, {recursive: true});

lib/utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ module.exports = { readPackageJson, parseCommaList, verifyDependencyType, instal
88
* @returns parsed package.json
99
*/
1010
async function readPackageJson(path) {
11-
return JSON.parse(
12-
await readFile(path, 'utf8')
13-
);
11+
let packageFile, parsedFile;
12+
13+
try {
14+
packageFile = await readFile(path, 'utf8');
15+
} catch (e) {
16+
throw new Error(`There was a problem reading the package.json file from ${path}`, e);
17+
}
18+
try {
19+
parsedFile = JSON.parse(packageFile);
20+
} catch (e) {
21+
throw new Error(`There was a problem parsing the package.json file from ${path} with the following content: ${packageFile}`);
22+
}
23+
return parsedFile;
1424
}
1525

1626
/**

0 commit comments

Comments
 (0)