forked from semantic-release/npm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (96 loc) · 3.61 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const {defaultTo, castArray} = require('lodash');
const AggregateError = require('aggregate-error');
const tempy = require('tempy');
const setLegacyToken = require('./lib/set-legacy-token');
const getPkg = require('./lib/get-pkg');
const verifyNpmConfig = require('./lib/verify-config');
const verifyNpmAuth = require('./lib/verify-auth');
const addChannelNpm = require('./lib/add-channel');
const prepareNpm = require('./lib/prepare');
const publishNpm = require('./lib/publish');
let verified;
let prepared;
const npmrc = tempy.file({name: '.npmrc'});
async function verifyConditions(pluginConfig, context) {
// If the npm publish plugin is used and has `npmPublish`, `tarballDir` or `pkgRoot` configured, validate them now in order to prevent any release if the configuration is wrong
if (context.options.publish) {
const publishPlugin =
castArray(context.options.publish).find((config) => config.path && config.path === '@semantic-release/npm') || {};
pluginConfig.npmPublish = defaultTo(pluginConfig.npmPublish, publishPlugin.npmPublish);
pluginConfig.tarballDir = defaultTo(pluginConfig.tarballDir, publishPlugin.tarballDir);
pluginConfig.pkgRoot = defaultTo(pluginConfig.pkgRoot, publishPlugin.pkgRoot);
}
const errors = verifyNpmConfig(pluginConfig);
setLegacyToken(context);
try {
const pkg = await getPkg(pluginConfig, context);
// Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
if (pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
errors.push(...error);
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
verified = true;
}
async function prepare(pluginConfig, context) {
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
setLegacyToken(context);
try {
// Reload package.json in case a previous external step updated it
const pkg = await getPkg(pluginConfig, context);
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
errors.push(...error);
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
await prepareNpm(npmrc, pluginConfig, context);
prepared = true;
}
async function publish(pluginConfig, context) {
let pkg;
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
setLegacyToken(context);
try {
// Reload package.json in case a previous external step updated it
pkg = await getPkg(pluginConfig, context);
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
errors.push(...error);
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
if (!prepared) {
await prepareNpm(npmrc, pluginConfig, context);
}
return publishNpm(npmrc, pluginConfig, pkg, context);
}
async function addChannel(pluginConfig, context) {
let pkg;
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
setLegacyToken(context);
try {
// Reload package.json in case a previous external step updated it
pkg = await getPkg(pluginConfig, context);
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
errors.push(...error);
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
return addChannelNpm(npmrc, pluginConfig, pkg, context);
}
module.exports = {verifyConditions, prepare, publish, addChannel};