Skip to content

Commit

Permalink
feat: dev & peer dependency handling
Browse files Browse the repository at this point in the history
Handle the dev and peer dependencies in the same way as normal dependencies
  • Loading branch information
ghinks committed Mar 2, 2021
1 parent ea46480 commit 458c399
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
16 changes: 15 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ exports.getLocalPackageJSON = async function getLocalPackageJSON () {
return JSON.parse(pkg)
}

/*
The check should search both the dev and peer dependencies to find out if the dependency is
regular, dev or peer. We only want to check on this the once :)
*/
exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) {
return Object.prototype.hasOwnProperty.call(packageJSON.dependencies, dep)
return (
(Object.prototype.hasOwnProperty.call(packageJSON, 'dependencies') && Object.prototype.hasOwnProperty.call(packageJSON.dependencies, dep)
? 'dependencies'
: false) ||
(Object.prototype.hasOwnProperty.call(packageJSON, 'devDependencies') && Object.prototype.hasOwnProperty.call(packageJSON.devDependencies, dep)
? 'devDependencies'
: false) ||
(Object.prototype.hasOwnProperty.call(packageJSON, 'peerDependencies') && Object.prototype.hasOwnProperty.call(packageJSON.peerDependencies, dep)
? 'peerDependencies'
: false)
)
}
9 changes: 5 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ const getCommitURL = async function getCommitURL (owner, repo, hash) {
}

const applyPatch = module.exports.applyPatch =
function applyPatch (patch, module, packageJSON) {
if (!context.checkPackageInPackageJSON(module, packageJSON)) {
function applyPatch (patch, module, dependentPkgJSON) {
const dependencyType = context.checkPackageInPackageJSON(module, dependentPkgJSON)
if (!dependencyType) {
throw new Error('Dependency not found in package.json')
}
packageJSON.dependencies[module] = patch
return packageJSON
dependentPkgJSON[dependencyType][module] = patch
return dependentPkgJSON
}

async function pushPatch (packageJSON, owner, repo, dep) {
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@ module.exports.PATCHED_PKGJSON = Object.assign({}, module.exports.PKGJSON, {
{ '@pkgjs/wiby': 'pkgjs/wiby#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e' }
)
})

module.exports.PKGJSON_DEV_DEPS = Object.assign({}, module.exports.PKGJSON, {
devDependencies: {
'@other/name': '*'
}
})
module.exports.PATCHED_DEV_DEPS_PKGJSON = Object.assign({}, module.exports.PKGJSON, {
devDependencies: Object.assign(
{},
{ '@other/name': 'other/name#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e' }
)
})

module.exports.PKGJSON_PEER_DEPS = Object.assign({}, module.exports.PKGJSON, {
peerDependencies: {
'@other/plugin': '*'
}
})
module.exports.PATCHED_PEER_DEPS_PKGJSON = Object.assign({}, module.exports.PKGJSON, {
peerDependencies: Object.assign(
{},
{ '@other/plugin': 'other/plugin#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e' }
)
})
module.exports.PATCH = 'pkgjs/wiby#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e'
module.exports.DEV_DEP_PATCH = 'other/name#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e'
module.exports.PEER_DEP_PATCH = 'other/plugin#577c08e8fd5e1b3156ce75b2e5d9e3023dac180e'
module.exports.PKG_NAME_UNIT = '@pkgjs/wiby'
module.exports.PKG_NAME_INTEGRATION = 'wiby'
module.exports.PKG_REPO = 'wiby'
Expand All @@ -17,3 +43,5 @@ module.exports.PKG_HEAD_SHA = '577c08e8fd5e1b3156ce75b2e5d9e3023dac180e'
module.exports.DEP_REPO = 'pass'
module.exports.DEP_ORG = 'wiby-test'
module.exports.DEP_REPO_PERM = 'ADMIN'
module.exports.DEV_DEP_PKG_NAME_UNIT = '@other/name'
module.exports.DEV_PEER_PKG_NAME_UNIT = '@other/plugin'
17 changes: 16 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ tap.afterEach(async () => {
nock.enableNetConnect()
})

tap.test('Check patch applied to package.json successfully', tap => {
tap.test('Check patch applied to dependency package.json successfully', tap => {
tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PATCH, CONFIG.PKG_NAME_UNIT, CONFIG.PKGJSON)), JSON.stringify(CONFIG.PATCHED_PKGJSON))
tap.end()
})
tap.test('Check patch applied to dev dependency package.json successfully', tap => {
tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.DEV_DEP_PATCH, CONFIG.DEV_DEP_PKG_NAME_UNIT, CONFIG.PKGJSON_DEV_DEPS)), JSON.stringify(CONFIG.PATCHED_DEV_DEPS_PKGJSON))
tap.end()
})

tap.test('Check patch applied to peer dependency package.json successfully', tap => {
tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PEER_DEP_PATCH, CONFIG.DEV_PEER_PKG_NAME_UNIT, CONFIG.PKGJSON_PEER_DEPS)), JSON.stringify(CONFIG.PATCHED_PEER_DEPS_PKGJSON))
tap.end()
})

tap.test('applyPatch() checks package exists in dependant package.json', tap => {
tap.throws(
Expand All @@ -29,6 +38,12 @@ tap.test('applyPatch() checks package exists in dependant package.json', tap =>
{
dependencies: {
'other-package': '*'
},
devDependencies: {
'further-packages': '*'
},
peerDependencies: {
'some-plugin': '*'
}
}
)
Expand Down

0 comments on commit 458c399

Please sign in to comment.