Skip to content

Commit bc50177

Browse files
committed
Fix JSON parsing to log npm warnings without crashing
1 parent 300cac9 commit bc50177

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

dist/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29979,7 +29979,26 @@ const getTestStatus = (uploadId, apiKey, dcdVersionString, apiUrl) => __awaiter(
2997929979
command += ` --api-url ${escapeShellValue(apiUrl)}`;
2998029980
}
2998129981
const { output } = yield executeCommand(command, false);
29982-
return JSON.parse(output);
29982+
// Filter out non-JSON lines (e.g., npm warnings)
29983+
// Find the first line that starts with '{' or '[' (JSON start)
29984+
const lines = output.split('\n');
29985+
const jsonStartIndex = lines.findIndex(line => {
29986+
const trimmed = line.trim();
29987+
return trimmed.startsWith('{') || trimmed.startsWith('[');
29988+
});
29989+
if (jsonStartIndex === -1) {
29990+
throw new Error('No JSON found in output');
29991+
}
29992+
// Log any warnings that appeared before the JSON output
29993+
if (jsonStartIndex > 0) {
29994+
const warnings = lines.slice(0, jsonStartIndex).join('\n').trim();
29995+
if (warnings) {
29996+
console.warn('npm warnings during status check:\n', warnings);
29997+
}
29998+
}
29999+
// Join from the JSON start to the end
30000+
const jsonOutput = lines.slice(jsonStartIndex).join('\n');
30001+
return JSON.parse(jsonOutput);
2998330002
}
2998430003
catch (error) {
2998530004
console.warn('Failed to get test status:', error);

src/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,30 @@ const getTestStatus = async (
7171
command += ` --api-url ${escapeShellValue(apiUrl)}`;
7272
}
7373
const { output } = await executeCommand(command, false);
74-
return JSON.parse(output);
74+
75+
// Filter out non-JSON lines (e.g., npm warnings)
76+
// Find the first line that starts with '{' or '[' (JSON start)
77+
const lines = output.split('\n');
78+
const jsonStartIndex = lines.findIndex(line => {
79+
const trimmed = line.trim();
80+
return trimmed.startsWith('{') || trimmed.startsWith('[');
81+
});
82+
83+
if (jsonStartIndex === -1) {
84+
throw new Error('No JSON found in output');
85+
}
86+
87+
// Log any warnings that appeared before the JSON output
88+
if (jsonStartIndex > 0) {
89+
const warnings = lines.slice(0, jsonStartIndex).join('\n').trim();
90+
if (warnings) {
91+
console.warn('npm warnings during status check:\n', warnings);
92+
}
93+
}
94+
95+
// Join from the JSON start to the end
96+
const jsonOutput = lines.slice(jsonStartIndex).join('\n');
97+
return JSON.parse(jsonOutput);
7598
} catch (error) {
7699
console.warn('Failed to get test status:', error);
77100
return null;

0 commit comments

Comments
 (0)