UNPKG

3.51 kBJavaScriptView Raw
1import { castArray, defaultTo } from "lodash-es";
2import AggregateError from "aggregate-error";
3import { temporaryFile } from "tempy";
4import getPkg from "./lib/get-pkg.js";
5import verifyNpmConfig from "./lib/verify-config.js";
6import verifyNpmAuth from "./lib/verify-auth.js";
7import addChannelNpm from "./lib/add-channel.js";
8import prepareNpm from "./lib/prepare.js";
9import publishNpm from "./lib/publish.js";
10
11let verified;
12let prepared;
13const npmrc = temporaryFile({ name: ".npmrc" });
14
15export async function verifyConditions(pluginConfig, context) {
16 // 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
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 // Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
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
46export async function prepare(pluginConfig, context) {
47 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
48
49 try {
50 // Reload package.json in case a previous external step updated it
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
67export async function publish(pluginConfig, context) {
68 let pkg;
69 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
70
71 try {
72 // Reload package.json in case a previous external step updated it
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
92export async function addChannel(pluginConfig, context) {
93 let pkg;
94 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
95
96 try {
97 // Reload package.json in case a previous external step updated it
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}