Skip to content

Commit 5078b4b

Browse files
authored
Migrate to ESM (#484)
* Migrate to ESM * just use `require.resolve` * reuse setup action * checkout PR head for a sec * ls things * try a thing * the dirname had a trailing whitespace, lol
1 parent 7ed1955 commit 5078b4b

21 files changed

+385
-1232
lines changed

.github/actions/ci-setup/action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Setup CI
2+
3+
runs:
4+
using: composite
5+
steps:
6+
- name: Setup Node.js
7+
uses: actions/setup-node@v4
8+
with:
9+
node-version-file: ".node-version"
10+
cache: yarn
11+
12+
- name: Install dependencies
13+
shell: bash
14+
run: yarn install --frozen-lockfile

.github/workflows/ci.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ jobs:
1313
runs-on: ubuntu-latest
1414

1515
steps:
16-
- name: Checkout Repo
17-
uses: actions/checkout@v4
18-
19-
- name: Use Node.js 20
20-
uses: actions/setup-node@v4
16+
- uses: actions/checkout@v4
2117
with:
22-
node-version: 20.x
18+
ref: ${{ github.event.pull_request.head.sha }}
2319

24-
- name: Install Dependencies
25-
run: yarn --frozen-lockfile
20+
- uses: ./.github/actions/ci-setup
2621

2722
- name: Typecheck
2823
run: yarn typecheck

.github/workflows/release-pr.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
needs: release_check
3838
steps:
3939
- uses: actions/checkout@v4
40+
- uses: ./.github/actions/ci-setup
4041

4142
- name: Checkout pull request
4243
run: gh pr checkout ${{ github.event.issue.number }}
@@ -54,14 +55,6 @@ jobs:
5455
if: steps.check_version_packages.outputs.version_packages == 'true'
5556
run: git reset --hard HEAD~1
5657

57-
- name: Use Node.js 20
58-
uses: actions/setup-node@v4
59-
with:
60-
node-version: 20.x
61-
62-
- name: Install Dependencies
63-
run: yarn --frozen-lockfile
64-
6558
- run: yarn changeset version --snapshot pr${{ github.event.issue.number }}
6659
env:
6760
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/version-or-publish.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@ jobs:
1111
changesets:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- name: Checkout Repo
15-
uses: actions/checkout@v4
16-
17-
- name: Use Node.js 20
18-
uses: actions/setup-node@v4
19-
with:
20-
node-version: 20.x
21-
22-
- name: Install Dependencies
23-
run: yarn --frozen-lockfile
14+
- uses: actions/checkout@v4
15+
- uses: ./.github/actions/ci-setup
2416

2517
- name: Build
2618
run: yarn build

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v22.9.0

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
3+
"version": "0.2.0",
4+
"configurations": [
5+
{
6+
"type": "node",
7+
"request": "launch",
8+
"name": "Debug Current Test File",
9+
"autoAttachChildProcesses": true,
10+
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
11+
"args": ["run", "${relativeFile}"],
12+
"smartStep": true,
13+
"console": "integratedTerminal"
14+
}
15+
]
16+
}

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "@changesets/action",
33
"version": "1.5.2",
44
"main": "dist/index.js",
5+
"type": "module",
56
"license": "MIT",
67
"devDependencies": {
7-
"@changesets/changelog-github": "^0.4.2",
8-
"@changesets/cli": "^2.20.0",
9-
"@changesets/write": "^0.1.6",
8+
"@changesets/changelog-github": "^0.5.1",
9+
"@changesets/cli": "^2.29.3",
10+
"@changesets/write": "^0.4.0",
1011
"@types/node": "^22.15.17",
1112
"@types/semver": "^7.5.0",
1213
"esbuild": "^0.25.4",
@@ -17,14 +18,14 @@
1718
"vitest": "^3.1.3"
1819
},
1920
"scripts": {
20-
"build": "esbuild src/index.ts --bundle --platform=node --target=node20 --minify --outfile=dist/index.js",
21+
"build": "esbuild src/index.ts --bundle --platform=node --target=node20 --format=esm --minify --outfile=dist/index.js",
2122
"test": "vitest",
2223
"test:watch": "yarn test --watch",
2324
"typecheck": "tsc",
2425
"changeset": "changeset",
25-
"bump": "node ./scripts/bump.js",
26-
"release": "node ./scripts/release.js",
27-
"release:pr": "node ./scripts/release-pr.js"
26+
"bump": "node --experimental-strip-types ./scripts/bump.ts",
27+
"release": "node --experimental-strip-types ./scripts/release.ts",
28+
"release:pr": "node --experimental-strip-types ./scripts/release-pr.ts"
2829
},
2930
"engines": {
3031
"node": ">= 20"
@@ -34,16 +35,15 @@
3435
"@actions/exec": "^1.1.1",
3536
"@actions/github": "^6.0.1",
3637
"@changesets/ghcommit": "^2.0.0",
37-
"@changesets/pre": "^1.0.9",
38-
"@changesets/read": "^0.5.3",
38+
"@changesets/pre": "^2.0.2",
39+
"@changesets/read": "^0.6.5",
3940
"@manypkg/get-packages": "^1.1.3",
4041
"@octokit/core": "^5.2.1",
4142
"@octokit/plugin-throttling": "^8.0.0",
4243
"@types/mdast": "^3.0.0",
4344
"mdast-util-to-string": "^1.0.6",
4445
"remark-parse": "^7.0.1",
4546
"remark-stringify": "^7.0.3",
46-
"resolve-from": "^5.0.0",
4747
"semver": "^7.5.3",
4848
"unified": "^8.3.2"
4949
},

