Skip to content

Commit 19aa402

Browse files
committed
feat: [OSM-2746] Removed support for non-JSON platform deps parsing
1 parent d5325db commit 19aa402

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

lib/composer-cmds.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import * as path from 'path';
22
import * as childProcess from 'child_process';
33

4-
export const composerVersionCmd = {command: 'composer', args: ['--version']};
5-
export const composerShowCmd = {command: 'composer', args: ['show', '-p']};
6-
export const pharVersionCmd = {
7-
command: `php`,
8-
args: [`${path.resolve(path.resolve() + '/composer.phar')}`, '--version']
9-
};
10-
export const pharShowCmd = {
11-
command: `php`,
12-
args: [`${path.resolve(path.resolve() + '/composer.phar')}`, 'show', '-p', '--format=json']
13-
};
4+
export const composerCmd = {command: 'composer', args: []};
5+
export const composerPharCmd = {command: 'php', args: [`${path.resolve(path.resolve() + '/composer.phar')}`]};
6+
7+
export const versionArgs = {args: ['--version']};
8+
export const showArgs = {args: ['show', '-p', '--format=json']};
149

1510
function cleanUpComposerWarnings(composerOutput: string): string {
16-
// Remove all lines preceding the JSON data; including Deprecation messages and blank lines.
11+
// Remove all lines preceding the JSON data; including "Deprecated" messages and blank lines.
1712
const lines = composerOutput.split('\n');
18-
const jsonStartIndex = lines.findIndex((line) => line.length > 0 && !line.startsWith('Deprecated:'));
13+
const jsonStartIndex = lines.findIndex((line) => line.startsWith('{'));
1914

2015
return lines.slice(jsonStartIndex).join('\n');
2116
}

lib/system-deps.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
import * as os from 'os';
2-
import { SystemPackages } from '@snyk/composer-lockfile-parser';
1+
import {SystemPackages} from '@snyk/composer-lockfile-parser';
32

43
import * as cmds from './composer-cmds';
5-
import { PhpOptions } from './types';
4+
import {PhpOptions} from './types';
65

7-
function isSet(variable): boolean {
6+
function isSet(variable: boolean | undefined): boolean {
87
return typeof variable !== 'undefined';
98
}
109

1110
export function systemDeps(basePath: string, options: PhpOptions): SystemPackages {
1211
const composerOk = isSet(options.composerIsFine) ?
13-
options.composerIsFine : cmds.cmdReturnsOk(cmds.composerVersionCmd.command, cmds.composerVersionCmd.args);
12+
options.composerIsFine : cmds.cmdReturnsOk(cmds.composerCmd.command, [...cmds.composerCmd.args, ...cmds.versionArgs.args]);
1413
const composerPharOk = isSet(options.composerPharIsFine) ?
15-
options.composerPharIsFine : cmds.cmdReturnsOk(cmds.pharVersionCmd.command, cmds.pharVersionCmd.args);
14+
options.composerPharIsFine : cmds.cmdReturnsOk(cmds.composerPharCmd.command, [...cmds.composerPharCmd.args, ...cmds.versionArgs.args]);
1615

1716
let finalVersionsObj = {};
1817

1918
if (options.systemVersions && (Object.keys(options.systemVersions).length > 0)) {
2019
// give first preference to a stub
2120
finalVersionsObj = options.systemVersions;
22-
} else if (composerOk) {
23-
const lines = cmds.execWithResult(cmds.composerShowCmd.command, basePath, cmds.composerShowCmd.args).split(os.EOL);
24-
lines.forEach((line) => {
25-
const [part1, part2] = line.split(/\s+/);
26-
if (part2) {
27-
finalVersionsObj[part1] = part2;
28-
}
29-
});
30-
} else if (composerPharOk) {
31-
const output = cmds.execWithResult(cmds.pharShowCmd.command, basePath, cmds.pharShowCmd.args);
21+
} else if (composerOk || composerPharOk) {
22+
const composer = composerOk ? cmds.composerCmd : cmds.composerPharCmd;
23+
24+
const output = cmds.execWithResult(composer.command, basePath, [...composer.args, ...cmds.showArgs.args]);
3225
const versionsObj = JSON.parse(output).platform;
3326

3427
versionsObj.forEach(({name, version}) => {

0 commit comments

Comments
 (0)