From 450fa76e4ab9142dfd25fc5fe7bf11e245fbc2ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 11 May 2025 11:20:14 +0200 Subject: [PATCH 1/2] Removed `fs-extra` dependency --- package.json | 2 -- src/index.ts | 8 ++++---- src/run.test.ts | 6 +++--- src/run.ts | 8 ++++---- yarn.lock | 7 ------- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index f15e1b7b..66546fc9 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "@changesets/changelog-github": "^0.4.2", "@changesets/cli": "^2.20.0", "@changesets/write": "^0.1.6", - "@types/fs-extra": "^8.0.0", "@types/jest": "^29.5.1", "@types/node": "^22.15.17", "@types/semver": "^7.5.0", @@ -45,7 +44,6 @@ "@octokit/core": "^5.2.1", "@octokit/plugin-throttling": "^8.0.0", "@types/mdast": "^3.0.0", - "fs-extra": "^8.1.0", "mdast-util-to-string": "^1.0.6", "remark-parse": "^7.0.1", "remark-stringify": "^7.0.3", diff --git a/src/index.ts b/src/index.ts index c5b0dc92..7bf242cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import * as core from "@actions/core"; -import fs from "fs-extra"; +import fs from "node:fs"; import { Git } from "./git"; import { setupOctokit } from "./octokit"; import readChangesetState from "./readChangesetState"; @@ -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"); @@ -41,7 +41,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; } core.info("setting GitHub credentials"); - await fs.writeFile( + fs.writeFileSync( `${process.env.HOME}/.netrc`, `machine github.com\nlogin github-actions[bot]\npassword ${githubToken}` ); @@ -73,7 +73,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; let userNpmrcPath = `${process.env.HOME}/.npmrc`; if (fs.existsSync(userNpmrcPath)) { core.info("Found existing user .npmrc file"); - const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8"); + const userNpmrcContent = fs.readFileSync(userNpmrcPath, "utf8"); const authLine = userNpmrcContent.split("\n").find((line) => { // check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line); diff --git a/src/run.test.ts b/src/run.test.ts index 0b3f0089..45f6b9f5 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -1,8 +1,8 @@ import { Changeset } from "@changesets/types"; import writeChangeset from "@changesets/write"; import fixturez from "fixturez"; -import fs from "fs-extra"; -import path from "path"; +import fs from "node:fs"; +import path from "node:path"; import { Git } from "./git"; import { setupOctokit } from "./octokit"; import { runVersion } from "./run"; @@ -36,7 +36,7 @@ let mockedGithubMethods = { let f = fixturez(__dirname); const linkNodeModules = async (cwd: string) => { - await fs.symlink( + fs.symlinkSync( path.join(__dirname, "..", "node_modules"), path.join(cwd, "node_modules") ); diff --git a/src/run.ts b/src/run.ts index 11e823ec..d96a3e60 100644 --- a/src/run.ts +++ b/src/run.ts @@ -3,8 +3,8 @@ 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 fs from "fs-extra"; -import path from "path"; +import fs from "node:fs"; +import path from "node:path"; import resolveFrom from "resolve-from"; import * as semver from "semver"; import { Git } from "./git"; @@ -30,7 +30,7 @@ const createRelease = async ( ) => { let changelog; try { - changelog = await fs.readFile(path.join(pkg.dir, "CHANGELOG.md"), "utf8"); + changelog = fs.readFileSync(path.join(pkg.dir, "CHANGELOG.md"), "utf8"); } catch (err) { if (isErrorWithCode(err, "ENOENT")) { // if we can't find a changelog, the user has probably disabled changelogs @@ -294,7 +294,7 @@ export async function runVersion({ let changedPackages = await getChangedPackages(cwd, versionsByDirectory); let changedPackagesInfoPromises = Promise.all( changedPackages.map(async (pkg) => { - let changelogContents = await fs.readFile( + let changelogContents = fs.readFileSync( path.join(pkg.dir, "CHANGELOG.md"), "utf8" ); diff --git a/yarn.lock b/yarn.lock index 07242858..724c9366 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1685,13 +1685,6 @@ dependencies: "@babel/types" "^7.3.0" -"@types/fs-extra@^8.0.0": - version "8.1.2" - resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.1.2.tgz#7125cc2e4bdd9bd2fc83005ffdb1d0ba00cca61f" - integrity sha512-SvSrYXfWSc7R4eqnOzbQF4TZmfpNSM9FrSWLU3EUnWBuyZqNBOrv1B1JA3byUDPUl9z4Ab3jeZG2eDdySlgNMg== - dependencies: - "@types/node" "*" - "@types/graceful-fs@^4.1.3": version "4.1.6" resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.6.tgz#e14b2576a1c25026b7f02ede1de3b84c3a1efeae" From b5a5ed96632dee13918d0c8381a5971713d588a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 15 May 2025 21:37:44 +0200 Subject: [PATCH 2/2] use `fs/promises` consistently --- src/index.ts | 13 +++++++------ src/run.test.ts | 14 +++++++------- src/run.ts | 6 +++--- src/utils.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7bf242cb..e3ad4218 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,10 @@ import * as core from "@actions/core"; -import fs from "node:fs"; +import fs from "node:fs/promises"; import { Git } from "./git"; import { setupOctokit } from "./octokit"; import readChangesetState from "./readChangesetState"; import { runPublish, runVersion } from "./run"; +import { fileExists } from "./utils"; const getOptionalInput = (name: string) => core.getInput(name) || undefined; @@ -41,7 +42,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; } core.info("setting GitHub credentials"); - fs.writeFileSync( + await fs.writeFile( `${process.env.HOME}/.netrc`, `machine github.com\nlogin github-actions[bot]\npassword ${githubToken}` ); @@ -71,9 +72,9 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; ); let userNpmrcPath = `${process.env.HOME}/.npmrc`; - if (fs.existsSync(userNpmrcPath)) { + if (await fileExists(userNpmrcPath)) { core.info("Found existing user .npmrc file"); - const userNpmrcContent = fs.readFileSync(userNpmrcPath, "utf8"); + const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8"); const authLine = userNpmrcContent.split("\n").find((line) => { // check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line); @@ -86,14 +87,14 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; core.info( "Didn't find existing auth token for the npm registry in the user .npmrc file, creating one" ); - fs.appendFileSync( + await fs.appendFile( userNpmrcPath, `\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` ); } } else { core.info("No user .npmrc file found, creating one"); - fs.writeFileSync( + await fs.writeFile( userNpmrcPath, `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` ); diff --git a/src/run.test.ts b/src/run.test.ts index 45f6b9f5..1c9a1013 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -1,7 +1,7 @@ import { Changeset } from "@changesets/types"; import writeChangeset from "@changesets/write"; import fixturez from "fixturez"; -import fs from "node:fs"; +import fs from "node:fs/promises"; import path from "node:path"; import { Git } from "./git"; import { setupOctokit } from "./octokit"; @@ -36,7 +36,7 @@ let mockedGithubMethods = { let f = fixturez(__dirname); const linkNodeModules = async (cwd: string) => { - fs.symlinkSync( + await fs.symlink( path.join(__dirname, "..", "node_modules"), path.join(cwd, "node_modules") ); @@ -52,7 +52,7 @@ beforeEach(() => { describe("version", () => { it("creates simple PR", async () => { let cwd = f.copy("simple-project"); - linkNodeModules(cwd); + await linkNodeModules(cwd); mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); @@ -90,7 +90,7 @@ describe("version", () => { it("only includes bumped packages in the PR body", async () => { let cwd = f.copy("simple-project"); - linkNodeModules(cwd); + await linkNodeModules(cwd); mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); @@ -124,7 +124,7 @@ describe("version", () => { it("doesn't include ignored package that got a dependency update in the PR body", async () => { let cwd = f.copy("ignored-package"); - linkNodeModules(cwd); + await linkNodeModules(cwd); mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); @@ -158,7 +158,7 @@ describe("version", () => { it("does not include changelog entries if full message exceeds size limit", async () => { let cwd = f.copy("simple-project"); - linkNodeModules(cwd); + await linkNodeModules(cwd); mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); @@ -216,7 +216,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis. it("does not include any release information if a message with simplified release info exceeds size limit", async () => { let cwd = f.copy("simple-project"); - linkNodeModules(cwd); + await linkNodeModules(cwd); mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); diff --git a/src/run.ts b/src/run.ts index d96a3e60..80f8a851 100644 --- a/src/run.ts +++ b/src/run.ts @@ -3,7 +3,7 @@ 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 fs from "node:fs"; +import fs from "node:fs/promises"; import path from "node:path"; import resolveFrom from "resolve-from"; import * as semver from "semver"; @@ -30,7 +30,7 @@ const createRelease = async ( ) => { let changelog; try { - changelog = fs.readFileSync(path.join(pkg.dir, "CHANGELOG.md"), "utf8"); + changelog = await fs.readFile(path.join(pkg.dir, "CHANGELOG.md"), "utf8"); } catch (err) { if (isErrorWithCode(err, "ENOENT")) { // if we can't find a changelog, the user has probably disabled changelogs @@ -294,7 +294,7 @@ export async function runVersion({ let changedPackages = await getChangedPackages(cwd, versionsByDirectory); let changedPackagesInfoPromises = Promise.all( changedPackages.map(async (pkg) => { - let changelogContents = fs.readFileSync( + let changelogContents = await fs.readFile( path.join(pkg.dir, "CHANGELOG.md"), "utf8" ); diff --git a/src/utils.ts b/src/utils.ts index f2bfacbf..319b69d9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import unified from "unified"; import remarkParse from "remark-parse"; import remarkStringify from "remark-stringify"; +import fs from "node:fs/promises"; import type { Root } from "mdast"; // @ts-ignore import mdastToString from "mdast-util-to-string"; @@ -105,3 +106,10 @@ export function isErrorWithCode(err: unknown, code: string) { err.code === code ); } + +export function fileExists(filePath: string) { + return fs.access(filePath, fs.constants.F_OK).then( + () => true, + () => false + ); +}