Skip to content

Commit

Permalink
feat: lifecycle hook loop (#5)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: completely new npm script publish hook API
  • Loading branch information
privatenumber authored Nov 1, 2020
1 parent fe718c4 commit 2fff524
Show file tree
Hide file tree
Showing 17 changed files with 8,322 additions and 2,114 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
8 changes: 0 additions & 8 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release
on:
push:
branches: [ master, next, next-major, beta, alpha ]
jobs:
release:
name: Release
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: 14.x
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm run test --if-present
- name: Build
run: npm run build --if-present
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test
on:
push:
branches: [ develop ]
pull_request:
branches: [ develop, master, next, next-major, beta, alpha ]
jobs:
test:
name: Test
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
node-version: [ 10.x, 14.x ]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm run test --if-present
57 changes: 6 additions & 51 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,61 +1,16 @@
# macOS
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history
# Node dependency directory
node_modules

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
.env.test
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v12.18.4
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Hiroki Osame
Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
110 changes: 69 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,84 @@
# <img src="https://upload.wikimedia.org/wikipedia/commons/d/db/Npm-logo.svg" height="20"> multi-publish <a href="https://npm.im/npm-multi-publish"><img src="https://badgen.net/npm/v/npm-multi-publish"></a>
# npm-multi-publish [![Latest version](https://badgen.net/npm/v/npm-multi-publish)](https://npm.im/npm-multi-publish) [![Monthly downloads](https://badgen.net/npm/dm/npm-multi-publish)](https://npm.im/npm-multi-publish) [![Install size](https://packagephobia.now.sh/badge?p=npm-multi-publish)](https://packagephobia.now.sh/result?p=npm-multi-publish)

> Publish a package to multiple npm registries
Publish an npm package to multiple registries

## :raised_hand: Why?
To make a private repo available in multiple private npm registries
## 🙋‍♂️ Why?
- **🔥 High Compatibility** Works with anything that uses `npm publish` or `yarn publish`!
- **⚡️ Easy setup** Just add it to your npm publish hooks!
- **🙌 Streamlined** Publishes to all registries in one `npm publish`!

## :rocket: Installation

#### Global installation
## 🚀 Install
```sh
$ npm i -g npm-multi-publish
npm i -D npm-multi-publish
```
#### Local installation (dev dependency)
```sh
$ npm i -D npm-multi-publish

## 🚦 Quick Setup
Add `npm-multi-publish` to your `package.json` `prepublishOnly` and `postpublish` hooks, and convert `publishConfig` into an array of configs:

```diff
{
...,

"scripts": {
+ "prepublishOnly": "npm-multi-publish",
+ "postpublish": "npm-multi-publish"
},

"publishConfig": [
+ {
+ "registry": "Registry URL 1"
+ },
+ {
+ "registry": "Registry URL 2"
+ },
+ ...
],

...
}
```

Add it to your `package.json` scripts to run it as `$ npm run multi-publish` (be careful not to call it `publish` as it's a [hook](https://docs.npmjs.com/misc/scripts#description))
```json
{
...
"scripts": {
"multi-publish": "multi-publish"
},
...
}
If using [Lerna](https://lerna.js.org/), add this configuration to the respective `package.json` of each package in the monorepo (not necessary in the root `package.json`).

## 💁‍♀️ FAQ

### Is it possible to authenticate to multiple npm registries with one `.npmrc`?

[Yes](https://docs.npmjs.com/logging-in-to-an-npm-enterprise-registry-from-the-command-line#logging-in-with-a-scope-configured-to-point-to-an-npm-enterprise-registry).

To login to an enterprise/custom registry:

```sh
$ npm login --registry=https://registry.company-name.npme.io
```

## :beginner: Setup
1. In your `package.json`, use [`publishConfig`](https://docs.npmjs.com/files/package.json#publishconfig) as an array to define the respective registries
```json
{
...
"publishConfig": [
{
"registry": "..."
},
{
"registry": "..."
}
],
...
}
To verify authentication on a specific registry:

```sh
$ npm whoami --registry=https://registry.company-name.npme.io
```

2. `$ multi-publish` if intalled globally, or `$ npm run multi-publish` if installed locally
If you have certs for the respective registries, you can [add multiple certs to your `.npmrc` file](https://docs.npmjs.com/misc/config#ca).


### How can I manage `.npmrc`s configured for multiple registries?

Use [`npmrc`](https://www.npmjs.com/package/npmrc). When `npm-multi-publish` can't authenticate with a registry, it will wait for you to authenticate (eg. by toggling your npmrc or by logging in).


## :book: FAQ
- _Is it possible to authenticate to multiple npm registries with one `.npmrc`?_
### How can I test publishing to a registry?
Use [`@pnpm/registry-mock`](https://github.com/pnpm/registry-mock/) to create a mock registry.

Yes. Login to a specific registry via `$ npm login --registry=https://registry.company-name.npme.io`. Learn more [here](https://docs.npmjs.com/logging-in-to-an-npm-enterprise-registry-from-the-command-line#logging-in-with-a-scope-configured-to-point-to-an-npm-enterprise-registry). You can check if you're already authenticated to a registry via `$ npm whoami --registry=https://registry.company-name.npme.io`. If you have certs for the respective registries, you can [add multiple certs in your `npmrc` file](https://docs.npmjs.com/misc/config#ca).
Set up a server directory:

- _If I have multiple `.npmrc`s configured for the respective registries, how should I toggle between?_
```sh
$ PNPM_REGISTRY_MOCK_PORT=4873 registry-mock prepare
```

Start the server:

```sh
$ PNPM_REGISTRY_MOCK_PORT=4873 registry-mock
```

Check out [`npmrc`](https://www.npmjs.com/package/npmrc). When multi-publish can't authenticate with a registry, it will wait for you to authenticate (eg. by toggling your npmrc or by logging in).
Use a different port to instantiate multiple test registries.
39 changes: 39 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

'use strict';

const {existsSync} = require('fs');
const prepublishOnly = require('../lib/prepublish-only');
const postpublish = require('../lib/postpublish');
const {
MULTI_PUBLISH_FILE,
readJson,
restorePkg,
Exit,
} = require('../lib/utils');

const {npm_lifecycle_event: lifeCycleEvent} = process.env;

let state;

(async () => {
state = existsSync(MULTI_PUBLISH_FILE) && (await readJson(MULTI_PUBLISH_FILE));

if (lifeCycleEvent === 'prepublishOnly') {
await prepublishOnly(state);
}

if (lifeCycleEvent === 'postpublish') {
await postpublish(state);
}
})().catch(async error => {
if (state) {
await restorePkg(state);
}

if (!(error instanceof Exit)) {
console.error(error);
}

process.exit(1);
});
64 changes: 0 additions & 64 deletions bin/multi-publish.js

This file was deleted.

Loading

0 comments on commit 2fff524

Please sign in to comment.