Skip to content
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

pnpm support #730

Merged
merged 25 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"node": ">=18",
"npm": ">=9",
"git": ">=2.11.0",
"yarn": ">=1.7.0"
"yarn": ">=1.7.0",
"pnpm": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -40,7 +41,6 @@
"execa": "^8.0.1",
"exit-hook": "^4.0.0",
"github-url-from-git": "^1.5.0",
"has-yarn": "^3.0.0",
"hosted-git-info": "^7.0.1",
"ignore-walk": "^6.0.3",
"import-local": "^3.1.0",
Expand All @@ -52,7 +52,7 @@
"listr": "^0.14.3",
"listr-input": "^0.2.1",
"log-symbols": "^6.0.0",
"meow": "^12.1.1",
"meow": "^13.1.0",
"new-github-release-url": "^2.0.0",
"npm-name": "^7.1.1",
"onetime": "^7.0.0",
Expand All @@ -62,8 +62,8 @@
"p-timeout": "^6.1.2",
"path-exists": "^5.0.0",
"pkg-dir": "^8.0.0",
"read-pkg": "^9.0.1",
"read-package-up": "^11.0.0",
"read-pkg": "^9.0.1",
"rxjs": "^7.8.1",
"semver": "^7.5.4",
"symbol-observable": "^4.0.0",
Expand Down
22 changes: 13 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ $ np --help
$ np <version>

Version can be:
major | minor | patch | premajor | preminor | prepatch | prerelease | 1.2.3
patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3

Options
--any-branch Allow publishing from any branch
Expand All @@ -85,13 +85,13 @@ $ np --help
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--release-draft-only Only opens a GitHub release draft for the latest published version
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
--message Version bump commit message. `%s` will be replaced with version. (default: '%s' with npm and 'v%s' with yarn)
--message Version bump commit message, '%s' will be replaced with version (default: '%s' with npm and 'v%s' with yarn)
--package-manager Use a specific package manager (default: 'packageManager' field in package.json)
mmkal marked this conversation as resolved.
Show resolved Hide resolved

Examples
$ np
Expand Down Expand Up @@ -121,21 +121,21 @@ Currently, these are the flags you can configure:
- `publish` - Publish (`true` by default).
- `preview` - Show tasks without actually executing them (`false` by default).
- `tag` - Publish under a given dist-tag (`latest` by default).
- `yarn` - Use yarn if possible (`true` by default).
- `contents` - Subdirectory to publish (`.` by default).
- `releaseDraft` - Open a GitHub release draft after releasing (`true` by default).
- `testScript` - Name of npm run script to run tests before publishing (`test` by default).
- `2fa` - Enable 2FA on new packages (`true` by default) (setting this to `false` is not recommended).
- `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`.
- `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.

For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:
For example, this configures `np` to use `unit-test` as a test script, and to use `dist` as the subdirectory to publish:
mmkal marked this conversation as resolved.
Show resolved Hide resolved

`package.json`
```json
{
"name": "superb-package",
"np": {
"yarn": false,
"testScript": "unit-test",
"contents": "dist"
}
}
Expand All @@ -144,23 +144,23 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
`.np-config.json`
```json
{
"yarn": false,
"testScript": "unit-test",
"contents": "dist"
}
```

`.np-config.js` or `.np-config.cjs`
```js
module.exports = {
yarn: false,
testScript: 'unit-test',
contents: 'dist'
};
```

`.np-config.mjs`
```js
export default {
yarn: false,
testScript: 'unit-test',
contents: 'dist'
};
```
Expand Down Expand Up @@ -276,6 +276,10 @@ Set the [`registry` option](https://docs.npmjs.com/misc/config#registry) in pack
}
```

### Package Managers

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 user 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 [corepack docs](https://nodejs.org/api/corepack.html).

### Publish with a CI

If you use a Continuous Integration server to publish your tagged commits, use the `--no-publish` flag to skip the publishing step of `np`.
Expand Down
29 changes: 17 additions & 12 deletions source/cli-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import 'symbol-observable'; // Important: This needs to be first to prevent weir
import logSymbols from 'log-symbols';
import meow from 'meow';
import updateNotifier from 'update-notifier';
import hasYarn from 'has-yarn';
import {gracefulExit} from 'exit-hook';
import config from './config.js';
import * as util from './util.js';
import * as git from './git-util.js';
import * as npm from './npm/util.js';
import {SEMVER_INCREMENTS} from './version.js';
import ui from './ui.js';
import {checkIfYarnBerry} from './yarn.js';
import np from './index.js';

const cli = meow(`
export const cli = meow(`
mmkal marked this conversation as resolved.
Show resolved Hide resolved
Usage
$ np <version>

Expand All @@ -31,13 +29,13 @@ const cli = meow(`
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--release-draft-only Only opens a GitHub release draft for the latest published version
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
--message Version bump commit message, '%s' will be replaced with version (default: '%s' with npm and 'v%s' with yarn)
--package-manager Use a specific package manager (default: 'packageManager' field in package.json)

Examples
$ np
Expand Down Expand Up @@ -80,9 +78,8 @@ const cli = meow(`
tag: {
type: 'string',
},
yarn: {
type: 'boolean',
default: hasYarn(),
packageManager: {
type: 'string',
},
contents: {
type: 'string',
Expand All @@ -105,7 +102,7 @@ const cli = meow(`

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

try {
export async function getOptions() {
mmkal marked this conversation as resolved.
Show resolved Hide resolved
const {pkg, rootDir} = await util.readPkg(cli.flags.contents);

const localConfig = await config(rootDir);
Expand All @@ -119,6 +116,10 @@ try {
flags['2fa'] = flags['2Fa'];
}

if (flags.packageManager) {
pkg.packageManager = flags.packageManager;
}

const runPublish = !flags.releaseDraftOnly && flags.publish && !pkg.private;

// TODO: does this need to run if `runPublish` is false?
Expand All @@ -132,22 +133,26 @@ try {

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

const isYarnBerry = flags.yarn && checkIfYarnBerry(pkg);

const options = await ui({
...flags,
runPublish,
availability,
version,
branch,
}, {pkg, rootDir, isYarnBerry});
}, {pkg, rootDir});

return {options, rootDir, pkg};
}

try {
const {options, rootDir, pkg} = await getOptions();

if (!options.confirm) {
gracefulExit();
}

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

if (options.preview || options.releaseDraftOnly) {
gracefulExit();
Expand Down
Loading
Loading