@bintoss/semantic-release-npm-multiple 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -0
  3. package/index.js +118 -0
  4. package/package.json +50 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Amanda Mitchell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # @bintoss/semantic-release-npm-multiple
2
+
3
+ This is a thin wrapper around the [@semantic-release/npm](https://github.com/semantic-release/npm) plugin for [Semantic Release](https://semantic-release.gitbook.io/semantic-release/) that allows it to be called multiple times, which can be useful if you need to publish to multiple NPM registries simultaneously.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add --dev @bintoss/semantic-release-npm-multiple
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ The plugin can be configured in the [**semantic-release** configuration file](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#configuration):
14
+
15
+ ```json
16
+ {
17
+ "plugins": [
18
+ "@semantic-release/commit-analyzer",
19
+ "@semantic-release/release-notes-generator",
20
+ [
21
+ "@bintoss/semantic-release-npm-multiple",
22
+ {
23
+ "registries": {
24
+ "registryName1": {
25
+ "npmPublish": true
26
+ },
27
+ "registryName2": {
28
+ "npmPublish": true
29
+ }
30
+ }
31
+ }
32
+ ]
33
+ ]
34
+ }
35
+ ```
36
+
37
+ ## Configuration
38
+
39
+ Each of the keys in `registries` refers to a specific registry that should be used and may be any value that is meaningful to you.
40
+ The object associated with that key is a set of options that should be passed to the `@semantic-release/npm` plugin when calling it.
41
+
42
+ `@bintoss/semantic-release-npm-multiple` also looks at a number of environment variables for its configuration:
43
+
44
+ - `NPM_TOKEN`
45
+ - `NPM_USERNAME`
46
+ - `NPM_PASSWORD`
47
+ - `NPM_EMAIL`
48
+ - `NPM_CONFIG_REGISTRY`
49
+ - `NPM_CONFIG_USERCONFIG`
50
+
51
+ For any of these variables, if you define a `{UPPER_CASE_REGISTRY_NAME}_{VARIABLE}` environment variable, it will be used instead.
52
+
53
+ For example, if you wanted to publish a package to both a GitHub private registry and the public NPM registry, you would first create a configuration file like this:
54
+
55
+ ```json
56
+ {
57
+ "plugins": [
58
+ "@semantic-release/commit-analyzer",
59
+ "@semantic-release/release-notes-generator",
60
+ [
61
+ "@bintoss/semantic-release-npm-multiple",
62
+ {
63
+ "registries": {
64
+ "github": {},
65
+ "public": {}
66
+ }
67
+ }
68
+ ]
69
+ ]
70
+ }
71
+ ```
72
+
73
+ And then, when running `semantic-release`, set these environment variables:
74
+
75
+ - `GITHUB_NPM_CONFIG_REGISTRY=https://npm.pkg.github.com/`
76
+ - `GITHUB_NPM_TOKEN=XXXXX`
77
+ - `PUBLIC_NPM_CONFIG_REGISTRY=https://registry.npmjs.org`
78
+ - `PUBLIC_NPM_TOKEN=XXXXX`
package/index.js ADDED
@@ -0,0 +1,118 @@
1
+ // @ts-check
2
+ const underlyingPluginSpecifier = '@semantic-release/npm';
3
+
4
+ // We use the safe navigation operator because when this module
5
+ // is executed by jest, import.meta.resolve is unavailable.
6
+ const resolvedNpm = import.meta.resolve?.(underlyingPluginSpecifier);
7
+
8
+ /**
9
+ * @typedef {Promise<unknown>} PluginPromise
10
+ * @typedef {Record<string, PluginPromise>} RegistryPlugins
11
+ * @import {BaseContext, VerifyConditionsContext} from 'semantic-release'
12
+ */
13
+
14
+ /** @type {RegistryPlugins} */
15
+ const registryPlugins = {};
16
+ /**
17
+ * @param {string } registryName
18
+ */
19
+ async function getChildPlugin(registryName) {
20
+ let plugin = registryPlugins[registryName];
21
+ if (!plugin) {
22
+ // What's going on here?
23
+ //
24
+ // @semantic-release/npm maintains some module-level state (specifically,
25
+ // a temporary file that acts as an npmrc), which means that we can't just
26
+ // call the same plugin lifecycle methods repeatedly because they would
27
+ // stomp on the shared state.
28
+ //
29
+ // Fortunately, node has a way to deliberately suppress its normal module
30
+ // caching behavior, which allows us to load multiple copies of a single module:
31
+ // by appending a query string to the specifier, node will load one copy of the
32
+ // module per query string.
33
+ //
34
+ // But there's a twist: node *doesn't* support this behavior on "bare specifiers",
35
+ // which is how we usually import other packages. In order to get around this,
36
+ // we must use import.meta.resolve in order to transform the bare specifier into
37
+ // a file url specifier.
38
+ //
39
+ // But then there's a double twist! Jest doesn't support import.meta.resolve, but
40
+ // it *does* support query strings on bare specifiers. So in order to be compatible
41
+ // with both tests and actual usage, we have this unsightly kludge.
42
+ const importSpecifier = `${resolvedNpm || underlyingPluginSpecifier}?registry=${encodeURIComponent(registryName)}`;
43
+
44
+ plugin = import(importSpecifier);
45
+ registryPlugins[registryName] = plugin;
46
+ }
47
+
48
+ return await plugin;
49
+ }
50
+
51
+ /**
52
+ * @param {string} callbackName
53
+ * @returns { (
54
+ * arg0: {
55
+ * registries: Record<string, object>,
56
+ * [key:string]: unknown
57
+ * },
58
+ * context: {
59
+ * logger: BaseContext['logger'],
60
+ * env?: VerifyConditionsContext['env'] | undefined
61
+ * }
62
+ * ) => Promise<void> }
63
+ */
64
+ function createCallbackWrapper(callbackName) {
65
+ return async ({ registries, ...pluginConfig }, context) => {
66
+ for (const [registryName, childConfig] of Object.entries(
67
+ registries || {},
68
+ )) {
69
+ const plugin = await getChildPlugin(registryName);
70
+ if (!plugin || typeof plugin !== 'object') {
71
+ context.logger.error(
72
+ 'plugin is null or undefined or is not an object.',
73
+ );
74
+ return;
75
+ }
76
+
77
+ const callback = Reflect.get(plugin, callbackName) ?? undefined;
78
+ if (!callback) {
79
+ context.logger.error(`${callbackName} does not exist in plugin!`);
80
+ return;
81
+ }
82
+
83
+ context.logger.log(
84
+ `Performing ${callbackName} for registry ${registryName}`,
85
+ );
86
+
87
+ const environmentVariablePrefix = `${registryName.toUpperCase()}_`;
88
+ const env = context.env ?? {};
89
+ const childEnv = { ...env };
90
+
91
+ for (const variableName of [
92
+ 'NPM_TOKEN',
93
+ 'NPM_USERNAME',
94
+ 'NPM_PASSWORD',
95
+ 'NPM_EMAIL',
96
+ 'NPM_CONFIG_REGISTRY',
97
+ 'NPM_CONFIG_USERCONFIG',
98
+ ]) {
99
+ const overriddenValue = env[environmentVariablePrefix + variableName];
100
+ if (overriddenValue) {
101
+ childEnv[variableName] = overriddenValue;
102
+ }
103
+ }
104
+
105
+ await callback(
106
+ { ...childConfig, ...pluginConfig },
107
+ { ...context, env: childEnv },
108
+ );
109
+ }
110
+ };
111
+ }
112
+
113
+ export default {
114
+ addChannel: createCallbackWrapper('addChannel'),
115
+ prepare: createCallbackWrapper('prepare'),
116
+ publish: createCallbackWrapper('publish'),
117
+ verifyConditions: createCallbackWrapper('verifyConditions'),
118
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@bintoss/semantic-release-npm-multiple",
3
+ "version": "0.0.0",
4
+ "description": "A semantic release plugin to publish an NPM package to multiple registries.",
5
+ "main": "index.js",
6
+ "repository": "https://github.com/bintoss/semantic-release-npm-multiple",
7
+ "author": "Amanda Mitchell <she@mandymitchell.me>",
8
+ "contributors": [
9
+ "Noah 'bintoss' Sherwin <noahrsherwin@gmail.com>"
10
+ ],
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "files": [
14
+ "index.js"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "keywords": [
20
+ "npm",
21
+ "publish",
22
+ "registry",
23
+ "semantic-release",
24
+ "version"
25
+ ],
26
+ "scripts": {
27
+ "_postinstall": "husky",
28
+ "prepack": "pinst --disable",
29
+ "postpack": "pinst --enable",
30
+ "test": "node --test"
31
+ },
32
+ "devDependencies": {
33
+ "@commitlint/config-conventional": "^20.0.0",
34
+ "@types/node": "^25.0.10",
35
+ "commitlint": "^20.0.0",
36
+ "husky": "^9.1.7",
37
+ "lint-staged": "^16.0.0",
38
+ "pinst": "^3.0.0",
39
+ "prettier": "^3.0.2",
40
+ "semantic-release": "^25.0.2",
41
+ "typescript": "^5.9.3"
42
+ },
43
+ "dependencies": {
44
+ "@semantic-release/npm": "^13.0.0"
45
+ },
46
+ "peerDependencies": {
47
+ "semantic-release": ">=19.0.0"
48
+ },
49
+ "packageManager": "yarn@4.12.0+sha512.f45ab632439a67f8bc759bf32ead036a1f413287b9042726b7cc4818b7b49e14e9423ba49b18f9e06ea4941c1ad062385b1d8760a8d5091a1a31e5f6219afca8"
50
+ }