Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/friendly-tools-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/format": patch
---

Fix formatter command execution on Windows.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
33 changes: 24 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
annotations: true
advanced-security: false

test:
name: Test
checks:
name: Build, lint, and typecheck
runs-on: ubuntu-latest
timeout-minutes: 10
env:
Expand All @@ -53,12 +53,6 @@ jobs:

- uses: ./.github/actions/ci-setup

# Deno is needed for tests only
- name: Set up Deno
uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282 # v2.0.4
with:
deno-version: 2.x

- name: Build
run: pnpm build

Expand All @@ -68,14 +62,35 @@ jobs:
- name: Typecheck
run: pnpm typecheck

test:
name: "Test on ${{ matrix.os }}"
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
fail-fast: false
steps:
- name: Check out repo
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
persist-credentials: false

- uses: ./.github/actions/ci-setup

- name: Set up Deno
uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282 # v2.0.4
with:
deno-version: 2.x

- name: Test
run: pnpm test

ci-ok:
name: CI OK
runs-on: ubuntu-latest
if: always()
needs: [lint-workflows, test]
needs: [lint-workflows, checks, test]
steps:
- name: Exit with error if some jobs are not successful
run: exit 1
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"release": "changeset publish"
},
"dependencies": {
"package-manager-detector": "^1.6.0"
"package-manager-detector": "^1.6.0",
"tinyexec": "^1.2.4"
},
"devDependencies": {
"@changesets/changelog-github": "^1.0.0-next.3",
Expand Down
13 changes: 5 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 4 additions & 20 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cp from "node:child_process";
import path from "node:path";
import { resolveCommand, type Agent } from "package-manager-detector";
import { exec } from "tinyexec";

/**
* Traverse upwards from `startDir` to `stopDir` (inclusive), calling `cb` in each directory. If
Expand Down Expand Up @@ -37,24 +37,8 @@ export async function packageManagerExecute(
}

export async function spawnProcess(command: string, args: string[], cwd: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = cp.spawn(command, args, { cwd });

let stderr = "";
child.stderr.on("data", (data) => {
stderr += data.toString();
});

child.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`${command} exited with code ${code}. Stderr:\n${stderr}`));
}
});

child.on("error", (err) => {
reject(err);
});
await exec(command, args, {
nodeOptions: { cwd },
throwOnError: true,
});
}
27 changes: 6 additions & 21 deletions tests/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@ import { expect, test, vi } from "vitest";
import { format, type FormatterName } from "../src/index.ts";

const mocks = vi.hoisted(() => ({
spawn: vi.fn().mockReturnValue({
// mock the bare-minimum based on how `src/utils.ts` uses `spawn`
on: (eventName: string, listener: Function) => {
if (eventName === "exit") {
listener(0); // simulate successful exit
}
},
stderr: { on: () => {} },
}),
exec: vi.fn().mockResolvedValue({ exitCode: 0, stderr: "", stdout: "" }),
}));

vi.mock(import("node:child_process"), async (importOriginal) => {
const mod = await importOriginal();
return {
...mod,
spawn: mocks.spawn,
default: {
...mod.default,
spawn: mocks.spawn,
},
};
});
vi.mock(import("tinyexec"), () => ({ exec: mocks.exec }));

const cases: { formatter: FormatterName; expectedCommand: string[] }[] = [
{ formatter: "prettier", expectedCommand: ["npx", "prettier", "--write", "file.ts"] },
Expand All @@ -42,5 +24,8 @@ test.for(cases)("executes the correct command for $formatter", async (c) => {
const result = await format(["file.ts"], { cwd: fixture.path, formatter: c.formatter });
expect(result).toBe(true);
const [command, ...args] = c.expectedCommand;
expect(mocks.spawn).toHaveBeenCalledWith(command, args, { cwd: fixture.path });
expect(mocks.exec).toHaveBeenCalledWith(command, args, {
nodeOptions: { cwd: fixture.path },
throwOnError: true,
});
});