@vitejs/release-scripts 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present, Vite contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @vitejs/release-scripts
2
+
3
+ This repo is used to share release & publish scripts for the org. Scripts should be executed from the workspace root via `tsx scripts/release.ts`
4
+
5
+ ## release
6
+
7
+ ```ts
8
+ import { release } from "@vitejs/release-scripts";
9
+
10
+ release({
11
+ // Name of the repo for CI link
12
+ repo: "release-scripts",
13
+ // List of options. Choice will be available in following callback as `pkg`
14
+ packages: ["release-scripts"],
15
+ toTag: (pkg, version) =>
16
+ pkg === "vite" ? `v${version}` : `${pkg}@${version}`,
17
+ // Not shared until we find a new changelog process
18
+ logChangelog: (pkg) =>
19
+ console.log(
20
+ execSync(
21
+ "git log $(git describe --tags --abbrev=0)..HEAD --oneline",
22
+ ).toString(),
23
+ ),
24
+ generateChangelog: (pkg, version) => {},
25
+ // use getPkgDir when not using a monorepo. Default to `packages/${pkg}`
26
+ getPkgDir: (pkg) => ".",
27
+ });
28
+ ```
29
+
30
+ ## publish
31
+
32
+ ```ts
33
+ import { publish } from "@vitejs/release-scripts";
34
+
35
+ publish({
36
+ // Used when tag is not `pkg@version`
37
+ defaultPackage: "release-scripts",
38
+ // use getPkgDir when not in a monorepo. Default to `packages/${pkg}`
39
+ getPkgDir: (pkg) => ".",
40
+ });
41
+ ```
@@ -0,0 +1,13 @@
1
+ export declare function publish(options: {
2
+ defaultPackage: string;
3
+ getPkgDir?: (pkg: string) => string;
4
+ }): Promise<void>;
5
+
6
+ export declare function release(options: {
7
+ repo: string;
8
+ packages: string[];
9
+ logChangelog: (pkg: string) => void | Promise<void>;
10
+ generateChangelog: (pkg: string, version: string) => void | Promise<void>;
11
+ toTag: (pkg: string, version: string) => string;
12
+ getPkgDir?: (pkg: string) => string;
13
+ }): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,279 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ publish: () => publish,
34
+ release: () => release
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+
38
+ // src/publish.ts
39
+ var import_semver2 = __toESM(require("semver"));
40
+
41
+ // src/utils.ts
42
+ var import_node_fs = require("node:fs");
43
+ var import_node_path = __toESM(require("node:path"));
44
+ var import_picocolors = __toESM(require("picocolors"));
45
+ var import_execa = require("execa");
46
+ var import_semver = __toESM(require("semver"));
47
+ var import_minimist = __toESM(require("minimist"));
48
+ var args = (0, import_minimist.default)(process.argv.slice(2));
49
+ var isDryRun = !!args.dry;
50
+ if (isDryRun) {
51
+ console.log(import_picocolors.default.inverse(import_picocolors.default.yellow(" DRY RUN ")));
52
+ console.log();
53
+ }
54
+ function getPackageInfo(pkgName, getPkgDir = (pkg) => `packages/${pkg}`) {
55
+ const pkgDir = getPkgDir(pkgName);
56
+ const pkgPath = import_node_path.default.resolve(pkgDir, "package.json");
57
+ const pkg = require(pkgPath);
58
+ const currentVersion = pkg.version;
59
+ if (pkg.private) {
60
+ throw new Error(`Package ${pkgName} is private`);
61
+ }
62
+ return { npmName: pkg.name, pkgDir, pkgPath, currentVersion };
63
+ }
64
+ async function run(bin, args2, opts = {}) {
65
+ return (0, import_execa.execa)(bin, args2, { stdio: "inherit", ...opts });
66
+ }
67
+ async function dryRun(bin, args2, opts) {
68
+ return console.log(
69
+ import_picocolors.default.blue(`[dryrun] ${bin} ${args2.join(" ")}`),
70
+ opts || ""
71
+ );
72
+ }
73
+ var runIfNotDry = isDryRun ? dryRun : run;
74
+ function step(msg) {
75
+ return console.log(import_picocolors.default.cyan(msg));
76
+ }
77
+ function getVersionChoices(currentVersion) {
78
+ const currentBeta = currentVersion.includes("beta");
79
+ const currentAlpha = currentVersion.includes("alpha");
80
+ const isStable = !currentBeta && !currentAlpha;
81
+ function inc(i, tag = currentAlpha ? "alpha" : "beta") {
82
+ return import_semver.default.inc(currentVersion, i, tag);
83
+ }
84
+ let versionChoices = [
85
+ {
86
+ title: "next",
87
+ value: inc(isStable ? "patch" : "prerelease")
88
+ }
89
+ ];
90
+ if (isStable) {
91
+ versionChoices.push(
92
+ {
93
+ title: "beta-minor",
94
+ value: inc("preminor")
95
+ },
96
+ {
97
+ title: "beta-major",
98
+ value: inc("premajor")
99
+ },
100
+ {
101
+ title: "alpha-minor",
102
+ value: inc("preminor", "alpha")
103
+ },
104
+ {
105
+ title: "alpha-major",
106
+ value: inc("premajor", "alpha")
107
+ },
108
+ {
109
+ title: "minor",
110
+ value: inc("minor")
111
+ },
112
+ {
113
+ title: "major",
114
+ value: inc("major")
115
+ }
116
+ );
117
+ } else if (currentAlpha) {
118
+ versionChoices.push({
119
+ title: "beta",
120
+ value: inc("patch") + "-beta.0"
121
+ });
122
+ } else {
123
+ versionChoices.push({
124
+ title: "stable",
125
+ value: inc("patch")
126
+ });
127
+ }
128
+ versionChoices.push({ value: "custom", title: "custom" });
129
+ versionChoices = versionChoices.map((i) => {
130
+ i.title = `${i.title} (${i.value})`;
131
+ return i;
132
+ });
133
+ return versionChoices;
134
+ }
135
+ function updateVersion(pkgPath, version) {
136
+ const pkg = JSON.parse((0, import_node_fs.readFileSync)(pkgPath, "utf-8"));
137
+ pkg.version = version;
138
+ (0, import_node_fs.writeFileSync)(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
139
+ }
140
+ async function publishPackage(pkdDir, tag) {
141
+ const publicArgs = ["publish", "--access", "public"];
142
+ if (tag) {
143
+ publicArgs.push(`--tag`, tag);
144
+ }
145
+ await runIfNotDry("npm", publicArgs, {
146
+ cwd: pkdDir
147
+ });
148
+ }
149
+ async function getActiveVersion(npmName) {
150
+ try {
151
+ return (await run("npm", ["info", npmName, "version"], { stdio: "pipe" })).stdout;
152
+ } catch (e) {
153
+ if (e.stderr.startsWith("npm ERR! code E404"))
154
+ return;
155
+ throw e;
156
+ }
157
+ }
158
+
159
+ // src/publish.ts
160
+ var publish = async ({ defaultPackage, getPkgDir }) => {
161
+ const tag = args._[0];
162
+ if (!tag)
163
+ throw new Error("No tag specified");
164
+ let pkgName = defaultPackage;
165
+ let version;
166
+ if (tag.includes("@"))
167
+ [pkgName, version] = tag.split("@");
168
+ else
169
+ version = tag;
170
+ if (version.startsWith("v"))
171
+ version = version.slice(1);
172
+ const { npmName, currentVersion, pkgDir } = getPackageInfo(
173
+ pkgName,
174
+ getPkgDir
175
+ );
176
+ if (currentVersion !== version)
177
+ throw new Error(
178
+ `Package version from tag "${version}" mismatches with current version "${currentVersion}"`
179
+ );
180
+ const activeVersion = await getActiveVersion(npmName);
181
+ step("Publishing package...");
182
+ const releaseTag = version.includes("beta") ? "beta" : version.includes("alpha") ? "alpha" : activeVersion && import_semver2.default.lt(currentVersion, activeVersion) ? "previous" : void 0;
183
+ await publishPackage(pkgDir, releaseTag);
184
+ };
185
+
186
+ // src/release.ts
187
+ var import_prompts = __toESM(require("prompts"));
188
+ var import_semver3 = __toESM(require("semver"));
189
+ var import_picocolors2 = __toESM(require("picocolors"));
190
+ var release = async ({
191
+ repo,
192
+ packages,
193
+ logChangelog,
194
+ generateChangelog,
195
+ toTag,
196
+ getPkgDir
197
+ }) => {
198
+ let targetVersion;
199
+ const pkg = packages.length === 1 ? packages[0] : (await (0, import_prompts.default)({
200
+ type: "select",
201
+ name: "pkg",
202
+ message: "Select package",
203
+ choices: packages.map((i) => ({ value: i, title: i }))
204
+ })).pkg;
205
+ if (!pkg)
206
+ return;
207
+ await logChangelog(pkg);
208
+ const { currentVersion, pkgPath } = getPackageInfo(pkg, getPkgDir);
209
+ if (!targetVersion) {
210
+ const { release: release2 } = await (0, import_prompts.default)({
211
+ type: "select",
212
+ name: "release",
213
+ message: "Select release type",
214
+ choices: getVersionChoices(currentVersion)
215
+ });
216
+ if (release2 === "custom") {
217
+ const res = await (0, import_prompts.default)({
218
+ type: "text",
219
+ name: "version",
220
+ message: "Input custom version",
221
+ initial: currentVersion
222
+ });
223
+ targetVersion = res.version;
224
+ } else {
225
+ targetVersion = release2;
226
+ }
227
+ }
228
+ if (!import_semver3.default.valid(targetVersion)) {
229
+ throw new Error(`invalid target version: ${targetVersion}`);
230
+ }
231
+ const tag = toTag(pkg, targetVersion);
232
+ if (targetVersion.includes("beta") && !args.tag) {
233
+ args.tag = "beta";
234
+ }
235
+ if (targetVersion.includes("alpha") && !args.tag) {
236
+ args.tag = "alpha";
237
+ }
238
+ const { yes } = await (0, import_prompts.default)({
239
+ type: "confirm",
240
+ name: "yes",
241
+ message: `Releasing ${import_picocolors2.default.yellow(tag)} Confirm?`
242
+ });
243
+ if (!yes)
244
+ return;
245
+ step("\nUpdating package version...");
246
+ updateVersion(pkgPath, targetVersion);
247
+ await generateChangelog(pkg, targetVersion);
248
+ const { stdout } = await run("git", ["diff"], { stdio: "pipe" });
249
+ if (stdout) {
250
+ step("\nCommitting changes...");
251
+ await runIfNotDry("git", ["add", "-A"]);
252
+ await runIfNotDry("git", ["commit", "-m", `release: ${tag}`]);
253
+ await runIfNotDry("git", ["tag", tag]);
254
+ } else {
255
+ console.log("No changes to commit.");
256
+ return;
257
+ }
258
+ step("\nPushing to GitHub...");
259
+ await runIfNotDry("git", ["push", "origin", `refs/tags/${tag}`]);
260
+ await runIfNotDry("git", ["push"]);
261
+ if (isDryRun) {
262
+ console.log(`
263
+ Dry run finished - run git diff to see package changes.`);
264
+ } else {
265
+ console.log(
266
+ import_picocolors2.default.green(
267
+ `
268
+ Pushed, publishing should starts shortly on CI.
269
+ https://github.com/vitejs/${repo}/actions/workflows/publish.yml`
270
+ )
271
+ );
272
+ }
273
+ console.log();
274
+ };
275
+ // Annotate the CommonJS export names for ESM import in node:
276
+ 0 && (module.exports = {
277
+ publish,
278
+ release
279
+ });
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@vitejs/release-scripts",
3
+ "version": "1.0.4",
4
+ "description": "@vitejs release scripts",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsx scripts/build.ts",
12
+ "prettier": "pnpm prettier-ci --write",
13
+ "prettier-ci": "prettier --cache --ignore-path=.gitignore --check '**/*.{ts,json,md,yml}'",
14
+ "ci": "tsc && pnpm prettier-ci && pnpm build",
15
+ "release": "tsx scripts/release.ts"
16
+ },
17
+ "prettier": {
18
+ "trailingComma": "all"
19
+ },
20
+ "dependencies": {
21
+ "execa": "^6.1.0",
22
+ "minimist": "^1.2.7",
23
+ "picocolors": "^1.0.0",
24
+ "prompts": "^2.4.2",
25
+ "semver": "^7.3.8"
26
+ },
27
+ "devDependencies": {
28
+ "@types/minimist": "^1.2.2",
29
+ "@types/node": "^18.11.18",
30
+ "@types/prompts": "^2.4.2",
31
+ "@types/semver": "^7.3.13",
32
+ "esbuild": "^0.16.17",
33
+ "prettier": "^2.8.2",
34
+ "tsx": "^3.12.2",
35
+ "typescript": "^4.9.4"
36
+ }
37
+ }