-
Notifications
You must be signed in to change notification settings - Fork 7
/
releaseTypes.mjs
166 lines (143 loc) · 4.65 KB
/
releaseTypes.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import path from "node:path";
import AggregateError from "aggregate-error";
import { temporaryFile } from "tempy";
import SemanticReleaseError from "@semantic-release/error";
import { add } from "@semantic-release/git/lib/git.js";
import {
getError,
getPkg,
verifyNpmAuth,
npmVersion,
npmDistTag,
npmPublish,
} from "./scripts/npm/index.mjs";
/**
* @typedef {import("./scripts/npm/types").PluginConfig} PluginConfig
*/
/**
* @typedef {Object} OptionsContext
* @prop {import("semantic-release").GlobalConfig} options
*/
let verified = false;
let prepared = false;
const typesPath = path.resolve(process.cwd(), "types");
const npmrc = temporaryFile({ name: ".npmrc" });
/**
* @param {T} context
* @template T
* @returns {T}
*/
function typeContext(context) {
context.cwd = typesPath;
return context;
}
/**
* Check if the plugin is defined before the plugin `@semantic-release/git` if it's defined.
*
* @param {import("semantic-release").VerifyConditionsContext & OptionsContext} context semantic-release context.
*/
function checkPluginsOrder(context) {
const pluginsList = context.options.plugins.map((plugin) =>
Array.isArray(plugin) ? plugin[0] : plugin,
);
const gitPluginIndex = pluginsList.indexOf("@semantic-release/git");
if (gitPluginIndex === -1) {
return;
}
const pluginIndex = pluginsList.findIndex((plugin) =>
plugin.includes(path.basename(import.meta.url)),
);
if (pluginIndex > gitPluginIndex) {
throw new SemanticReleaseError(
"This plugin should be defined before plugin `@semantic-release/git`",
"EINVALIDSETUP",
);
}
}
/**
* Verify the npm configuration and plugin setup:
* - The plugin should be define before the plugin `@semantic-release/git` if it's defined.
*
* @param {PluginConfig} pluginConfig The plugin configuration.
* @param {import("semantic-release").VerifyConditionsContext & OptionsContext} ctx semantic-release context.
*/
export async function verifyConditions(pluginConfig, ctx) {
const context = typeContext(ctx);
const errors = [];
try {
checkPluginsOrder(context);
} catch (error) {
errors.push(error);
}
// ? Check if `npmPublish` is boolean
if (
pluginConfig.npmPublish !== undefined &&
pluginConfig.npmPublish !== true &&
pluginConfig.npmPublish !== false
) {
errors.push(getError("EINVALIDNPMPUBLISH", pluginConfig));
}
try {
if (pluginConfig.npmPublish !== false) {
const pkg = await getPkg(context.cwd);
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
errors.push(error);
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
verified = true;
}
/**
* Prepare the npm package for release
*
* @param {PluginConfig} pluginConfig The plugin configuration.
* @param {import("semantic-release").PrepareContext & OptionsContext} ctx semantic-release context.
*/
export async function prepare(pluginConfig, ctx) {
const context = typeContext(ctx);
const { env, cwd } = context;
await npmVersion(npmrc, context);
// ? Can't modify assets configuration in a plugin, so manually add modified `package.json` in git index
// ? `@semantic-release/git` make release commit with the other assets files after, this is why this plugin should be defined before him
await add([path.join(typesPath, "package.json")], { cwd, env });
prepared = true;
}
/**
* Publish the npm package to the registry
*
* @param {PluginConfig} pluginConfig The plugin configuration.
* @param {import("semantic-release").PublishContext & OptionsContext} ctx semantic-release context.
*/
export async function publish(pluginConfig, ctx) {
const context = typeContext(ctx);
if (pluginConfig.npmPublish === false) {
context.logger.log(
`Skip publishing to npm registry as npmPublish is false`,
);
return false;
}
// Reload package.json in case a previous external step updated it
const pkg = await getPkg(context.cwd);
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
if (!prepared) {
await prepare(pluginConfig, context);
}
return npmPublish(npmrc, pluginConfig, pkg, context);
}
/**
* Tag npm published version with release channel (eg: `beta`) or `latest` for main releases
*
* @param {PluginConfig} pluginConfig The plugin configuration.
* @param {import("semantic-release").AddChannelContext & OptionsContext} ctx semantic-release context.
*/
export async function addChannel(pluginConfig, ctx) {
const context = typeContext(ctx);
// Reload package.json in case a previous external step updated it
const pkg = await getPkg(context.cwd);
return npmDistTag(npmrc, pluginConfig, pkg, context);
}