Skip to content

Commit

Permalink
Fix yarn list --json stdOut parsing. fixes serverless-heaven#388
Browse files Browse the repository at this point in the history
  • Loading branch information
asprouse committed Jun 14, 2019
1 parent eaee022 commit 948b6e3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
14 changes: 10 additions & 4 deletions lib/packagers/yarn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
/**
* Yarn packager.
*
*
* Yarn specific packagerOptions (default):
* flat (false) - Use --flat with install
* ignoreScripts (false) - Do not execute scripts during install
Expand Down Expand Up @@ -58,7 +58,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(trees, (__, tree) => {
const splitModule = _.split(tree.name, '@');
Expand All @@ -77,7 +83,7 @@ class Yarn {
const trees = _.get(parsedTree, 'data.trees', []);
const result = {
problems: [],
dependencies: convertTrees(trees)
dependencies: convertTrees(trees)
};
return result;
});
Expand All @@ -87,7 +93,7 @@ class Yarn {
const fileVersionMatcher = /[^"/]@(?:file:)?((?:\.\/|\.\.\/).*?)[":,]/gm;
const replacements = [];
let match;

// Detect all references and create replacement line strings
while ((match = fileVersionMatcher.exec(lockfile)) !== null) {
replacements.push({
Expand Down
28 changes: 18 additions & 10 deletions lib/packagers/yarn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,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 Expand Up @@ -198,7 +206,7 @@ describe('yarn', () => {
version "5.5.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
`;

expect(yarnModule.rebaseLockfile('../../project', testContent)).to.equal(expectedContent);
});
});
Expand Down Expand Up @@ -286,4 +294,4 @@ describe('yarn', () => {
});
});

});
});
16 changes: 15 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function guid() {

/**
* Remove the specified module from the require cache.
* @param {string} moduleName
* @param {string} moduleName
*/
function purgeCache(moduleName) {
return searchAndProcessCache(moduleName, function (mod) {
Expand Down Expand Up @@ -97,10 +97,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,
safeJsonParse,
splitLines,
};
16 changes: 16 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,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 948b6e3

Please sign in to comment.