scripts/bump.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/bump.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { exec } from "@actions/exec";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import pkgJson from "../package.json" with { type: "json" };
5+
6+
process.chdir(path.join(import.meta.dirname, ".."));
7+
8+
await exec("changeset", ["version"]);
9+
10+
const releaseLine = `v${pkgJson.version.split(".")[0]}`;
11+
12+
const readmePath = path.join(import.meta.dirname, "..", "README.md");
13+
const content = fs.readFileSync(readmePath, "utf8");
14+
const updatedContent = content.replace(
15+
/changesets\/action@[^\s]+/g,
16+
`changesets/action@${releaseLine}`
17+
);
18+
fs.writeFileSync(readmePath, updatedContent);

scripts/release-pr.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

scripts/release-pr.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { exec } from "@actions/exec";
2+
import path from "node:path";
3+
4+
import pkgJson from "../package.json" with { type: "json" };
5+
6+
const tag = `v${pkgJson.version}`;
7+
const releaseLine = "pr-release";
8+
9+
process.chdir(path.join(__dirname, ".."));
10+
11+
await exec("git", ["checkout", "--detach"]);
12+
await exec("git", ["add", "--force", "dist"]);
13+
await exec("git", ["commit", "-m", tag]);
14+
15+
await exec("git", [
16+
"push",
17+
"--force",
18+
"origin",
19+
`HEAD:refs/heads/${releaseLine}`,
20+
]);

scripts/release.js renamed to scripts/release.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const path = require("path");
2-
const { exec, getExecOutput } = require("@actions/exec");
1+
import { exec, getExecOutput } from "@actions/exec";
2+
import path from "node:path";
33

4-
const { version } = require("../package.json");
5-
const tag = `v${version}`;
6-
const releaseLine = `v${version.split(".")[0]}`;
4+
import pkgJson from "../package.json" with { type: "json" };
75

8-
process.chdir(path.join(__dirname, ".."));
6+
const tag = `v${pkgJson.version}`;
7+
const releaseLine = `v${pkgJson.version.split(".")[0]}`;
8+
9+
process.chdir(path.join(import.meta.dirname, ".."));
910

