UNPKG

2.92 kBJavaScriptView Raw
1const {defaultTo, castArray} = require('lodash');
2const AggregateError = require('aggregate-error');
3const setLegacyToken = require('./lib/set-legacy-token');
4const getPkg = require('./lib/get-pkg');
5const verifyNpmConfig = require('./lib/verify-config');
6const verifyNpmAuth = require('./lib/verify-auth');
7const prepareNpm = require('./lib/prepare');
8const publishNpm = require('./lib/publish');
9
10let verified;
11let prepared;
12
13async function verifyConditions(pluginConfig, context) {
14 // 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
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 // Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
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
43async function prepare(pluginConfig, context) {
44 let pkg;
45 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
46
47 try {
48 // Reload package.json in case a previous external step updated it
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
64async function publish(pluginConfig, context) {
65 let pkg;
66 const errors = verified ? [] : verifyNpmConfig(pluginConfig);
67
68 setLegacyToken();
69
70 try {
71 // Reload package.json in case a previous external step updated it
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
88module.exports = {verifyConditions, prepare, publish};