Skip to content

Commit 174a8bd

Browse files
committed
feat: implement detect pm name
1 parent 393c560 commit 174a8bd

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

command.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ const yargs = require('yargs')
121121
type: 'string',
122122
describe: 'Name of the package from which the tags will be extracted'
123123
})
124-
.option('npmClient', {
124+
.option('npmPublishHint', {
125125
type: 'string',
126-
default: defaults.npmClient,
127-
describe: 'Show publish hint with the specified npm client'
126+
default: defaults.npmPublishHint,
127+
describe: 'Customized publishing hint'
128128
})
129129
.check((argv) => {
130130
if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {

defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const defaults = {
1515
tagForce: false,
1616
gitTagFallback: true,
1717
preset: require.resolve('conventional-changelog-conventionalcommits'),
18-
npmClient: 'npm'
18+
npmPublishHint: undefined
1919
}
2020

2121
/**

lib/detect-package-manager.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* modified from <https://github.com/egoist/detect-package-manager/blob/main/src/index.ts>
3+
* the original code is licensed under MIT
4+
* modified to support only detecting lock file and not detecting global package manager
5+
*/
6+
7+
const { promises: fs } = require('fs')
8+
const { resolve } = require('path')
9+
10+
/**
11+
* Check if a path exists
12+
*/
13+
async function pathExists (p) {
14+
try {
15+
await fs.access(p)
16+
return true
17+
} catch {
18+
return false
19+
}
20+
}
21+
22+
function getTypeofLockFile (cwd = '.') {
23+
return Promise.all([
24+
pathExists(resolve(cwd, 'yarn.lock')),
25+
pathExists(resolve(cwd, 'package-lock.json')),
26+
pathExists(resolve(cwd, 'pnpm-lock.yaml'))
27+
]).then(([isYarn, isNpm, isPnpm]) => {
28+
let value = null
29+
30+
if (isYarn) {
31+
value = 'yarn'
32+
} else if (isPnpm) {
33+
value = 'pnpm'
34+
} else if (isNpm) {
35+
value = 'npm'
36+
}
37+
38+
return value
39+
})
40+
}
41+
42+
const detectPMByLockFile = async (cwd) => {
43+
const type = await getTypeofLockFile(cwd)
44+
if (type) {
45+
return type
46+
}
47+
48+
return 'npm'
49+
}
50+
51+
module.exports = {
52+
detectPMByLockFile
53+
}

lib/lifecycles/tag.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const figures = require('figures')
55
const formatCommitMessage = require('../format-commit-message')
66
const runExecFile = require('../run-execFile')
77
const runLifecycleScript = require('../run-lifecycle-script')
8+
const { detectPMByLockFile } = require('../detect-package-manager')
89

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

17+
async function detectPublishHint () {
18+
const npmClientName = await detectPMByLockFile()
19+
const publishCommand = 'publish'
20+
return `${npmClientName} ${publishCommand}`
21+
}
22+
1623
async function execTag (newVersion, pkgPrivate, args) {
1724
const tagOption = []
1825
if (args.sign) {
@@ -28,7 +35,8 @@ async function execTag (newVersion, pkgPrivate, args) {
2835
const currentBranch = await runExecFile('', 'git', ['rev-parse', '--abbrev-ref', 'HEAD'])
2936
let message = 'git push --follow-tags origin ' + currentBranch.trim()
3037
if (pkgPrivate !== true && bump.getUpdatedConfigs()['package.json']) {
31-
message += ` && ${args.npmClient || 'npm'} publish`
38+
const npmPublishHint = args.npmPublishHint || await detectPublishHint()
39+
message += ` && ${npmPublishHint}`
3240
if (args.prerelease !== undefined) {
3341
if (args.prerelease === '') {
3442
message += ' --tag prerelease'

test/git.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ describe('git', function () {
399399

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

test/utils.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* global describe it */
2+
3+
const mockery = require('mockery')
4+
const { promises: fsp } = require('fs')
5+
require('chai').should()
6+
7+
function mockNpm () {
8+
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
9+
let lockFile = ''
10+
11+
const fsMock = {
12+
promises: {
13+
access: async function (path) {
14+
if (lockFile && path.endsWith(lockFile)) {
15+
return true
16+
}
17+
await fsp.access(path)
18+
}
19+
}
20+
}
21+
mockery.registerMock('fs', fsMock)
22+
return {
23+
setLockFile (file) {
24+
lockFile = file
25+
}
26+
}
27+
}
28+
29+
describe('utils', () => {
30+
it('detectPMByLockFile should work', async function () {
31+
const { setLockFile } = mockNpm()
32+
const { detectPMByLockFile } = require('../lib/detect-package-manager')
33+
34+
let pm = await detectPMByLockFile()
35+
pm.should.equal('npm')
36+
37+
setLockFile('yarn.lock')
38+
pm = await detectPMByLockFile()
39+
pm.should.equal('yarn')
40+
41+
setLockFile('package-lock.json')
42+
pm = await detectPMByLockFile()
43+
pm.should.equal('npm')
44+
45+
setLockFile('pnpm-lock.yaml')
46+
pm = await detectPMByLockFile()
47+
pm.should.equal('pnpm')
48+
49+
mockery.deregisterAll()
50+
mockery.disable()
51+
})
52+
})

0 commit comments

Comments
 (0)