1 | const {defaultTo, castArray} = require('lodash');
|
2 | const AggregateError = require('aggregate-error');
|
3 | const tempy = require('tempy');
|
4 | const setLegacyToken = require('./lib/set-legacy-token');
|
5 | const getPkg = require('./lib/get-pkg');
|
6 | const verifyNpmConfig = require('./lib/verify-config');
|
7 | const verifyNpmAuth = require('./lib/verify-auth');
|
8 | const addChannelNpm = require('./lib/add-channel');
|
9 | const prepareNpm = require('./lib/prepare');
|
10 | const publishNpm = require('./lib/publish');
|
11 |
|
12 | let verified;
|
13 | let prepared;
|
14 | const npmrc = tempy.file({name: '.npmrc'});
|
15 |
|
16 | async function verifyConditions(pluginConfig, context) {
|
17 |
|
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 |
|
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 |
|
49 | async function prepare(pluginConfig, context) {
|
50 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
51 |
|
52 | setLegacyToken(context);
|
53 |
|
54 | try {
|
55 |
|
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 |
|
72 | async function publish(pluginConfig, context) {
|
73 | let pkg;
|
74 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
75 |
|
76 | setLegacyToken(context);
|
77 |
|
78 | try {
|
79 |
|
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 |
|
99 | async function addChannel(pluginConfig, context) {
|
100 | let pkg;
|
101 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
102 |
|
103 | setLegacyToken(context);
|
104 |
|
105 | try {
|
106 |
|
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 |
|
122 | module.exports = {verifyConditions, prepare, publish, addChannel};
|