react-uswds icon indicating copy to clipboard operation
react-uswds copied to clipboard

fix: Allow npm users to install from the git repo

Open dmethvin opened this issue 4 years ago • 0 comments

Fixes #1359

Description

Issue #312 fixed the /lib directory for git dependencies. Merely grabbing the git repo doesn't generate that directory, and it's not good to save build artifacts in a git repo (because repos are for source files, not output files).

PR #842 made a change to run the prepublishOnly script for test and linting, then prepare to build /src into /lib for publishing. There are a couple of complications with that:

  1. Neither prepublishOnly nor prepare are supported in yarn v2. We're supposed to use prepack and prepublish instead. However, npm docs say prepublish is deprecated so that leaves prepack as the only common command.
  2. When the git repo is being directly used as a dependency, the files are built on the client using yarn which must be on the installing user's path. If the client uses npm for their project and doesn't have yarn, the install fails.

Changes in this PR

  1. Run the prepack script to build /lib. According to the docs, both npm and yarn run prepack when installing git dependencies. So, this change still fixes the case identified in #312 and works for npm clients.

  2. Use npm-run-all to determine whether yarn or npm are running the scripts. This is only necessary for the prepack script when npm users install the git repo as a dependency. Owners of the react-uswds project publishing the library should still run yarn so that the repo's yarn.lock file is used.

Note that this does run the test and lint scripts on the installer's system when it did not do so before. I consider this an enhancement since npm users may have different dependencies. It is worth testing the final package to help ensure that reported bugs are reproducible.

Alternatives considered

  • Use yarn-or-npm to determine if a particular repo wants yarn or npm based on the presence of a yarn.lock or package-lock.json file. Unfortunately that would not help here because that command always tries to build with yarn based on this repo having only a yarn.lock.

  • Use the npm_execpath environment variable. Both npm and yarn set this in child processes, which could be used in a shell command instead of hard coding. However, the command line use of environment variables is different on Windows (%npm_execpath%) versus Unix ($npm_execpath) shells. We would need to also add a tool like cross-env-shell, which has unergonomic escaping requirements.

  • Document that the project doesn't support npm when react-uswds is installed from a git repo, and yarn must be installed instead. Since npm ignores the yarn.lock file saved in the repo, npm users installing from the git repo will almost certainly receive different dependency versions than if they'd used yarn. If all dependencies follow semver it shouldn't matter, but it could cause hard-to-debug issues.

References:

dmethvin avatar Sep 21 '21 00:09 dmethvin