1011
(async () => {
1112
const { exitCode, stderr } = await getExecOutput(

src/git.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from "@actions/core";
22
import { exec, getExecOutput } from "@actions/exec";
33
import * as github from "@actions/github";
44
import { commitChangesFromRepo } from "@changesets/ghcommit/git";
5-
import { Octokit } from "./octokit";
5+
import type { Octokit } from "./octokit.ts";
66

77
type GitOptions = {
88
cwd: string;

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as core from "@actions/core";
22
import fs from "node:fs/promises";
33
import path from "node:path";
4-
import { Git } from "./git";
5-
import { setupOctokit } from "./octokit";
6-
import readChangesetState from "./readChangesetState";
7-
import { runPublish, runVersion } from "./run";
8-
import { fileExists } from "./utils";
4+
import { Git } from "./git.ts";
5+
import { setupOctokit } from "./octokit.ts";
6+
import readChangesetState from "./readChangesetState.ts";
7+
import { runPublish, runVersion } from "./run.ts";
8+
import { fileExists } from "./utils.ts";
99

1010
const getOptionalInput = (name: string) => core.getInput(name) || undefined;
1111

src/readChangesetState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PreState, NewChangeset } from "@changesets/types";
1+
import type { PreState, NewChangeset } from "@changesets/types";
22
import { readPreState } from "@changesets/pre";
33
import readChangesets from "@changesets/read";
44

src/run.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Changeset } from "@changesets/types";
1+
import type { Changeset } from "@changesets/types";
22
import writeChangeset from "@changesets/write";
33
import fixturez from "fixturez";
44
import fs from "node:fs/promises";
55
import path from "node:path";
66
import { beforeEach, describe, expect, it, vi } from "vitest";
7-
import { Git } from "./git";
8-
import { setupOctokit } from "./octokit";
9-
import { runVersion } from "./run";
7+
import { Git } from "./git.ts";
8+
import { setupOctokit } from "./octokit.ts";
9+
import { runVersion } from "./run.ts";
1010

1111
vi.mock("@actions/github", () => ({
1212
context: {
@@ -21,7 +21,7 @@ vi.mock("@actions/github", () => ({
2121
rest: mockedGithubMethods,
2222
}),
2323
}));
24-
vi.mock("./git");
24+
vi.mock("./git.ts");
2525
vi.mock("@changesets/ghcommit/git");
2626

2727
let mockedGithubMethods = {
@@ -34,11 +34,11 @@ let mockedGithubMethods = {
3434
},
3535
};
3636

37-
let f = fixturez(__dirname);
37+
let f = fixturez(import.meta.dirname);
3838

3939
const linkNodeModules = async (cwd: string) => {
4040
await fs.symlink(
41-
path.join(__dirname, "..", "node_modules"),
41+
path.join(import.meta.dirname, "..", "node_modules"),
4242
path.join(cwd, "node_modules")
4343
);
4444
};

src/run.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import * as core from "@actions/core";
22
import { exec, getExecOutput } from "@actions/exec";
33
import * as github from "@actions/github";
4-
import { PreState } from "@changesets/types";
5-
import { Package, getPackages } from "@manypkg/get-packages";
4+
import type { PreState } from "@changesets/types";
5+
import { type Package, getPackages } from "@manypkg/get-packages";
66
import fs from "node:fs/promises";
7+
import { createRequire } from "node:module";
78
import path from "node:path";
8-
import resolveFrom from "resolve-from";
9-
import semverLt from "semver/functions/lt";
10-
import { Git } from "./git";
11-
import { Octokit } from "./octokit";
12-
import readChangesetState from "./readChangesetState";
9+
import semverLt from "semver/functions/lt.js";
10+
import { Git } from "./git.ts";
11+
import type { Octokit } from "./octokit.ts";
12+
import readChangesetState from "./readChangesetState.ts";
1313
import {
1414
getChangedPackages,
1515
getChangelogEntry,
1616
getVersionsByDirectory,
1717
isErrorWithCode,
1818
sortTheThings,
19-
} from "./utils";
19+
} from "./utils.ts";
20+
21+
const require = createRequire(import.meta.url);
2022

2123
// GitHub Issues/PRs messages have a max size limit on the
2224
// message body payload.
@@ -162,7 +164,9 @@ export async function runPublish({
162164

163165
const requireChangesetsCliPkgJson = (cwd: string) => {
164166
try {
165-
return require(resolveFrom(cwd, "@changesets/cli/package.json"));
167+
return require(require.resolve("@changesets/cli/package.json", {
168+
paths: [cwd],
169+
}));
166170
} catch (err) {
167171
if (isErrorWithCode(err, "MODULE_NOT_FOUND")) {
168172
throw new Error(
@@ -286,9 +290,18 @@ export async function runVersion({
286290
let cmd = semverLt(changesetsCliPkgJson.version, "2.0.0")
287291
? "bump"
288292
: "version";
289-
await exec("node", [resolveFrom(cwd, "@changesets/cli/bin.js"), cmd], {
290-
cwd,
291-
});
293+
await exec(
294+
"node",
295+
[
296+
require.resolve("@changesets/cli/bin.js", {
297+
paths: [cwd],
298+
}),
299+
cmd,
300+
],
301+
{
302+
cwd,
303+
}
304+
);
292305
}
293306

294307
let changedPackages = await getChangedPackages(cwd, versionsByDirectory);

src/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "vitest";
2-
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils";
2+
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils.ts";
33

44
let changelog = `# @keystone-alpha/email
55

0 commit comments

Comments
 (0)