forked from g-ongenae/semantic-release-npm-multiple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (57 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const requireReload = require('require-reload');
const reload = requireReload(require);
// The @semantic-release/npm plugin maintains
// some state at the module level to decide where to
// store its .npmrc file.
// Because of this, we have to monkey around a bit with
// Node's require cache in order to create multiple copies
// of the module in order to use it with different configurations.
const registryPlugins = {};
function getChildPlugin(registryName) {
let plugin = registryPlugins[registryName];
if (!plugin) {
plugin = reload('@semantic-release/npm');
registryPlugins[registryName] = plugin;
}
return plugin;
}
function createCallbackWrapper(callbackName) {
return async ({ registries, ...pluginConfig }, context) => {
for (const [registryName, childConfig] of Object.entries(
registries || {},
)) {
const callback = getChildPlugin(registryName)[callbackName];
if (!callback) {
return;
}
context.logger.log(
`Performing ${callbackName} for registry ${registryName}`,
);
const environmentVariablePrefix = `${registryName.toUpperCase()}_`;
const { env } = context;
const childEnv = { ...env };
for (const variableName of [
'NPM_TOKEN',
'NPM_USERNAME',
'NPM_PASSWORD',
'NPM_EMAIL',
'NPM_CONFIG_REGISTRY',
'NPM_CONFIG_USERCONFIG',
]) {
const overridenValue = env[environmentVariablePrefix + variableName];
if (overridenValue) {
childEnv[variableName] = overridenValue;
}
}
await callback(
{ ...childConfig, ...pluginConfig },
{ ...context, env: childEnv },
);
}
};
}
const callbackNames = ['verify', 'prepare', 'publish', 'success', 'fail'];
module.exports = Object.assign(
{},
...callbackNames.map(name => ({ [name]: createCallbackWrapper(name) })),
);