Skip to content

Commit

Permalink
Merge pull request #84 from ghinks/feature/other/dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas authored Mar 18, 2021
2 parents 5b031ee + 220c6bc commit d7168f5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
13 changes: 12 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ 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.
*/
exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) {
return Object.prototype.hasOwnProperty.call(packageJSON.dependencies, dep)
const dependencyKeyNames = [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies'
]
const checkKeyHasDep = (key) => Object.prototype.hasOwnProperty.call(packageJSON, key) && Object.prototype.hasOwnProperty.call(packageJSON[key], dep)
return dependencyKeyNames.find(x => checkKeyHasDep(x))
}
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
24 changes: 24 additions & 0 deletions test/fixtures/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@ 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:
{ '@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:
{ '@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 +39,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'
20 changes: 19 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,15 @@ tap.test('applyPatch() checks package exists in dependant package.json', tap =>
{
dependencies: {
'other-package': '*'
},
devDependencies: {
'further-packages': '*'
},
peerDependencies: {
'some-plugin': '*'
},
optionalDependencies: {
'some-optional-dep': '*'
}
}
)
Expand Down

0 comments on commit d7168f5

Please sign in to comment.