1 | const {defaultTo, castArray} = require('lodash');
|
2 | const AggregateError = require('aggregate-error');
|
3 | const setLegacyToken = require('./lib/set-legacy-token');
|
4 | const getPkg = require('./lib/get-pkg');
|
5 | const verifyNpmConfig = require('./lib/verify-config');
|
6 | const verifyNpmAuth = require('./lib/verify-auth');
|
7 | const prepareNpm = require('./lib/prepare');
|
8 | const publishNpm = require('./lib/publish');
|
9 |
|
10 | let verified;
|
11 | let prepared;
|
12 |
|
13 | async function verifyConditions(pluginConfig, context) {
|
14 |
|
15 | if (context.options.publish) {
|
16 | const publishPlugin =
|
17 | castArray(context.options.publish).find(config => config.path && config.path === '@semantic-release/npm') || {};
|
18 |
|
19 | pluginConfig.npmPublish = defaultTo(pluginConfig.npmPublish, publishPlugin.npmPublish);
|
20 | pluginConfig.tarballDir = defaultTo(pluginConfig.tarballDir, publishPlugin.tarballDir);
|
21 | pluginConfig.pkgRoot = defaultTo(pluginConfig.pkgRoot, publishPlugin.pkgRoot);
|
22 | }
|
23 |
|
24 | const errors = verifyNpmConfig(pluginConfig);
|
25 |
|
26 | try {
|
27 | const pkg = await getPkg(pluginConfig.pkgRoot);
|
28 |
|
29 |
|
30 | if (pluginConfig.npmPublish !== false && pkg.private !== true) {
|
31 | setLegacyToken();
|
32 | await verifyNpmAuth(pluginConfig, pkg, context);
|
33 | }
|
34 | } catch (err) {
|
35 | errors.push(...err);
|
36 | }
|
37 | if (errors.length > 0) {
|
38 | throw new AggregateError(errors);
|
39 | }
|
40 | verified = true;
|
41 | }
|
42 |
|
43 | async function prepare(pluginConfig, context) {
|
44 | let pkg;
|
45 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
46 |
|
47 | try {
|
48 |
|
49 | pkg = await getPkg(pluginConfig.pkgRoot);
|
50 | if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
|
51 | setLegacyToken();
|
52 | await verifyNpmAuth(pluginConfig, pkg, context);
|
53 | }
|
54 | } catch (err) {
|
55 | errors.push(...err);
|
56 | }
|
57 | if (errors.length > 0) {
|
58 | throw new AggregateError(errors);
|
59 | }
|
60 | await prepareNpm(pluginConfig, context);
|
61 | prepared = true;
|
62 | }
|
63 |
|
64 | async function publish(pluginConfig, context) {
|
65 | let pkg;
|
66 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
67 |
|
68 | setLegacyToken();
|
69 |
|
70 | try {
|
71 |
|
72 | pkg = await getPkg(pluginConfig.pkgRoot);
|
73 | if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
|
74 | await verifyNpmAuth(pluginConfig, pkg, context);
|
75 | }
|
76 | } catch (err) {
|
77 | errors.push(...err);
|
78 | }
|
79 | if (errors.length > 0) {
|
80 | throw new AggregateError(errors);
|
81 | }
|
82 | if (!prepared) {
|
83 | await prepareNpm(pluginConfig, context);
|
84 | }
|
85 | return publishNpm(pluginConfig, pkg, context);
|
86 | }
|
87 |
|
88 | module.exports = {verifyConditions, prepare, publish};
|