diff --git a/.changeset/wild-pugs-invite.md b/.changeset/wild-pugs-invite.md new file mode 100644 index 0000000..a216a14 --- /dev/null +++ b/.changeset/wild-pugs-invite.md @@ -0,0 +1,5 @@ +--- +'changesets-snapshot': patch +--- + +Replace `@antfu/ni` dependency with `detect-package-manager` diff --git a/package.json b/package.json index 0573deb..02be1f0 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "main": "dist/index.js", "scripts": { "build": "tsup", + "changeset": "changeset", "format": "skuba format", "lint": "skuba lint", "release": "node ./scripts/release.mjs", @@ -17,11 +18,11 @@ "@actions/core": "^1.10.0", "@actions/exec": "^1.1.1", "@actions/github": "^6.0.0", - "@antfu/ni": "^0.23.0", "@changesets/changelog-github": "^0.5.0", "@changesets/cli": "^2.26.0", "@manypkg/get-packages": "^2.0.0", "@types/node": "^20.11.16", + "package-manager-detector": "^0.2.2", "prettier": "^3.0.0", "resolve-from": "^5.0.0", "skuba": "8.2.1", diff --git a/src/publish.test.ts b/src/publish.test.ts index e916f9d..6cbfb86 100644 --- a/src/publish.test.ts +++ b/src/publish.test.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import { detect } from '@antfu/ni'; +import { detect } from 'package-manager-detector/detect'; import resolveFrom from 'resolve-from'; import { logger } from './logger'; @@ -9,10 +9,7 @@ import { run, runPublish } from './run'; jest.mock('@actions/github'); jest.mock('@actions/core'); -jest.mock('@antfu/ni', () => ({ - ...jest.requireActual('@antfu/ni'), - detect: jest.fn().mockName('@antfu/ni.detect'), -})); +jest.mock('package-manager-detector/detect'); jest.mock('resolve-from'); jest.mock('./npm-utils'); jest.mock('./run'); @@ -42,41 +39,41 @@ afterEach(() => { jest.clearAllMocks(); }); -test.each(['yarn', 'npm', 'pnpm'])( - 'command output for %s', - async (packageManager) => { - process.env.GITHUB_TOKEN = '@github-token'; - process.env.NPM_TOKEN = '@npm-token'; - github.context.ref = 'feature/123-branch'; - runMock.mockResolvedValueOnce({ - code: 0, - stdout: '', - stderr: '', - }); - runPublishMock.mockResolvedValueOnce({ - published: true, - publishedPackages: [ - { name: '@multiple/package1', version: '1.2.3-SNAPSHOT' }, - { name: '@multiple/package-two', version: '1.2.3-SNAPSHOT' }, - ], - }); - detectMock.mockResolvedValueOnce( - packageManager as unknown as ReturnType, +const testCases = ['yarn', 'npm', 'pnpm'] as const; + +test.each(testCases)('command output for %s', async (packageManager) => { + process.env.GITHUB_TOKEN = '@github-token'; + process.env.NPM_TOKEN = '@npm-token'; + github.context.ref = 'feature/123-branch'; + runMock.mockResolvedValueOnce({ + code: 0, + stdout: '', + stderr: '', + }); + runPublishMock.mockResolvedValueOnce({ + published: true, + publishedPackages: [ + { name: '@multiple/package1', version: '1.2.3-SNAPSHOT' }, + { name: '@multiple/package-two', version: '1.2.3-SNAPSHOT' }, + ], + }); + detectMock.mockResolvedValueOnce({ + name: packageManager, + agent: packageManager, + }); + jest + .mocked(resolveFrom) + .mockImplementationOnce( + (_fromDirectory, moduleId) => `/__mocked_node_modules__/${moduleId}`, ); - jest - .mocked(resolveFrom) - .mockImplementationOnce( - (_fromDirectory, moduleId) => `/__mocked_node_modules__/${moduleId}`, - ); - await publishSnapshot(); + await publishSnapshot(); - expect(getScriptCalls(runMock)).toMatchSnapshot('run'); - expect(getScriptCalls(runPublishMock)).toMatchSnapshot('runPublish'); - expect(jest.mocked(logger).log.mock.calls[0]).toMatchSnapshot('logger.log'); - expectSummary(); - }, -); + expect(getScriptCalls(runMock)).toMatchSnapshot('run'); + expect(getScriptCalls(runPublishMock)).toMatchSnapshot('runPublish'); + expect(jest.mocked(logger).log.mock.calls[0]).toMatchSnapshot('logger.log'); + expectSummary(); +}); describe('error handling', () => { test('missing NPM token', async () => { @@ -115,7 +112,7 @@ describe('error handling', () => { stdout: '', stderr: '\nNo unreleased changesets found\n', }); - detectMock.mockResolvedValueOnce('yarn'); + detectMock.mockResolvedValueOnce({ name: 'yarn', agent: 'yarn' }); await publishSnapshot(); diff --git a/src/publish.ts b/src/publish.ts index 15a2307..ec70611 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import { detect, getCommand } from '@antfu/ni'; +import { resolveCommand } from 'package-manager-detector/commands'; +import { detect } from 'package-manager-detector/detect'; import resolveFrom from 'resolve-from'; import { logger } from './logger'; @@ -45,11 +46,13 @@ export const publishSnapshot = async () => { throw failure('Unable to retrieve NPM publish token'); } - const packageManager = await detect({ cwd }); - if (!packageManager) { + const detectResult = await detect({ cwd }); + if (!detectResult) { throw failure('Unable to detect package manager'); } + const { name: packageManager } = detectResult; + const preVersionScript = core.getInput('pre-version'); if (preVersionScript) { await run({ script: preVersionScript, cwd }); @@ -95,12 +98,20 @@ export const publishSnapshot = async () => { result.publishedPackages.length === 1 ? 'snapshot' : 'snapshots'; for (const { name, version } of result.publishedPackages) { + const resolvedCommand = resolveCommand(packageManager, 'add', [ + `${name}@${cleansedBranchName}`, + ]); + + if (!resolvedCommand) { + throw new Error('Failed to resolve command'); + } + + const { command, args } = resolvedCommand; + await writeSummary({ title: '🦋 New snapshot published!', message: `Version: ${name}@${version}`, - codeBlock: getCommand(packageManager, 'add', [ - `${name}@${cleansedBranchName}`, - ]), + codeBlock: `${command} ${args.join(' ')}`, }); } diff --git a/yarn.lock b/yarn.lock index 13dc749..711abf6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -48,11 +48,6 @@ "@jridgewell/gen-mapping" "^0.3.5" "@jridgewell/trace-mapping" "^0.3.24" -"@antfu/ni@^0.23.0": - version "0.23.0" - resolved "https://registry.yarnpkg.com/@antfu/ni/-/ni-0.23.0.tgz#5f52055865963d5aa06dd759da43e82bbd670ca1" - integrity sha512-R5/GkA3PfGewAXLzz6lN5XagunF6PKeDtWt8dbZQXvHfebLS0qEczV+Azg/d+tKgSh6kRBpxvu8oSjARdPtw0A== - "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.12.13", "@babel/code-frame@^7.22.13", "@babel/code-frame@^7.24.7": version "7.24.7" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.24.7.tgz#882fd9e09e8ee324e496bd040401c6f046ef4465" @@ -6369,6 +6364,11 @@ package-json-from-dist@^1.0.0: resolved "https://registry.yarnpkg.com/package-json-from-dist/-/package-json-from-dist-1.0.0.tgz#e501cd3094b278495eb4258d4c9f6d5ac3019f00" integrity sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw== +package-manager-detector@^0.2.2: + version "0.2.2" + resolved "https://registry.yarnpkg.com/package-manager-detector/-/package-manager-detector-0.2.2.tgz#fbbc8afe87cdaee471ca9b89c3700236c6d2d9e5" + integrity sha512-VgXbyrSNsml4eHWIvxxG/nTL4wgybMTXCV2Un/+yEc3aDKKU6nQBZjbeP3Pl3qm9Qg92X/1ng4ffvCeD/zwHgg== + pacote@^18.0.0, pacote@^18.0.6: version "18.0.6" resolved "https://registry.yarnpkg.com/pacote/-/pacote-18.0.6.tgz#ac28495e24f4cf802ef911d792335e378e86fac7"