Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions packages/plugin-js-packages/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,13 @@ async function processOutdated(
packageJsonPaths: PackageJsonPaths,
) {
const pm = packageManagers[id];
const { stdout, stderr } = await executeProcess({
const { stdout } = await executeProcess({
command: pm.command,
args: pm.outdated.commandArgs,
cwd: process.cwd(),
ignoreExitCode: true, // outdated returns exit code 1 when outdated dependencies are found
});

// Successful outdated check has empty stderr
if (stderr) {
throw new Error(`JS packages plugin: outdated error: ${stderr}`);
}

// Locate all package.json files in the repository if not provided
const finalPaths = Array.isArray(packageJsonPaths)
? packageJsonPaths
Expand Down Expand Up @@ -122,16 +117,12 @@ async function processAudit(
const auditResults = await Promise.allSettled(
compatibleAuditDepGroups.map(
async (depGroup): Promise<[DependencyGroup, AuditResult]> => {
const { stdout, stderr } = await executeProcess({
const { stdout } = await executeProcess({
command: pm.command,
args: pm.audit.getCommandArgs(depGroup),
cwd: process.cwd(),
ignoreExitCode: pm.audit.ignoreExitCode,
});
// Successful audit check has empty stderr
if (stderr) {
throw new Error(`JS packages plugin: audit error: ${stderr}`);
}
return [depGroup, pm.audit.unifyResult(stdout)];
},
),
Expand Down
14 changes: 12 additions & 2 deletions packages/utils/src/lib/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,19 @@ export function toUnixNewlines(text: string): string {
return platform() === 'win32' ? text.replace(/\r\n/g, '\n') : text;
}

export function fromJsonLines<T = unknown>(jsonLines: string) {
export function fromJsonLines<T extends unknown[]>(jsonLines: string) {
const unifiedNewLines = toUnixNewlines(jsonLines).trim();
return JSON.parse(`[${unifiedNewLines.split('\n').join(',')}]`) as T;
const invalid = Symbol('invalid json');
return unifiedNewLines
.split('\n')
.map(line => {
try {
return JSON.parse(line);
} catch {
return invalid;
}
})
.filter(line => line !== invalid) as T;
}

export function toJsonLines<T>(json: T[]) {
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/src/lib/transform.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ describe('JSON lines format', () => {

expect(fromJsonLines(jsonLines)).toEqual([head, body]);
});

it('should ignore non-JSON lines', () => {
const jsonLines = [
'(node:346640) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.',
'(Use `node --trace-deprecation ...` to show where the warning was created)',
JSON.stringify(head),
JSON.stringify(body),
].join('\n');

expect(fromJsonLines(jsonLines)).toEqual([head, body]);
});
});

describe('toJsonLines', () => {
Expand Down
Loading