Skip to content

Commit 4b3b599

Browse files
mmkalsindresorhus
andauthored
Add pnpm support (#730)
Co-authored-by: Misha Kaletsky <[email protected]> Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 37d4bc9 commit 4b3b599

28 files changed

+348
-369
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"node": ">=18",
1212
"npm": ">=9",
1313
"git": ">=2.11.0",
14-
"yarn": ">=1.7.0"
14+
"yarn": ">=1.7.0",
15+
"pnpm": ">=8"
1516
},
1617
"scripts": {
1718
"test": "xo && ava"
@@ -40,7 +41,6 @@
4041
"execa": "^8.0.1",
4142
"exit-hook": "^4.0.0",
4243
"github-url-from-git": "^1.5.0",
43-
"has-yarn": "^3.0.0",
4444
"hosted-git-info": "^7.0.1",
4545
"ignore-walk": "^6.0.3",
4646
"import-local": "^3.1.0",
@@ -52,7 +52,7 @@
5252
"listr": "^0.14.3",
5353
"listr-input": "^0.2.1",
5454
"log-symbols": "^6.0.0",
55-
"meow": "^12.1.1",
55+
"meow": "^13.1.0",
5656
"new-github-release-url": "^2.0.0",
5757
"npm-name": "^7.1.1",
5858
"onetime": "^7.0.0",
@@ -62,8 +62,8 @@
6262
"p-timeout": "^6.1.2",
6363
"path-exists": "^5.0.0",
6464
"pkg-dir": "^8.0.0",
65-
"read-pkg": "^9.0.1",
6665
"read-package-up": "^11.0.0",
66+
"read-pkg": "^9.0.1",
6767
"rxjs": "^7.8.1",
6868
"semver": "^7.5.4",
6969
"symbol-observable": "^4.0.0",

readme.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ $ np --help
7474
$ np <version>
7575
7676
Version can be:
77-
major | minor | patch | premajor | preminor | prepatch | prerelease | 1.2.3
77+
patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3
7878
7979
Options
8080
--any-branch Allow publishing from any branch
@@ -85,13 +85,13 @@ $ np --help
8585
--no-publish Skips publishing
8686
--preview Show tasks without actually executing them
8787
--tag Publish under a given dist-tag
88-
--no-yarn Don't use Yarn
8988
--contents Subdirectory to publish
9089
--no-release-draft Skips opening a GitHub release draft
9190
--release-draft-only Only opens a GitHub release draft for the latest published version
9291
--test-script Name of npm run script to run tests before publishing (default: test)
9392
--no-2fa Don't enable 2FA on new packages (not recommended)
94-
--message Version bump commit message. `%s` will be replaced with version. (default: '%s' with npm and 'v%s' with yarn)
93+
--message Version bump commit message, '%s' will be replaced with version (default: '%s' with npm and 'v%s' with yarn)
94+
--package-manager Use a specific package manager (default: 'packageManager' field in package.json)
9595
9696
Examples
9797
$ np
@@ -121,21 +121,21 @@ Currently, these are the flags you can configure:
121121
- `publish` - Publish (`true` by default).
122122
- `preview` - Show tasks without actually executing them (`false` by default).
123123
- `tag` - Publish under a given dist-tag (`latest` by default).
124-
- `yarn` - Use yarn if possible (`true` by default).
125124
- `contents` - Subdirectory to publish (`.` by default).
126125
- `releaseDraft` - Open a GitHub release draft after releasing (`true` by default).
127126
- `testScript` - Name of npm run script to run tests before publishing (`test` by default).
128127
- `2fa` - Enable 2FA on new packages (`true` by default) (setting this to `false` is not recommended).
129128
- `message` - The commit message used for the version bump. Any `%s` in the string will be replaced with the new version. By default, npm uses `%s` and Yarn uses `v%s`.
129+
- `packageManager` - Set the package manager to be used. Defaults to the [packageManager field in package.json](https://nodejs.org/api/packages.html#packagemanager), so only use if you can't update package.json for some reason.
130130

131-
For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:
131+
For example, this configures `np` to use `unit-test` as a test script, and to use `dist` as the subdirectory to publish:
132132

133133
`package.json`
134134
```json
135135
{
136136
"name": "superb-package",
137137
"np": {
138-
"yarn": false,
138+
"testScript": "unit-test",
139139
"contents": "dist"
140140
}
141141
}
@@ -144,23 +144,23 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
144144
`.np-config.json`
145145
```json
146146
{
147-
"yarn": false,
147+
"testScript": "unit-test",
148148
"contents": "dist"
149149
}
150150
```
151151

152152
`.np-config.js` or `.np-config.cjs`
153153
```js
154154
module.exports = {
155-
yarn: false,
155+
testScript: 'unit-test',
156156
contents: 'dist'
157157
};
158158
```
159159

160160
`.np-config.mjs`
161161
```js
162162
export default {
163-
yarn: false,
163+
testScript: 'unit-test',
164164
contents: 'dist'
165165
};
166166
```
@@ -276,6 +276,10 @@ Set the [`registry` option](https://docs.npmjs.com/misc/config#registry) in pack
276276
}
277277
```
278278

279+
### Package managers
280+
281+
If a package manager is not set in package.json, via configuration (`packageManager`), or via the CLI (`--package-manager`), `np` will attempt to infer the best package manager to use by looking for lockfiles. But it's recommended to set the [`packageManager` field](https://nodejs.org/api/packages.html#packagemanager) in your package.json to be consistent with other tools. See also the [corepack docs](https://nodejs.org/api/corepack.html).
282+
279283
### Publish with a CI
280284

281285
If you use a Continuous Integration server to publish your tagged commits, use the `--no-publish` flag to skip the publishing step of `np`.

source/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.d.ts

source/cli-implementation.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import 'symbol-observable'; // Important: This needs to be first to prevent weir
44
import logSymbols from 'log-symbols';
55
import meow from 'meow';
66
import updateNotifier from 'update-notifier';
7-
import hasYarn from 'has-yarn';
87
import {gracefulExit} from 'exit-hook';
98
import config from './config.js';
109
import * as util from './util.js';
1110
import * as git from './git-util.js';
1211
import * as npm from './npm/util.js';
1312
import {SEMVER_INCREMENTS} from './version.js';
1413
import ui from './ui.js';
15-
import {checkIfYarnBerry} from './yarn.js';
1614
import np from './index.js';
1715

16+
/** @typedef {typeof cli} CLI */
17+
1818
const cli = meow(`
1919
Usage
2020
$ np <version>
@@ -31,13 +31,13 @@ const cli = meow(`
3131
--no-publish Skips publishing
3232
--preview Show tasks without actually executing them
3333
--tag Publish under a given dist-tag
34-
--no-yarn Don't use Yarn
3534
--contents Subdirectory to publish
3635
--no-release-draft Skips opening a GitHub release draft
3736
--release-draft-only Only opens a GitHub release draft for the latest published version
3837
--test-script Name of npm run script to run tests before publishing (default: test)
3938
--no-2fa Don't enable 2FA on new packages (not recommended)
4039
--message Version bump commit message, '%s' will be replaced with version (default: '%s' with npm and 'v%s' with yarn)
40+
--package-manager Use a specific package manager (default: 'packageManager' field in package.json)
4141
4242
Examples
4343
$ np
@@ -80,9 +80,8 @@ const cli = meow(`
8080
tag: {
8181
type: 'string',
8282
},
83-
yarn: {
84-
type: 'boolean',
85-
default: hasYarn(),
83+
packageManager: {
84+
type: 'string',
8685
},
8786
contents: {
8887
type: 'string',
@@ -105,7 +104,9 @@ const cli = meow(`
105104

106105
updateNotifier({pkg: cli.pkg}).notify();
107106

108-
try {
107+
/** @typedef {Awaited<ReturnType<typeof getOptions>>['options']} Options */
108+
109+
export async function getOptions() {
109110
const {pkg, rootDir} = await util.readPkg(cli.flags.contents);
110111

111112
const localConfig = await config(rootDir);
@@ -119,6 +120,10 @@ try {
119120
flags['2fa'] = flags['2Fa'];
120121
}
121122

123+
if (flags.packageManager) {
124+
pkg.packageManager = flags.packageManager;
125+
}
126+
122127
const runPublish = !flags.releaseDraftOnly && flags.publish && !pkg.private;
123128

124129
// TODO: does this need to run if `runPublish` is false?
@@ -132,22 +137,26 @@ try {
132137

133138
const branch = flags.branch ?? await git.defaultBranch();
134139

135-
const isYarnBerry = flags.yarn && checkIfYarnBerry(pkg);
136-
137140
const options = await ui({
138141
...flags,
139142
runPublish,
140143
availability,
141144
version,
142145
branch,
143-
}, {pkg, rootDir, isYarnBerry});
146+
}, {pkg, rootDir});
147+
148+
return {options, rootDir, pkg};
149+
}
150+
151+
try {
152+
const {options, rootDir, pkg} = await getOptions();
144153

145154
if (!options.confirm) {
146155
gracefulExit();
147156
}
148157

149158
console.log(); // Prints a newline for readability
150-
const newPkg = await np(options.version, options, {pkg, rootDir, isYarnBerry});
159+
const newPkg = await np(options.version, options, {pkg, rootDir});
151160

152161
if (options.preview || options.releaseDraftOnly) {
153162
gracefulExit();

source/git-util.js

-15
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,3 @@ export const verifyRecentGitVersion = async () => {
270270
const installedVersion = await gitVersion();
271271
util.validateEngineVersionSatisfies('git', installedVersion);
272272
};
273-
274-
export const checkIfFileGitIgnored = async pathToFile => {
275-
try {
276-
const {stdout} = await execa('git', ['check-ignore', pathToFile]);
277-
return Boolean(stdout);
278-
} catch (error) {
279-
// If file is not ignored, `git check-ignore` throws an empty error and exits.
280-
// Check that and return false so as not to throw an unwanted error.
281-
if (error.stdout === '' && error.stderr === '') {
282-
return false;
283-
}
284-
285-
throw error;
286-
}
287-
};

0 commit comments

Comments
 (0)