fix: Allow npm users to install from the git repo
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:
- Neither
prepublishOnlynorprepareare supported in yarn v2. We're supposed to useprepackandprepublishinstead. However, npm docs sayprepublishis deprecated so that leavesprepackas the only common command. - 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
-
Run the
prepackscript to build/lib. According to the docs, both npm and yarn runprepackwhen installing git dependencies. So, this change still fixes the case identified in #312 and works for npm clients. -
Use
npm-run-allto determine whether yarn or npm are running the scripts. This is only necessary for theprepackscript when npm users install the git repo as a dependency. Owners of thereact-uswdsproject publishing the library should still run yarn so that the repo'syarn.lockfile 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-npmto determine if a particular repo wants yarn or npm based on the presence of ayarn.lockorpackage-lock.jsonfile. Unfortunately that would not help here because that command always tries to build with yarn based on this repo having only ayarn.lock. -
Use the
npm_execpathenvironment 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 likecross-env-shell, which has unergonomic escaping requirements. -
Document that the project doesn't support npm when
react-uswdsis installed from a git repo, and yarn must be installed instead. Since npm ignores theyarn.lockfile 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: