Skip to content

Commit

Permalink
fix(monorepo): avoid fetching infos for internal dependencies
Browse files Browse the repository at this point in the history
yarn monorepos are identified by packageJson.workspaceRoot.
if we are handling one of those, we might encounter a [sub-]
package.json that specifies a dependency like this:
"@reponame/dependency": "*" which looks like a scoped npm
dependency, but is in reality a in-monorepo dependency.
For those dependencies, we should not try to fetch infos
from npm, because the dependencies are not there.
  • Loading branch information
janl authored and hulkoba committed Jul 15, 2019
1 parent c5b9a15 commit 3d2dfac
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/utils/__snapshots__/initial-branch-utils.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`initial branch utils getDependenciesFromPackageFiles duplicate dependency across dep type 1`] = `
Array [
Object {
"name": "@finnpauls/dep",
"type": "devDependencies",
"version": "1.0.0",
},
]
`;

exports[`initial branch utils getDependenciesFromPackageFiles single devDependency 1`] = `
Array [
Object {
"name": "@finnpauls/dep",
"type": "devDependencies",
"version": "1.0.0",
},
]
`;

exports[`initial branch utils getDependenciesFromPackageFiles workspace 1`] = `
Array [
Object {
"name": "@finnpauls/blup",
"type": "devDependencies",
"version": "1.0.0",
},
Object {
"name": "florp",
"type": "dependencies",
"version": "1.2.3",
},
]
`;
42 changes: 42 additions & 0 deletions test/utils/initial-branch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,45 @@ describe('create initial branch', () => {
expect(updatedDependencies[0].newVersion).toEqual('2.0.0')
})
})

/*
function getDependenciesFromPackageFiles (packagePaths, packageJsonContents) {
*/

const { getDependenciesFromPackageFiles } = require('../../utils/initial-branch-utils')
describe('initial branch utils', () => {
test('getDependenciesFromPackageFiles single devDependency', () => {
const paths = [
'foobar/package.json'
]
const contents = { 'foobar/package.json': { devDependencies: { '@finnpauls/dep': '1.0.0' } } }
const result = getDependenciesFromPackageFiles(paths, contents)
expect(result).toMatchSnapshot()
})

test('getDependenciesFromPackageFiles duplicate dependency across dep type', () => {
const paths = [
'foobar/package.json',
'bazquux/package.json'
]
const contents = {
'foobar/package.json': { devDependencies: { '@finnpauls/dep': '1.0.0' } },
'bazquux/package.json': { devDependencies: { '@finnpauls/dep': '1.0.0' } }
}
const result = getDependenciesFromPackageFiles(paths, contents)
expect(result).toMatchSnapshot()
})

test('getDependenciesFromPackageFiles workspace', () => {
const paths = [
'package.json',
'bazquux/package.json'
]
const contents = {
'package.json': { devDependencies: { '@finnpauls/blup': '1.0.0' }, workspaceRoot: 'bazquux/' },
'bazquux/package.json': { devDependencies: { '@finnpauls/dep': '*' }, dependencies: { 'florp': '1.2.3' } }
}
const result = getDependenciesFromPackageFiles(paths, contents)
expect(result).toMatchSnapshot()
})
})
14 changes: 14 additions & 0 deletions utils/initial-branch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ const registryUrl = env.NPM_REGISTRY
}]
*/
function getDependenciesFromPackageFiles (packagePaths, packageJsonContents) {
/*
yarn monorepos are identified by packageJson.workspaceRoot.
if we are handling one of those, we might encounter a [sub-]
package.json that specifies a dependency like this:
"@reponame/dependency": "*" which looks like a scoped npm
dependency, but is in reality a in-monorepo dependency.
For those dependencies, we should not try to fetch infos
from npm, because the dependencies are not there.
*/
const isMonorepo = !!_.get(packageJsonContents['package.json'], 'workspaceRoot')
const isMonorepoStar = ({ name, version, type }) => {
return !(isMonorepo && version === '*')
}
return _.compact(_.uniqWith(_.flatten(packagePaths.map(path => {
return _.flatten(
['dependencies', 'devDependencies', 'optionalDependencies'].map(type => {
if (packageJsonContents[path]) {
return _.map(packageJsonContents[path][type], (version, name) => ({ name, version, type }))
.filter(isMonorepoStar)
}
})
)
Expand Down

0 comments on commit 3d2dfac

Please sign in to comment.