Skip to content

Commit

Permalink
Add pnpm support (#730)
Browse files Browse the repository at this point in the history
Co-authored-by: Misha Kaletsky <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
3 people authored Feb 26, 2024
1 parent 37d4bc9 commit 4b3b599
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 369 deletions.
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)
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:

`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 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).

### 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
1 change: 1 addition & 0 deletions source/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.d.ts
31 changes: 20 additions & 11 deletions source/cli-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ 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';

/** @typedef {typeof cli} CLI */

const cli = meow(`
Usage
$ np <version>
Expand All @@ -31,13 +31,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 +80,8 @@ const cli = meow(`
tag: {
type: 'string',
},
yarn: {
type: 'boolean',
default: hasYarn(),
packageManager: {
type: 'string',
},
contents: {
type: 'string',
Expand All @@ -105,7 +104,9 @@ const cli = meow(`

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

try {
/** @typedef {Awaited<ReturnType<typeof getOptions>>['options']} Options */

export async function getOptions() {
const {pkg, rootDir} = await util.readPkg(cli.flags.contents);

const localConfig = await config(rootDir);
Expand All @@ -119,6 +120,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 +137,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
15 changes: 0 additions & 15 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,3 @@ export const verifyRecentGitVersion = async () => {
const installedVersion = await gitVersion();
util.validateEngineVersionSatisfies('git', installedVersion);
};

export const checkIfFileGitIgnored = async pathToFile => {
try {
const {stdout} = await execa('git', ['check-ignore', pathToFile]);
return Boolean(stdout);
} catch (error) {
// If file is not ignored, `git check-ignore` throws an empty error and exits.
// Check that and return false so as not to throw an unwanted error.
if (error.stdout === '' && error.stderr === '') {
return false;
}

throw error;
}
};
Loading

0 comments on commit 4b3b599

Please sign in to comment.