Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/actions/ci-setup /action.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 2 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: Install Dependencies
run: yarn --frozen-lockfile
- uses: actions/checkout@v4
- uses: ./.github/actions/ci-setup

- name: Typecheck
run: yarn typecheck
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/version-or-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ jobs:
changesets:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: Install Dependencies
run: yarn --frozen-lockfile
- uses: actions/checkout@v4
- uses: ./.github/actions/ci-setup

- name: Build
run: yarn build
Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.9.0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fuzzy on this. This repo only has to support one node version. I had issues using --experimental-strip-types on node 20 so I decided to use node 22 here - for dev workflows and on CI. But then only node 20 is currently supported for GitHub Actions (using: 'node22' doesn't work yet).

16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@changesets/action",
"version": "1.5.2",
"main": "dist/index.js",
"type": "module",
"license": "MIT",
"devDependencies": {
"@changesets/changelog-github": "^0.4.2",
"@changesets/cli": "^2.20.0",
"@changesets/write": "^0.1.6",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.29.3",
"@changesets/write": "^0.4.0",
"@types/fs-extra": "^8.0.0",
"@types/node": "^22.15.17",
"@types/semver": "^7.5.0",
Expand All @@ -18,13 +19,13 @@
"vitest": "^3.1.3"
},
"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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory... this should be quite safe but then... who knows. Perhaps this should only land after testing this e2e in some repo. It feels like a somewhat risky change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used to migrate to esbuild & esm in my actions but reverted back due to some issues I've forgotten, so probably good to look out here too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn. That's not great :p I think I'll still try to run with it - but I have to definitely test it e2e first

"test": "vitest",
"test:watch": "yarn test --watch",
"typecheck": "tsc",
"changeset": "changeset",
"bump": "node ./scripts/bump.js",
"release": "node ./scripts/release.js"
"bump": "node --experimental-strip-types ./scripts/bump.ts",
"release": "node --experimental-strip-types ./scripts/release.ts"
},
"engines": {
"node": ">= 20"
Expand All @@ -34,17 +35,17 @@
"@actions/exec": "^1.1.1",
"@actions/github": "^6.0.1",
"@changesets/ghcommit": "1.4.0",
"@changesets/pre": "^1.0.9",
"@changesets/read": "^0.5.3",
"@changesets/pre": "^2.0.2",
"@changesets/read": "^0.6.5",
"@manypkg/get-packages": "^1.1.3",
"@octokit/core": "^5.2.1",
"@octokit/plugin-throttling": "^8.0.0",
"@types/mdast": "^3.0.0",
"fs-extra": "^8.1.0",
"import-meta-resolve": "^4.1.0",
"mdast-util-to-string": "^1.0.6",
"remark-parse": "^7.0.1",
"remark-stringify": "^7.0.3",
"resolve-from": "^5.0.0",
"semver": "^7.5.3",
"unified": "^8.3.2"
},
Expand Down
19 changes: 0 additions & 19 deletions scripts/bump.js

This file was deleted.

18 changes: 18 additions & 0 deletions scripts/bump.ts
Original file line number Diff line number Diff line change
@@ -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, ".."));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use import.meta.dirname in tests and script so far so it's fine anyway. But I wonder if there are any compatibility gotchas to consider if we'd use this in the code of the action itself.

I'm not sure what is the caveat the docs mention here:

Caveat: only present on file: modules.

Is that only that it wouldn't be present for modules imported/downloaded using HTTP(s) etc?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that's what it means. On GitHub workflows the code should be ran as files though and shouldn't have this issue, though you need to account for it resolving from dist instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though you need to account for it resolving from dist instead.

Good point. It's not an issue right now as we don't use it in src (and all files are nested directly in src too). But I'll keep that in mind as something to be more careful about.


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);
13 changes: 7 additions & 6 deletions scripts/release.js → scripts/release.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const path = require("path");
const { exec, getExecOutput } = require("@actions/exec");
import { exec, getExecOutput } from "@actions/exec";
import path from "node:path";

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

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 () => {
const { exitCode, stderr } = await getExecOutput(
Expand Down
2 changes: 1 addition & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { exec, getExecOutput } from "@actions/exec";
import * as github from "@actions/github";
import { commitChangesFromRepo } from "@changesets/ghcommit/git";
import { Octokit } from "./octokit";
import type { Octokit } from "./octokit.ts";

type GitOptions = {
cwd: string;
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as core from "@actions/core";
import fs from "fs-extra";
import { Git } from "./git";
import { setupOctokit } from "./octokit";
import readChangesetState from "./readChangesetState";
import { runPublish, runVersion } from "./run";
import { Git } from "./git.ts";
import { setupOctokit } from "./octokit.ts";
import readChangesetState from "./readChangesetState.ts";
import { runPublish, runVersion } from "./run.ts";

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

Expand All @@ -30,7 +30,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
}
const git = new Git({
octokit: commitMode === "github-api" ? octokit : undefined,
cwd
cwd,
});

let setupGitUser = core.getBooleanInput("setupGitUser");
Expand Down
2 changes: 1 addition & 1 deletion src/readChangesetState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PreState, NewChangeset } from "@changesets/types";
import type { PreState, NewChangeset } from "@changesets/types";
import { readPreState } from "@changesets/pre";
import readChangesets from "@changesets/read";

Expand Down
14 changes: 7 additions & 7 deletions src/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Changeset } from "@changesets/types";
import type { Changeset } from "@changesets/types";
import writeChangeset from "@changesets/write";
import fixturez from "fixturez";
import fs from "fs-extra";
import path from "path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { Git } from "./git";
import { setupOctokit } from "./octokit";
import { runVersion } from "./run";
import { Git } from "./git.ts";
import { setupOctokit } from "./octokit.ts";
import { runVersion } from "./run.ts";

