Skip to content

feat: support customize npmPublishHint #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions command.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ const yargs = require('yargs')
type: 'string',
describe: 'Name of the package from which the tags will be extracted'
})
.option('npmPublishHint', {
type: 'string',
default: defaults.npmPublishHint,
describe: 'Customized publishing hint'
})
.check((argv) => {
if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {
throw Error('scripts must be an object')
Expand Down
3 changes: 2 additions & 1 deletion defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const defaults = {
dryRun: false,
tagForce: false,
gitTagFallback: true,
preset: require.resolve('conventional-changelog-conventionalcommits')
preset: require.resolve('conventional-changelog-conventionalcommits'),
npmPublishHint: undefined
}

/**
Expand Down
53 changes: 53 additions & 0 deletions lib/detect-package-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* modified from <https://github.com/egoist/detect-package-manager/blob/main/src/index.ts>
* the original code is licensed under MIT
* modified to support only detecting lock file and not detecting global package manager
*/

const { promises: fs } = require('fs')
const { resolve } = require('path')

/**
* Check if a path exists
*/
async function pathExists (p) {
try {
await fs.access(p)
return true
} catch {
return false
}
}

function getTypeofLockFile (cwd = '.') {
return Promise.all([
pathExists(resolve(cwd, 'yarn.lock')),
pathExists(resolve(cwd, 'package-lock.json')),
pathExists(resolve(cwd, 'pnpm-lock.yaml'))
]).then(([isYarn, isNpm, isPnpm]) => {
let value = null

if (isYarn) {
value = 'yarn'
} else if (isPnpm) {
value = 'pnpm'
} else if (isNpm) {
value = 'npm'
}

return value
})
}

const detectPMByLockFile = async (cwd) => {
const type = await getTypeofLockFile(cwd)
if (type) {
return type
}

return 'npm'
}

module.exports = {
detectPMByLockFile
}
10 changes: 9 additions & 1 deletion lib/lifecycles/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const figures = require('figures')
const formatCommitMessage = require('../format-commit-message')
const runExecFile = require('../run-execFile')
const runLifecycleScript = require('../run-lifecycle-script')
const { detectPMByLockFile } = require('../detect-package-manager')

module.exports = async function (newVersion, pkgPrivate, args) {
if (args.skip.tag) return
Expand All @@ -13,6 +14,12 @@ module.exports = async function (newVersion, pkgPrivate, args) {
await runLifecycleScript(args, 'posttag')
}

async function detectPublishHint () {
const npmClientName = await detectPMByLockFile()
const publishCommand = 'publish'
return `${npmClientName} ${publishCommand}`
}

async function execTag (newVersion, pkgPrivate, args) {
const tagOption = []
if (args.sign) {
Expand All @@ -28,7 +35,8 @@ async function execTag (newVersion, pkgPrivate, args) {
const currentBranch = await runExecFile('', 'git', ['rev-parse', '--abbrev-ref', 'HEAD'])
let message = 'git push --follow-tags origin ' + currentBranch.trim()
if (pkgPrivate !== true && bump.getUpdatedConfigs()['package.json']) {
message += ' && npm publish'
const npmPublishHint = args.npmPublishHint || await detectPublishHint()
message += ` && ${npmPublishHint}`
if (args.prerelease !== undefined) {
if (args.prerelease === '') {
message += ' --tag prerelease'
Expand Down
8 changes: 8 additions & 0 deletions test/git.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ describe('git', function () {
.should.match(/npm publish/)
})

it('can display publish hints with custom npm client name', async function () {
const flush = mock({ bump: 'patch' })
await exec('--npmPublishHint "yarn publish"')
flush()
.stdout.join('')
.should.match(/yarn publish/)
})

it('does not display `npm publish` if the package is private', async function () {
writePackageJson('1.0.0', { private: true })
const flush = mock({ bump: 'patch' })
Expand Down
52 changes: 52 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* global describe it */

const mockery = require('mockery')
const { promises: fsp } = require('fs')
require('chai').should()

function mockNpm () {
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
let lockFile = ''

const fsMock = {
promises: {
access: async function (path) {
if (lockFile && path.endsWith(lockFile)) {
return true
}
await fsp.access(path)
}
}
}
mockery.registerMock('fs', fsMock)
return {
setLockFile (file) {
lockFile = file
}
}
}

describe('utils', () => {
it('detectPMByLockFile should work', async function () {
const { setLockFile } = mockNpm()
const { detectPMByLockFile } = require('../lib/detect-package-manager')

let pm = await detectPMByLockFile()
pm.should.equal('npm')

setLockFile('yarn.lock')
pm = await detectPMByLockFile()
pm.should.equal('yarn')

setLockFile('package-lock.json')
pm = await detectPMByLockFile()
pm.should.equal('npm')

setLockFile('pnpm-lock.yaml')
pm = await detectPMByLockFile()
pm.should.equal('pnpm')

mockery.deregisterAll()
mockery.disable()
})
})