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
This commit is contained in:
Mateusz Burzyński
2025-05-16 11:39:43 +02:00
committed by GitHub
parent 7ed1955546
commit 5078b4b48b
21 changed files with 384 additions and 1231 deletions
+14
View File
@@ -0,0 +1,14 @@
name: Setup CI
runs:
using: composite
steps:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".node-version"
cache: yarn
- name: Install dependencies
shell: bash
run: yarn install --frozen-lockfile
+3 -8
View File
@@ -13,16 +13,11 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Repo - uses: actions/checkout@v4
uses: actions/checkout@v4
- name: Use Node.js 20
uses: actions/setup-node@v4
with: with:
node-version: 20.x ref: ${{ github.event.pull_request.head.sha }}
- name: Install Dependencies - uses: ./.github/actions/ci-setup
run: yarn --frozen-lockfile
- name: Typecheck - name: Typecheck
run: yarn typecheck run: yarn typecheck
+1 -8
View File
@@ -37,6 +37,7 @@ jobs:
needs: release_check needs: release_check
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: ./.github/actions/ci-setup
- name: Checkout pull request - name: Checkout pull request
run: gh pr checkout ${{ github.event.issue.number }} run: gh pr checkout ${{ github.event.issue.number }}
@@ -54,14 +55,6 @@ jobs:
if: steps.check_version_packages.outputs.version_packages == 'true' if: steps.check_version_packages.outputs.version_packages == 'true'
run: git reset --hard HEAD~1 run: git reset --hard HEAD~1
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Install Dependencies
run: yarn --frozen-lockfile
- run: yarn changeset version --snapshot pr${{ github.event.issue.number }} - run: yarn changeset version --snapshot pr${{ github.event.issue.number }}
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+2 -10
View File
@@ -11,16 +11,8 @@ jobs:
changesets: changesets:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Repo - uses: actions/checkout@v4
uses: actions/checkout@v4 - uses: ./.github/actions/ci-setup
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Install Dependencies
run: yarn --frozen-lockfile
- name: Build - name: Build
run: yarn build run: yarn build
+1
View File
@@ -0,0 +1 @@
v22.9.0
+16
View File
@@ -0,0 +1,16 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Current Test File",
"autoAttachChildProcesses": true,
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}"],
"smartStep": true,
"console": "integratedTerminal"
}
]
}
+10 -10
View File
@@ -2,11 +2,12 @@
"name": "@changesets/action", "name": "@changesets/action",
"version": "1.5.2", "version": "1.5.2",
"main": "dist/index.js", "main": "dist/index.js",
"type": "module",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@changesets/changelog-github": "^0.4.2", "@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.20.0", "@changesets/cli": "^2.29.3",
"@changesets/write": "^0.1.6", "@changesets/write": "^0.4.0",
"@types/node": "^22.15.17", "@types/node": "^22.15.17",
"@types/semver": "^7.5.0", "@types/semver": "^7.5.0",
"esbuild": "^0.25.4", "esbuild": "^0.25.4",
@@ -17,14 +18,14 @@
"vitest": "^3.1.3" "vitest": "^3.1.3"
}, },
"scripts": { "scripts": {
"build": "esbuild src/index.ts --bundle --platform=node --target=node20 --minify --outfile=dist/index.js", "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --format=esm --minify --outfile=dist/index.js",
"test": "vitest", "test": "vitest",
"test:watch": "yarn test --watch", "test:watch": "yarn test --watch",
"typecheck": "tsc", "typecheck": "tsc",
"changeset": "changeset", "changeset": "changeset",
"bump": "node ./scripts/bump.js", "bump": "node --experimental-strip-types ./scripts/bump.ts",
"release": "node ./scripts/release.js", "release": "node --experimental-strip-types ./scripts/release.ts",
"release:pr": "node ./scripts/release-pr.js" "release:pr": "node --experimental-strip-types ./scripts/release-pr.ts"
}, },
"engines": { "engines": {
"node": ">= 20" "node": ">= 20"
@@ -34,8 +35,8 @@
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"@actions/github": "^6.0.1", "@actions/github": "^6.0.1",
"@changesets/ghcommit": "^2.0.0", "@changesets/ghcommit": "^2.0.0",
"@changesets/pre": "^1.0.9", "@changesets/pre": "^2.0.2",
"@changesets/read": "^0.5.3", "@changesets/read": "^0.6.5",
"@manypkg/get-packages": "^1.1.3", "@manypkg/get-packages": "^1.1.3",
"@octokit/core": "^5.2.1", "@octokit/core": "^5.2.1",
"@octokit/plugin-throttling": "^8.0.0", "@octokit/plugin-throttling": "^8.0.0",
@@ -43,7 +44,6 @@
"mdast-util-to-string": "^1.0.6", "mdast-util-to-string": "^1.0.6",
"remark-parse": "^7.0.1", "remark-parse": "^7.0.1",
"remark-stringify": "^7.0.3", "remark-stringify": "^7.0.3",
"resolve-from": "^5.0.0",
"semver": "^7.5.3", "semver": "^7.5.3",
"unified": "^8.3.2" "unified": "^8.3.2"
}, },
-19
View File
@@ -1,19 +0,0 @@
const fs = require("fs");
const path = require("path");
const { exec } = require("@actions/exec");
process.chdir(path.join(__dirname, ".."));
(async () => {
await exec("changeset", ["version"]);
const releaseLine = `v${require("../package.json").version.split(".")[0]}`;
const readmePath = path.join(__dirname, "..", "README.md");
const content = fs.readFileSync(readmePath, "utf8");
const updatedContent = content.replace(
/changesets\/action@[^\s]+/g,
`changesets/action@${releaseLine}`
);
fs.writeFileSync(readmePath, updatedContent);
})();
+18
View File
@@ -0,0 +1,18 @@
import { exec } from "@actions/exec";
import fs from "node:fs";
import path from "node:path";
import pkgJson from "../package.json" with { type: "json" };
process.chdir(path.join(import.meta.dirname, ".."));
await exec("changeset", ["version"]);
const releaseLine = `v${pkgJson.version.split(".")[0]}`;
const readmePath = path.join(import.meta.dirname, "..", "README.md");
const content = fs.readFileSync(readmePath, "utf8");
const updatedContent = content.replace(
/changesets\/action@[^\s]+/g,
`changesets/action@${releaseLine}`
);
fs.writeFileSync(readmePath, updatedContent);
-21
View File
@@ -1,21 +0,0 @@
const path = require("path");
const { exec, getExecOutput } = require("@actions/exec");
const { version } = require("../package.json");
const tag = `v${version}`;
const releaseLine = "pr-release";
process.chdir(path.join(__dirname, ".."));
(async () => {
await exec("git", ["checkout", "--detach"]);
await exec("git", ["add", "--force", "dist"]);
await exec("git", ["commit", "-m", tag]);
await exec("git", [
"push",
"--force",
"origin",
`HEAD:refs/heads/${releaseLine}`,
]);
})();
+20
View File
@@ -0,0 +1,20 @@
import { exec } from "@actions/exec";
import path from "node:path";
import pkgJson from "../package.json" with { type: "json" };
const tag = `v${pkgJson.version}`;
const releaseLine = "pr-release";
process.chdir(path.join(__dirname, ".."));
await exec("git", ["checkout", "--detach"]);
await exec("git", ["add", "--force", "dist"]);
await exec("git", ["commit", "-m", tag]);
await exec("git", [
"push",
"--force",
"origin",
`HEAD:refs/heads/${releaseLine}`,
]);
+7 -6
View File
@@ -1,11 +1,12 @@
const path = require("path"); import { exec, getExecOutput } from "@actions/exec";
const { exec, getExecOutput } = require("@actions/exec"); import path from "node:path";
const { version } = require("../package.json"); import pkgJson from "../package.json" with { type: "json" };
const tag = `v${version}`;
const releaseLine = `v${version.split(".")[0]}`;
process.chdir(path.join(__dirname, "..")); const tag = `v${pkgJson.version}`;
const releaseLine = `v${pkgJson.version.split(".")[0]}`;
process.chdir(path.join(import.meta.dirname, ".."));
(async () => { (async () => {
const { exitCode, stderr } = await getExecOutput( const { exitCode, stderr } = await getExecOutput(
+1 -1
View File
@@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { exec, getExecOutput } from "@actions/exec"; import { exec, getExecOutput } from "@actions/exec";
import * as github from "@actions/github"; import * as github from "@actions/github";
import { commitChangesFromRepo } from "@changesets/ghcommit/git"; import { commitChangesFromRepo } from "@changesets/ghcommit/git";
import { Octokit } from "./octokit"; import type { Octokit } from "./octokit.ts";
type GitOptions = { type GitOptions = {
cwd: string; cwd: string;
+5 -5
View File
@@ -1,11 +1,11 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { Git } from "./git"; import { Git } from "./git.ts";
import { setupOctokit } from "./octokit"; import { setupOctokit } from "./octokit.ts";
import readChangesetState from "./readChangesetState"; import readChangesetState from "./readChangesetState.ts";
import { runPublish, runVersion } from "./run"; import { runPublish, runVersion } from "./run.ts";
import { fileExists } from "./utils"; import { fileExists } from "./utils.ts";
const getOptionalInput = (name: string) => core.getInput(name) || undefined; const getOptionalInput = (name: string) => core.getInput(name) || undefined;
+1 -1
View File
@@ -1,4 +1,4 @@
import { PreState, NewChangeset } from "@changesets/types"; import type { PreState, NewChangeset } from "@changesets/types";
import { readPreState } from "@changesets/pre"; import { readPreState } from "@changesets/pre";
import readChangesets from "@changesets/read"; import readChangesets from "@changesets/read";
+7 -7
View File
@@ -1,12 +1,12 @@
import { Changeset } from "@changesets/types"; import type { Changeset } from "@changesets/types";
import writeChangeset from "@changesets/write"; import writeChangeset from "@changesets/write";
import fixturez from "fixturez"; import fixturez from "fixturez";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
import { Git } from "./git"; import { Git } from "./git.ts";
import { setupOctokit } from "./octokit"; import { setupOctokit } from "./octokit.ts";
import { runVersion } from "./run"; import { runVersion } from "./run.ts";
vi.mock("@actions/github", () => ({ vi.mock("@actions/github", () => ({
context: { context: {
@@ -21,7 +21,7 @@ vi.mock("@actions/github", () => ({
rest: mockedGithubMethods, rest: mockedGithubMethods,
}), }),
})); }));
vi.mock("./git"); vi.mock("./git.ts");
vi.mock("@changesets/ghcommit/git"); vi.mock("@changesets/ghcommit/git");
let mockedGithubMethods = { let mockedGithubMethods = {
@@ -34,11 +34,11 @@ let mockedGithubMethods = {
}, },
}; };
let f = fixturez(__dirname); let f = fixturez(import.meta.dirname);
const linkNodeModules = async (cwd: string) => { const linkNodeModules = async (cwd: string) => {
await fs.symlink( await fs.symlink(
path.join(__dirname, "..", "node_modules"), path.join(import.meta.dirname, "..", "node_modules"),
path.join(cwd, "node_modules") path.join(cwd, "node_modules")
); );
}; };
+25 -12
View File
@@ -1,22 +1,24 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { exec, getExecOutput } from "@actions/exec"; import { exec, getExecOutput } from "@actions/exec";
import * as github from "@actions/github"; import * as github from "@actions/github";
import { PreState } from "@changesets/types"; import type { PreState } from "@changesets/types";
import { Package, getPackages } from "@manypkg/get-packages"; import { type Package, getPackages } from "@manypkg/get-packages";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import { createRequire } from "node:module";
import path from "node:path"; import path from "node:path";
import resolveFrom from "resolve-from"; import semverLt from "semver/functions/lt.js";
import semverLt from "semver/functions/lt"; import { Git } from "./git.ts";
import { Git } from "./git"; import type { Octokit } from "./octokit.ts";
import { Octokit } from "./octokit"; import readChangesetState from "./readChangesetState.ts";
import readChangesetState from "./readChangesetState";
import { import {
getChangedPackages, getChangedPackages,
getChangelogEntry, getChangelogEntry,
getVersionsByDirectory, getVersionsByDirectory,
isErrorWithCode, isErrorWithCode,
sortTheThings, sortTheThings,
} from "./utils"; } from "./utils.ts";
const require = createRequire(import.meta.url);
// GitHub Issues/PRs messages have a max size limit on the // GitHub Issues/PRs messages have a max size limit on the
// message body payload. // message body payload.
@@ -162,7 +164,9 @@ export async function runPublish({
const requireChangesetsCliPkgJson = (cwd: string) => { const requireChangesetsCliPkgJson = (cwd: string) => {
try { try {
return require(resolveFrom(cwd, "@changesets/cli/package.json")); return require(require.resolve("@changesets/cli/package.json", {
paths: [cwd],
}));
} catch (err) { } catch (err) {
if (isErrorWithCode(err, "MODULE_NOT_FOUND")) { if (isErrorWithCode(err, "MODULE_NOT_FOUND")) {
throw new Error( throw new Error(
@@ -286,9 +290,18 @@ export async function runVersion({
let cmd = semverLt(changesetsCliPkgJson.version, "2.0.0") let cmd = semverLt(changesetsCliPkgJson.version, "2.0.0")
? "bump" ? "bump"
: "version"; : "version";
await exec("node", [resolveFrom(cwd, "@changesets/cli/bin.js"), cmd], { await exec(
cwd, "node",
}); [
require.resolve("@changesets/cli/bin.js", {
paths: [cwd],
}),
cmd,
],
{
cwd,
}
);
} }
let changedPackages = await getChangedPackages(cwd, versionsByDirectory); let changedPackages = await getChangedPackages(cwd, versionsByDirectory);
+1 -1
View File
@@ -1,5 +1,5 @@
import { expect, test } from "vitest"; import { expect, test } from "vitest";
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils"; import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils.ts";
let changelog = `# @keystone-alpha/email let changelog = `# @keystone-alpha/email
+1 -1
View File
@@ -5,7 +5,7 @@ import fs from "node:fs/promises";
import type { Root } from "mdast"; import type { Root } from "mdast";
// @ts-ignore // @ts-ignore
import mdastToString from "mdast-util-to-string"; import mdastToString from "mdast-util-to-string";
import { getPackages, Package } from "@manypkg/get-packages"; import { getPackages, type Package } from "@manypkg/get-packages";
export const BumpLevels = { export const BumpLevels = {
dep: 0, dep: 0,
+9 -7
View File
@@ -1,12 +1,14 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "esnext", "allowImportingTsExtensions": true,
"module": "node18",
"noEmit": true,
"isolatedModules": true,
"strict": true,
"moduleResolution": "node16",
"erasableSyntaxOnly": true, "erasableSyntaxOnly": true,
"skipLibCheck": true "isolatedModules": true,
"module": "node18",
"moduleResolution": "node16",
"noEmit": true,
"resolveJsonModule": true,
"strict": true,
"target": "esnext",
"verbatimModuleSyntax": true
} }
} }
+242 -1114
View File
File diff suppressed because it is too large Load Diff