Skip to content

Commit

Permalink
Fix yarn list --json stdOut parsing. fixes #388
Browse files Browse the repository at this point in the history
  • Loading branch information
asprouse committed Jun 19, 2019
1 parent 2139384 commit 6cb147b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/packagers/yarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class Yarn {
return BbPromise.reject(err);
})
.then(processOutput => processOutput.stdout)
.then(depJson => BbPromise.try(() => JSON.parse(depJson)))
.then(stdout =>
BbPromise.try(() => {
const lines = Utils.splitLines(stdout);
const parsedLines = _.map(lines, Utils.safeJsonParse);
return _.find(parsedLines, line => line && line.type === 'tree');
})
)
.then(parsedTree => {
const convertTrees = trees =>
_.reduce(
Expand Down
24 changes: 16 additions & 8 deletions lib/packagers/yarn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ describe('yarn', () => {
});

it('should transform yarn trees to npm dependencies', () => {
const testYarnResult = `{"type":"tree","data":{"type":"list","trees":[
{"name":"[email protected]","children":[],"hint":null,"color":"bold",
"depth":0},{"name":"[email protected]","children":[],"hint":null,"color":
"bold","depth":0},{"name":"[email protected]","children":[],"hint":null,
"color":"bold","depth":0},{"name":"[email protected]","children":[{"name":
"[email protected]","children":[],"hint":null,"color":"bold","depth":0}],
"hint":null,"color":null,"depth":0},{"name":"@sls/[email protected]",
"children":[],"hint":null,"color":"bold","depth":0}]}}`;
const testYarnResult =
'{"type":"activityStart","data":{"id":0}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"archiver@^2.1.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"bluebird@^3.5.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"fs-extra@^4.0.3"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"mkdirp@^0.5.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"minimist@^0.0.8"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"@sls/webpack@^1.0.0"}}\n' +
'{"type":"tree","data":{"type":"list","trees":[' +
'{"name":"[email protected]","children":[],"hint":null,"color":"bold",' +
'"depth":0},{"name":"[email protected]","children":[],"hint":null,"color":' +
'"bold","depth":0},{"name":"[email protected]","children":[],"hint":null,' +
'"color":"bold","depth":0},{"name":"[email protected]","children":[{"name":' +
'"[email protected]","children":[],"hint":null,"color":"bold","depth":0}],' +
'"hint":null,"color":null,"depth":0},{"name":"@sls/[email protected]",' +
'"children":[],"hint":null,"color":"bold","depth":0}]}}\n';
const expectedResult = {
problems: [],
dependencies: {
Expand Down
16 changes: 15 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,24 @@ function spawnProcess(command, args, options) {
});
}

function safeJsonParse(str) {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
}

function splitLines(str) {
return _.split(str, /\r?\n/);
}

module.exports = {
guid,
purgeCache,
searchAndProcessCache,
SpawnError,
spawnProcess
spawnProcess,
safeJsonParse,
splitLines
};
16 changes: 16 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@ describe('Utils', () => {
return expect(Utils.spawnProcess('cmd', [])).to.be.rejectedWith(Utils.SpawnError);
});
});

describe('safeJsonParse', () => {
it('should parse valid JSON', () => {
expect(Utils.safeJsonParse('{"foo": "bar"}')).to.deep.equal({ foo: 'bar' });
});

it('should return null for invalid JSON', () => {
expect(Utils.safeJsonParse('{"foo":')).to.equal(null);
});
});

describe('splitLines', () => {
it('should split on new line characters', () => {
expect(Utils.splitLines('a\r\nb\nc')).to.deep.equal([ 'a', 'b', 'c' ]);
});
});
});

0 comments on commit 6cb147b

Please sign in to comment.