From 02a0f71439e3ec62f150505843bb39a6ff72ffd3 Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Thu, 14 Jul 2022 16:57:29 -0400 Subject: [PATCH] feat: run npm hooks (#4) --- README.md | 13 ++++++++----- src/index.ts | 48 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 113514f..8920db6 100644 --- a/README.md +++ b/README.md @@ -85,16 +85,19 @@ However, this will not yield the same exact output as `npm publish` because: - There can be missing distribution files (eg. files outside of `dist`). _build-this-branch_ uses [npm-packlist](https://github.com/npm/npm-packlist) —the same library `npm publish` uses—to detect publish files declared via `package.json#files` and `.npmignore`. - Irrelevant files are committed (eg. source files). This can slow down installation or even interfere with the library behavior. For example, if your project has development configuration files, they can accidentally be read by the dependent tooling. +- npm hooks are not executed. _build-this-branch_ simulates package packing and runs hooks `prepare` and `prepack`. + ### What exactly does this script do? This script does the following to make a _built branch_: 1. Run build script (eg. `npm run build`) -2. Create a new branch with the `built/` namespace -3. [Detects](https://github.com/npm/npm-packlist) and only commits npm publish files to the new branch -4. Force pushes up to remote -5. Deletes local built branch -6. Prints the installation command for the built branch +2. Run "prepare" & "prepack" [npm hooks](https://docs.npmjs.com/cli/v8/using-npm/scripts) +3. Create a new branch with the `built/` namespace +4. [Detects](https://github.com/npm/npm-packlist) and only commits npm publish files to the new branch +5. Force pushes up to remote +6. Deletes local built branch +7. Prints the installation command for the built branch ### Can I install from a built branch hosted on a private repository? diff --git a/src/index.ts b/src/index.ts index 5c4e5a8..7a315f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -109,7 +109,6 @@ const { stringify } = JSON; throw new Error('No package.json found'); } - const packageJson = await readJson(packageJsonPath); const { builtBranch = `built/${branchFrom}`, buildCommand, @@ -172,12 +171,31 @@ const { stringify } = JSON; getPublishFiles.clear(); } - const removePrepack = await task('Removing prepack & prepare scripts', async ({ setWarning }) => { + const runHooks = await task('Running hooks', async ({ setWarning, setTitle }) => { if (dry) { setWarning(''); return; } + setTitle('Running hook "prepare"'); + await execa('npm', ['run', '--if-present', 'prepare']); + + setTitle('Running hook "prepack"'); + await execa('npm', ['run', '--if-present', 'prepack']); + }); + + if (!dry) { + runHooks.clear(); + } + + const removeHooks = await task('Removing "prepare" & "prepack" hooks', async ({ setWarning }) => { + if (dry) { + setWarning(''); + return; + } + + // Re-read incase hooks modified the package.json + const packageJson = await readJson(packageJsonPath); if (!('scripts' in packageJson)) { return; } @@ -185,18 +203,6 @@ const { stringify } = JSON; const { scripts } = packageJson; let mutated = false; - /** - * Remove "prepack" script - * https://github.com/npm/cli/issues/1229#issuecomment-699528830 - * - * Upon installing a git dependency, the prepack script is run - * without devdependency installation. - */ - if ('prepack' in scripts) { - delete scripts.prepack; - mutated = true; - } - /** * npm uses "prepare" script for git dependencies * because its usually unbuilt. @@ -211,6 +217,18 @@ const { stringify } = JSON; mutated = true; } + /** + * Remove "prepack" script + * https://github.com/npm/cli/issues/1229#issuecomment-699528830 + * + * Upon installing a git dependency, the prepack script is run + * without devdependency installation. + */ + if ('prepack' in scripts) { + delete scripts.prepack; + mutated = true; + } + if (mutated) { await fs.promises.writeFile( packageJsonPath, @@ -220,7 +238,7 @@ const { stringify } = JSON; }); if (!dry) { - removePrepack.clear(); + removeHooks.clear(); } const checkoutBranch = await task(`Checking out branch ${stringify(builtBranch)}`, async ({ setWarning }) => {