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

chore(package.json): fix settings allowing developers to only use pnpm #673

Merged
merged 4 commits into from
Feb 26, 2023

Conversation

sounisi5011
Copy link
Owner

@sounisi5011 sounisi5011 commented Feb 26, 2023

We tried running the npm i command on this project. We found some problems, which we will fix.

  • Automatically inputs "y" into the stdin of the npx only-allow pnpm command

    The npx command prompts before installing a non-existent package.
    This prompt will continue if type "y". Thus, we have fixed it to automatically input "y".

    Note
    The --yes option is not used. This is because the npx command included with npm v6 will not work with the --yes option.

  • Add incorrect engines field for npm and yarn

    In npm v7 and later, the npm install command executes the preinstall script after downloading the dependencies.
    Thus, after waiting for the dependencies to download, the developer learns that he/she/they should use pnpm instead of npm for this project.
    This is a waste of time.

    So we set an incorrect value in the engines field.
    npm will display a warning that the engines field is invalid while downloading dependencies.
    Thus, the developer can abort the dependency download.
    see https://zenn.dev/uttk/articles/create-pnpm-monorepo

    Note
    If we set the engine-strict config flag, npm will automatically abort the dependency download.
    However, error messages reporting incorrect engines fields are never easy to read.
    The developer will have difficulty knowing why the npm install command is failing.
    Error messages displayed by the npx only-allow pnpm command are better.

We tried running the `npm i` command on this project. We found some problems, which we will fix.

+ Add `--yes` option to `npx only-allow pnpm` command

    The npx command prompts before installing a non-existent package.
    To suppress this prompt, the `--yes` option must be added.

+ Add incorrect `engines` field for npm and yarn

    The `npm install` command executes the `preinstall` script after downloading the dependencies.
    Thus, after waiting for the dependencies to download, the developer learns that he/she/they should use pnpm instead of npm for this project.
    This is a waste of time.

    So we set an incorrect value in the `engines` field.
    npm will display a warning that the `engines` field is invalid while downloading dependencies.
    Thus, the developer can abort the dependency download.
    see https://zenn.dev/uttk/articles/create-pnpm-monorepo

    Note: If we set the `engine-strict` config flag, npm will automatically abort the dependency download.
          However, error messages reporting incorrect `engines` fields are never easy to read.
          The developer will have difficulty knowing why the `npm install` command is failing.
          Error messages displayed by the `npx --yes only-allow pnpm` command are better.
…command

The `npx` command included with npm v6 does not allow the `--yes` option.
However, the `npx` command included with npm v7 and later will print a prompt if the `--yes` option is not added.

To solve this problem, we decided to include `only-allow` in the dev dependencies for this project.
This will ensure that `only-allow` is executed correctly in each version of the `npx` command:

+ If using npm v6, the `--yes` option is not necessary.
  npx will run `only-allow` without prompting.
+ If using npm v7 or later, the `npm install` command will install the dependencies before running the `preinstall` script.
  `only-allow` is included in the dependencies, so npx will not fetch `only-allow` from remote.
  Therefore, npx will not print a prompt.
…option with the `npx` command

With yarn, the `preinstall` script is run before the dependencies are installed.
In this case, the npx command will print a prompt if npm v7 or later is installed in the development environment.

This reverts commit fe4cb64.
github-actions[bot]
github-actions bot previously approved these changes Feb 26, 2023
…nto stdin of the `npx` command

The `npx` command included with npm v6 does not allow the `--yes` option.
However, the `npx` command included with npm v7 and later will print a prompt if the `--yes` option is not added.

To solve this problem, we decided to automatically input "y" into the stdin of the `npx` command.
This is the same solution as using the `yes` command in Unix.
However, the `yes` command is not available on Windows, so the `node` command is used to output "y" instead of it.

Note: The `npx` command included with npm v7 and later seems not to print a prompt just the Unix pipeline is connected.
      see https://github.com/npm/cli/blob/libnpmexec-v5.0.11/workspaces/libnpmexec/lib/index.js#L245-L262
      see https://github.com/npm/cli/blob/libnpmexec-v5.0.11/workspaces/libnpmexec/test/prompt.js#L167-L186
      Thus, without outputting "y", this problem can be solved with the following simple command: `node -e "" | npx only-allow pnpm`
      However, this behavior may not be the same for future `npx` commands.
@codeclimate
Copy link

codeclimate bot commented Feb 26, 2023

Code Climate has analyzed commit 19872e0 and detected 0 issues on this pull request.

View more on Code Climate.

@sounisi5011 sounisi5011 merged commit 96a311f into main Feb 26, 2023
@sounisi5011 sounisi5011 deleted the fix-only-allow-pnpm-settings branch February 26, 2023 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant