Skip to content

Commit

Permalink
feat: add semrel context paths remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 18, 2021
1 parent 24f0e15 commit 0d290d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Follow [its docs](https://github.com/semantic-release/npm/blob/master/README.md)
}]
]
}

```

### License
Expand Down
11 changes: 9 additions & 2 deletions src/main/js/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const plugin = require('@semantic-release/npm')
const { castArray } = require('lodash')

let verified
let result

async function verifyConditionsHooked(...args) {
async function verifyConditionsHooked(pluginConfig, context) {
if (verified) {
return result
}

return plugin.verifyConditions(...args).then(r => {
// Options remapping
if (context.options.publish) {
const pluginConfig = castArray(context.options.publish).find((config) => config.path === '@semrel-extra/npm') || {}
pluginConfig.path = '@semantic-release/npm'
}

return plugin.verifyConditions(pluginConfig, context).then(r => {
result = r
verified = true
})
Expand Down
27 changes: 20 additions & 7 deletions src/test/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('uvu/assert')
const plugin = require('../../main/js')
const realNpmPlugin = require('@semantic-release/npm')


test('prepare, publish, addChannel are re-exported as is', () => {
assert.is(realNpmPlugin.addChannel, plugin.addChannel)
assert.is(realNpmPlugin.publish, plugin.publish)
Expand All @@ -12,24 +13,36 @@ test('prepare, publish, addChannel are re-exported as is', () => {

test('verifyConditions passes args down to `semrel/npm`', async () => {
const mock = sinon.mock(realNpmPlugin)
mock.expects('verifyConditions').once().withArgs('test').returns(Promise.resolve())
const context = {options: {}}
mock.expects('verifyConditions').once().withArgs('test', context).returns(Promise.resolve())

await plugin.verifyConditions('test', context)

mock.verify()
plugin.verifyConditions._reset()
})

test('verifyConditions replaces context paths `', async () => {
const mock = sinon.mock(realNpmPlugin)
const context = {options: {publish: {path: '@semrel-extra/npm'}}}
const _context = {options: {publish: {path: '@semantic-release/npm'}}}
mock.expects('verifyConditions').once().withArgs('test', _context).returns(Promise.resolve())

await plugin.verifyConditions('test')
await plugin.verifyConditions('test', context)

mock.verify()
mock.restore()
plugin.verifyConditions._reset()
})

test('prevents multiple `verifyConditions` invocations', async () => {
const mock = sinon.mock(realNpmPlugin)
mock.expects('verifyConditions').once().withArgs('test').returns(Promise.resolve())
const context = {options: {publish: {}}}
mock.expects('verifyConditions').once().withArgs('test', context).returns(Promise.resolve())

await plugin.verifyConditions('test')
await plugin.verifyConditions('test')
await plugin.verifyConditions('test', context)
await plugin.verifyConditions('test', context)

mock.verify()
mock.restore()
plugin.verifyConditions._reset()
})

Expand Down

0 comments on commit 0d290d1

Please sign in to comment.