UNPKG

3.7 kBJavaScriptView Raw
1const {defaultTo, castArray} = require('lodash');
2const AggregateError = require('aggregate-error');
3const tempy = require('tempy');
4const setLegacyToken = require('./lib/set-legacy-token');
5const getPkg = require('./lib/get-pkg');
6const verifyNpmConfig = require('./lib/verify-config');
7const verifyNpmAuth = require('./lib/verify-auth');
8const addChannelNpm = require('./lib/add-channel');
9const prepareNpm = require('./lib/prepare');
10const publishNpm = require('./lib/publish');
11
12let verified;
13let prepared;
14const npmrc = tempy.file({name: '.npmrc'});
15
16async function verifyConditions(pluginConfig, context) {
17 // 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
18 if (context.options.publish) {
19 const publishPlugin =
20 castArray(context.options.publish).find((config) => config.path && config.path === '@semantic-release/npm') || {};
21
22 pluginConfig.npmPublish = defaultTo(pluginConfig.npmPublish, publishPlugin.npmPublish);
23 pluginConfig.tarballDir = defaultTo(pluginConfig.tarballDir, publishPlugin.tarballDir);
24 pluginConfig.pkgRoot = defaultTo(pluginConfig.pkgRoot, publishPlugin.pkgRoot);
25 }
26
27 const errors = verifyNpmConfig(pluginConfig);
28
29 setLegacyToken(context);
30
31 try {
32 const pkg = await getPkg(pluginConfig, context);
33
34 // Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
35 if (pluginConfig.npmPublish !== false && pkg.private !== true) {
36 await verifyNpmAuth(npmrc, pkg, context);
37 }
38 } catch (error) {
39 errors.push(...error);
40 }
41
42 if (errors.length > 0) {
43 throw new AggregateError(errors);
44 }
45
46 verified = true;
47}
48
49async function prepare(pluginConfig, context) {
50 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
51
52 setLegacyToken(context);
53
54 try {
55 // Reload package.json in case a previous external step updated it
56 const pkg = await getPkg(pluginConfig, context);
57 if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
58 await verifyNpmAuth(npmrc, pkg, context);
59 }
60 } catch (error) {
61 errors.push(...error);
62 }
63
64 if (errors.length > 0) {
65 throw new AggregateError(errors);
66 }
67
68 await prepareNpm(npmrc, pluginConfig, context);
69 prepared = true;
70}
71
72async function publish(pluginConfig, context) {
73 let pkg;
74 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
75
76 setLegacyToken(context);
77
78 try {
79 // Reload package.json in case a previous external step updated it
80 pkg = await getPkg(pluginConfig, context);
81 if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
82 await verifyNpmAuth(npmrc, pkg, context);
83 }
84 } catch (error) {
85 errors.push(...error);
86 }
87
88 if (errors.length > 0) {
89 throw new AggregateError(errors);
90 }
91
92 if (!prepared) {
93 await prepareNpm(npmrc, pluginConfig, context);
94 }
95
96 return publishNpm(npmrc, pluginConfig, pkg, context);
97}
98
99async function addChannel(pluginConfig, context) {
100 let pkg;
101 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
102
103 setLegacyToken(context);
104
105 try {
106 // Reload package.json in case a previous external step updated it
107 pkg = await getPkg(pluginConfig, context);
108 if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
109 await verifyNpmAuth(npmrc, pkg, context);
110 }
111 } catch (error) {
112 errors.push(...error);
113 }
114
115 if (errors.length > 0) {
116 throw new AggregateError(errors);
117 }
118
119 return addChannelNpm(npmrc, pluginConfig, pkg, context);
120}
121
122module.exports = {verifyConditions, prepare, publish, addChannel};