vi.mock("@actions/github", () => ({
context: {
Expand All @@ -21,7 +21,7 @@ vi.mock("@actions/github", () => ({
rest: mockedGithubMethods,
}),
}));
vi.mock("./git");
vi.mock("./git.ts");
vi.mock("@changesets/ghcommit/git");

let mockedGithubMethods = {
Expand All @@ -34,11 +34,11 @@ let mockedGithubMethods = {
},
};

let f = fixturez(__dirname);
let f = fixturez(import.meta.dirname);

const linkNodeModules = async (cwd: string) => {
await fs.symlink(
path.join(__dirname, "..", "node_modules"),
path.join(import.meta.dirname, "..", "node_modules"),
path.join(cwd, "node_modules")
);
};
Expand Down
42 changes: 30 additions & 12 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import * as core from "@actions/core";
import { exec, getExecOutput } from "@actions/exec";
import * as github from "@actions/github";
import { PreState } from "@changesets/types";
import { Package, getPackages } from "@manypkg/get-packages";
import type { PreState } from "@changesets/types";
import { type Package, getPackages } from "@manypkg/get-packages";
import fs from "fs-extra";
import { resolve } from "import-meta-resolve";
import { createRequire } from "node:module";
import { pathToFileURL, fileURLToPath } from "node:url";
import path from "path";
import resolveFrom from "resolve-from";
import semverLt from "semver/functions/lt";
import { Git } from "./git";
import { Octokit } from "./octokit";
import readChangesetState from "./readChangesetState";
import semverLt from "semver/functions/lt.js";
import { Git } from "./git.ts";
import type { Octokit } from "./octokit.ts";
import readChangesetState from "./readChangesetState.ts";
import {
getChangedPackages,
getChangelogEntry,
getVersionsByDirectory,
isErrorWithCode,
sortTheThings,
} from "./utils";
} from "./utils.ts";

const require = createRequire(import.meta.url);

// GitHub Issues/PRs messages have a max size limit on the
// message body payload.
Expand Down Expand Up @@ -162,7 +166,9 @@ export async function runPublish({

const requireChangesetsCliPkgJson = (cwd: string) => {
try {
return require(resolveFrom(cwd, "@changesets/cli/package.json"));
return require(require.resolve("@changesets/cli/package.json", {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we were using resolveFrom. require.resolve supported this for a long time already. It also just seems like using require here is an easier option than dancing around this using import-meta-resolve.

paths: [cwd],
}));
} catch (err) {
if (isErrorWithCode(err, "MODULE_NOT_FOUND")) {
throw new Error(
Expand Down Expand Up @@ -286,9 +292,21 @@ export async function runVersion({
let cmd = semverLt(changesetsCliPkgJson.version, "2.0.0")
? "bump"
: "version";
await exec("node", [resolveFrom(cwd, "@changesets/cli/bin.js"), cmd], {
cwd,
});
await exec(
"node",
[
fileURLToPath(
resolve(
"@changesets/cli/bin.js",
pathToFileURL(path.join(cwd, "x.cjs")).toString()
)
),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is... quite a dance to make it work. Not the biggest issue but I wish there would be a builtin way to make this easier.

If I'm not mistaken, it also means that a bug was introduced here:
https://github.com/changesets/changesets/blob/268a29fedc948f22c672a3b1e3e51df4427f478d/packages/cli/src/commit/getCommitFunctions.ts#L16

A directory can't be used directly as input to resolve. It should be a filepath. cc @bluwy for double-checking my logic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think you're right. That should probably be a file path otherwise it resolves from one directory higher, which probably coincidentally worked because there's no node_modules in .changeset/ for the linked code.

About this code, could we also use require.resolve instead of import-meta-resolve too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this code, could we also use require.resolve instead of import-meta-resolve too?

Yep, that was my plan - I just wanted to have a pushed out reference of how to do this using import-meta-resolve ;p I simplified this now.

cmd,
],
{
cwd,
}
);
}

let changedPackages = await getChangedPackages(cwd, versionsByDirectory);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils";
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils.ts";

let changelog = `# @keystone-alpha/email

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import remarkStringify from "remark-stringify";
import type { Root } from "mdast";
// @ts-ignore
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 = {
dep: 0,
Expand Down
12 changes: 7 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"isolatedModules": true,
"module": "node18",
"moduleResolution": "node16",
"noEmit": true,
"isolatedModules": true,
"resolveJsonModule": true,
"strict": true,
"moduleResolution": "node16",
"erasableSyntaxOnly": true,
"skipLibCheck": true
"target": "esnext",
"verbatimModuleSyntax": true
}
}
Loading
Loading