diff --git a/lib/context.js b/lib/context.js index 1b73ef3..59d8a5e 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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)) } diff --git a/lib/test.js b/lib/test.js index ccf9501..c02861b 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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) { diff --git a/test/fixtures/config.js b/test/fixtures/config.js index 18275b2..0698607 100644 --- a/test/fixtures/config.js +++ b/test/fixtures/config.js @@ -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' @@ -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' diff --git a/test/test.js b/test/test.js index ef60c7b..36065b5 100644 --- a/test/test.js +++ b/test/test.js @@ -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( @@ -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': '*' } } )