Skip to content

Commit

Permalink
chore(package manager): introduce Corepack (#674)
Browse files Browse the repository at this point in the history
* chore(package manager): introduce Corepack

    From now on, we will use Corepack to manage pnpm versions.
    see https://nodejs.org/api/corepack.html
    see https://github.com/nodejs/corepack

* ci(github actions): setup Corepack instead of pnpm

    Corepack automatically prepares the pnpm.
    Thus, all we have to do is execute the `corepack enable` command.

* ci(corepack): should not use the latest Corepack for old Node.js

* ci(corepack): override Corepack if version range is specified

* ci(corepack): show installed Corepack versions

* ci: use the `npm install --global ...` command instead of the `npm install -g ...` command

* ci(debug): try to use pnpm without running the `corepack enable` command

    Surprisingly, Corepack seems to work with all versions of Node.js on GitHub Actions.
    Perhaps there is no need to run the `corepack enable` command.

* revert: ci(debug): try to use pnpm without running the `corepack enable` command

    The following error occurred on each job:

        pnpm: command not found

    It seems that the `corepack enable` command must be executed.

    This reverts commit 8cb0f48.

* docs: add setup instructions using Corepack in `CONTRIBUTING.md` file

* ci(github actions): fix the name of steps for enable Corepack to be more understandable

    The step name "Setup Corepack" indicates that it is setting up Corepack.
    However, this step name does not clearly indicate that pnpm will be available automatically.
    Contributors who are not familiar with Node.js will not be able to grasp what is possible by enabling Corepack.
    So we fix the step names and clarify what we are enabling Corepack for.
  • Loading branch information
sounisi5011 authored Feb 26, 2023
1 parent 96a311f commit 2a5ca2d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 99 deletions.
53 changes: 43 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
- name: Cache .pnpm-store
uses: actions/cache@v3
with:
Expand All @@ -82,8 +84,6 @@ jobs:
${{ runner.os }}-node-${{ matrix.node-version }}-
${{ runner.os }}-node-
${{ runner.os }}-
- name: Setup pnpm
uses: ./actions/setup-pnpm
- name: Install Dependencies
run: pnpm install
- name: Build packages
Expand Down Expand Up @@ -145,6 +145,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
- name: Cache actionlint
uses: actions/cache@v3
with:
Expand All @@ -154,8 +156,6 @@ jobs:
./scripts/actionlint/download-actionlint.bash
./scripts/actionlint/download-actionlint.bash.http-cache-metadata.json
key: actionlint-${{ runner.os }}
- name: Setup pnpm
uses: ./actions/setup-pnpm
- run: pnpm run lint
format:
needs: pre-build
Expand Down Expand Up @@ -184,8 +184,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Setup pnpm
uses: ./actions/setup-pnpm
- name: Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
- name: Setup dprint
# The dprint command may fail to deserialize the wasm module.
# This only happens on CI, and in some cases no error occurs.
Expand Down Expand Up @@ -303,8 +303,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Setup pnpm
uses: ./actions/setup-pnpm
- name: Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
- name: Install flatbuffers
# https://snapcraft.io/flatbuffers
run: |
Expand Down Expand Up @@ -401,6 +401,21 @@ jobs:
- 17.x
- 18.0.0
- 18.x
include:
# Corepack 0.11.0 and later only supports Node.js 14.14.0 and later
# see https://github.com/nodejs/corepack/pull/227
- node-version: 12.17.0
corepack-version-range: "<0.11"
- node-version: 12.20.0
corepack-version-range: "<0.11"
- node-version: 12.x
corepack-version-range: "<0.11"
- node-version: 14.1.0
corepack-version-range: "<0.11"
- node-version: 14.13.0
corepack-version-range: "<0.11"
- node-version: 14.13.1
corepack-version-range: "<0.11"
steps:
- uses: actions/checkout@v3
- name: Download installed dependencies
Expand Down Expand Up @@ -430,6 +445,26 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Enable Corepack (Automatically setup a package manager for Node.js)
shell: bash
# If Corepack does not exist or a Corepack version range is specified, install Corepack
run: |
readonly COREPACK_VERSION_RANGE='${{ matrix.corepack-version-range }}'
if { type corepack >/dev/null 2>&1; } && [ -z "${COREPACK_VERSION_RANGE}" ]; then
echo Corepack "$(corepack --version)" is already installed
else
readonly corepack_package_spec="corepack${COREPACK_VERSION_RANGE:+"@${COREPACK_VERSION_RANGE}"}"
if type corepack >/dev/null 2>&1; then
echo '::group::Override corepack' "(install ${corepack_package_spec})"
else
echo '::group::Install' "${corepack_package_spec}"
fi
npm install --global "${corepack_package_spec}"
echo '::endgroup::'
fi
echo '::group::$' corepack enable
corepack enable
echo '::endgroup::'
- name: Cache .pnpm-store
uses: actions/cache@v3
with:
Expand All @@ -440,8 +475,6 @@ jobs:
${{ runner.os }}-node-
${{ runner.os }}-
if: ${{ ! steps.restore-deps-and-build.outputs.node_modules }}
- name: Setup pnpm
uses: ./actions/setup-pnpm
- name: Install Dependencies
run: pnpm install
if: ${{ ! steps.restore-deps-and-build.outputs.node_modules }}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ jobs:
with:
node-version: 16.x
if: ${{ steps.release-pr.outputs.ref }}
- name: Commit to Release PR / Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
if: ${{ steps.release-pr.outputs.ref }}
- name: Commit to Release PR / Cache .pnpm-store
uses: actions/cache@v3
with:
Expand All @@ -134,9 +137,6 @@ jobs:
${{ runner.os }}-node-
${{ runner.os }}-
if: ${{ steps.release-pr.outputs.ref }}
- name: Commit to Release PR / Setup pnpm
uses: ./actions/setup-pnpm
if: ${{ steps.release-pr.outputs.ref }}
- name: Commit to Release PR / Install Dependencies
run: pnpm install
if: ${{ steps.release-pr.outputs.ref }}
Expand Down Expand Up @@ -215,6 +215,9 @@ jobs:
# Note: The `registry-url` option is required.
# If this option is not set, the "npm publish" command will not detect the environment variable NODE_AUTH_TOKEN.
if: ${{ steps.release.outputs.releases_created }}
- name: Publish / Enable Corepack (Automatically setup a package manager for Node.js)
run: corepack enable
if: ${{ steps.release.outputs.releases_created }}
- name: Publish / Cache .pnpm-store
uses: actions/cache@v3
with:
Expand All @@ -225,9 +228,6 @@ jobs:
${{ runner.os }}-node-
${{ runner.os }}-
if: ${{ steps.release.outputs.releases_created }}
- name: Publish / Setup pnpm
uses: ./actions/setup-pnpm
if: ${{ steps.release.outputs.releases_created }}
- name: Publish / Install Dependencies
run: pnpm install
if: ${{ steps.release.outputs.releases_created }}
Expand Down
11 changes: 10 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ We use [Semantic Versioning](https://semver.org/).

- [Git](https://git-scm.com/)
- [Node.js](https://nodejs.org/)
- [pnpm](https://pnpm.js.org/)
- [pnpm](https://pnpm.js.org/) (If [Corepack] is available, you do not need to install pnpm)

[Corepack]: https://nodejs.org/api/corepack.html

### Setup

> **Note**
> If [Corepack] is available in your development environment, do not forget to [enable Corepack](https://nodejs.org/docs/latest-v16.x/api/corepack.html#enabling-the-feature) before running the `pnpm` command.
>
> ```sh
> $ corepack enable
> ```
```sh
$ git clone https://github.com/sounisi5011/npm-packages.git
$ cd ./npm-packages/
Expand Down
15 changes: 0 additions & 15 deletions actions/setup-pnpm/action.yaml

This file was deleted.

67 changes: 0 additions & 67 deletions actions/setup-pnpm/get-pnpm-data.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"typescript": "4.9.4",
"ultra-runner": "3.10.5"
},
"packageManager": "[email protected]",
"engines": {
"node": "^12.17.x || 14.x || 15.x || 16.x || 17.x || >=18.x",
"npm": "use pnpm please!",
Expand Down

0 comments on commit 2a5ca2d

Please sign in to comment.