Skip to content

Commit

Permalink
feat: run npm hooks (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jul 14, 2022
1 parent d128690 commit 02a0f71
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -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?

Expand Down
48 changes: 33 additions & 15 deletions src/index.ts
Expand Up @@ -109,7 +109,6 @@ const { stringify } = JSON;
throw new Error('No package.json found');
}

const packageJson = await readJson(packageJsonPath);
const {
builtBranch = `built/${branchFrom}`,
buildCommand,
Expand Down Expand Up @@ -172,31 +171,38 @@ 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;
}

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.
Expand All @@ -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,
Expand All @@ -220,7 +238,7 @@ const { stringify } = JSON;
});

if (!dry) {
removePrepack.clear();
removeHooks.clear();
}

const checkoutBranch = await task(`Checking out branch ${stringify(builtBranch)}`, async ({ setWarning }) => {
Expand Down

0 comments on commit 02a0f71

Please sign in to comment.