Skip to content

Commit

Permalink
fix: wrong duplicates (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonBall authored and tabrindle committed Apr 15, 2019
1 parent a6a8f90 commit 9a12ed6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
!__tests__/packages/**/node_modules
.DS_Store
.idea/
22 changes: 22 additions & 0 deletions __tests__/__snapshots__/duplicates.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Running the programmatic interface return expected duplicates in json 1`] = `
Object {
"npmPackages": Object {
"b": Object {
"duplicates": Array [
"1.0.2",
],
"installed": "1.0.0",
"wanted": "1.0.0",
},
},
}
`;

exports[`Running the programmatic interface return expected duplicates in yaml 1`] = `
"
npmPackages:
b: 1.0.0 => 1.0.0 (1.0.2)
"
`;
26 changes: 26 additions & 0 deletions __tests__/duplicates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const envinfo = require('../src/envinfo');
const path = require('path');

describe('Running the programmatic interface', () => {
test('return expected duplicates in json', async () => {
const cwd = process.cwd();
process.chdir(path.join(__dirname, 'packages/test-duplicates'));
try {
const data = await envinfo.run({ npmPackages: ['b'] }, { duplicates: true, json: true });
expect(JSON.parse(data)).toMatchSnapshot();
} finally {
process.chdir(cwd);
}
});

test('return expected duplicates in yaml', async () => {
const cwd = process.cwd();
process.chdir(path.join(__dirname, 'packages/test-duplicates'));
try {
const data = await envinfo.run({ npmPackages: ['b'] }, { duplicates: true });
expect(data).toMatchSnapshot();
} finally {
process.chdir(cwd);
}
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions __tests__/packages/test-duplicates/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-duplicates",
"version": "1.0.0",
"dependencies": {
"a": "1.0.0",
"b": "1.0.0"
}
}
25 changes: 16 additions & 9 deletions src/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,33 @@ function getnpmPackages(packages, options) {
const paths = result[0];
const files = result[1];

return files.reduce((acc, d, idx) => {
const versions = files.reduce((acc, d, idx) => {
// if the file is a test stub, or doesn't have a name, ignore it.
if (!d || !d.name) return acc;
// create object if its not already created
if (!acc[d.name]) acc[d.name] = {};
// set duplicates if flag set, if version not already there, && !== installed
// set duplicates if flag set, if version not already there
if (options.duplicates) {
if (acc[d.name].installed && acc[d.name].installed !== d.version) {
utils.uniq(
(acc[d.name].duplicates = (acc[d.name].duplicates || []).concat(d.version))
);
}
acc[d.name].duplicates = utils.uniq((acc[d.name].duplicates || []).concat(d.version));
}
// set the installed version, if its installed top level
if ((paths[idx].match(/node_modules/g) || []).length === 1)
acc[d.name].installed = d.version;
// if it is a top level dependency, get the wanted version
if (tld[d.name]) acc[d.name].wanted = tld[d.name];
return acc;
}, {});

Object.keys(versions).forEach(name => {
// update duplicates, only !== installed
if (versions[name].duplicates && versions[name].installed) {
versions[name].duplicates = versions[name].duplicates.filter(
v => v !== versions[name].installed
);
}
// if it is a top level dependency, get the wanted version
if (tld[name]) versions[name].wanted = tld[name];
});

return versions;
})
.then(versions => {
if (options.showNotFound && Array.isArray(packages)) {
Expand Down

0 comments on commit 9a12ed6

Please sign in to comment.