Skip to content

Commit

Permalink
Replace @antfu/ni with detect-package-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Oct 21, 2024
1 parent 173f712 commit 9bd7e31
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-pugs-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'changesets-snapshot': patch
---

Replace `@antfu/ni` dependency with `detect-package-manager`
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"main": "dist/index.js",
"scripts": {
"build": "tsup",
"changeset": "changeset",
"format": "skuba format",
"lint": "skuba lint",
"release": "node ./scripts/release.mjs",
Expand All @@ -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",
Expand Down
73 changes: 35 additions & 38 deletions src/publish.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -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<typeof detect>,
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 () => {
Expand Down Expand Up @@ -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();

Expand Down
23 changes: 17 additions & 6 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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: <code>${name}@${version}</code>`,
codeBlock: getCommand(packageManager, 'add', [
`${name}@${cleansedBranchName}`,
]),
codeBlock: `${command} ${args.join(' ')}`,
});
}

Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 9bd7e31

Please sign in to comment.