1 | import { castArray, defaultTo } from "lodash-es";
|
2 | import AggregateError from "aggregate-error";
|
3 | import { temporaryFile } from "tempy";
|
4 | import getPkg from "./lib/get-pkg.js";
|
5 | import verifyNpmConfig from "./lib/verify-config.js";
|
6 | import verifyNpmAuth from "./lib/verify-auth.js";
|
7 | import addChannelNpm from "./lib/add-channel.js";
|
8 | import prepareNpm from "./lib/prepare.js";
|
9 | import publishNpm from "./lib/publish.js";
|
10 |
|
11 | let verified;
|
12 | let prepared;
|
13 | const npmrc = temporaryFile({ name: ".npmrc" });
|
14 |
|
15 | export async function verifyConditions(pluginConfig, context) {
|
16 |
|
17 | if (context.options.publish) {
|
18 | const publishPlugin =
|
19 | castArray(context.options.publish).find((config) => config.path && config.path === "@semantic-release/npm") || {};
|
20 |
|
21 | pluginConfig.npmPublish = defaultTo(pluginConfig.npmPublish, publishPlugin.npmPublish);
|
22 | pluginConfig.tarballDir = defaultTo(pluginConfig.tarballDir, publishPlugin.tarballDir);
|
23 | pluginConfig.pkgRoot = defaultTo(pluginConfig.pkgRoot, publishPlugin.pkgRoot);
|
24 | }
|
25 |
|
26 | const errors = verifyNpmConfig(pluginConfig);
|
27 |
|
28 | try {
|
29 | const pkg = await getPkg(pluginConfig, context);
|
30 |
|
31 |
|
32 | if (pluginConfig.npmPublish !== false && pkg.private !== true) {
|
33 | await verifyNpmAuth(npmrc, pkg, context);
|
34 | }
|
35 | } catch (error) {
|
36 | errors.push(...error.errors);
|
37 | }
|
38 |
|
39 | if (errors.length > 0) {
|
40 | throw new AggregateError(errors);
|
41 | }
|
42 |
|
43 | verified = true;
|
44 | }
|
45 |
|
46 | export async function prepare(pluginConfig, context) {
|
47 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
48 |
|
49 | try {
|
50 |
|
51 | const pkg = await getPkg(pluginConfig, context);
|
52 | if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
|
53 | await verifyNpmAuth(npmrc, pkg, context);
|
54 | }
|
55 | } catch (error) {
|
56 | errors.push(...error.errors);
|
57 | }
|
58 |
|
59 | if (errors.length > 0) {
|
60 | throw new AggregateError(errors);
|
61 | }
|
62 |
|
63 | await prepareNpm(npmrc, pluginConfig, context);
|
64 | prepared = true;
|
65 | }
|
66 |
|
67 | export async function publish(pluginConfig, context) {
|
68 | let pkg;
|
69 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
70 |
|
71 | try {
|
72 |
|
73 | pkg = await getPkg(pluginConfig, context);
|
74 | if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
|
75 | await verifyNpmAuth(npmrc, pkg, context);
|
76 | }
|
77 | } catch (error) {
|
78 | errors.push(...error.errors);
|
79 | }
|
80 |
|
81 | if (errors.length > 0) {
|
82 | throw new AggregateError(errors);
|
83 | }
|
84 |
|
85 | if (!prepared) {
|
86 | await prepareNpm(npmrc, pluginConfig, context);
|
87 | }
|
88 |
|
89 | return publishNpm(npmrc, pluginConfig, pkg, context);
|
90 | }
|
91 |
|
92 | export async function addChannel(pluginConfig, context) {
|
93 | let pkg;
|
94 | const errors = verified ? [] : verifyNpmConfig(pluginConfig);
|
95 |
|
96 | try {
|
97 |
|
98 | pkg = await getPkg(pluginConfig, context);
|
99 | if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
|
100 | await verifyNpmAuth(npmrc, pkg, context);
|
101 | }
|
102 | } catch (error) {
|
103 | errors.push(...error.errors);
|
104 | }
|
105 |
|
106 | if (errors.length > 0) {
|
107 | throw new AggregateError(errors);
|
108 | }
|
109 |
|
110 | return addChannelNpm(npmrc, pluginConfig, pkg, context);
|
111 | }
|