Skip to content

Commit 62716ea

Browse files
authored
fix: update dry-run and publish conflict logic for npm>=10 (#232)
1 parent d76128b commit 62716ea

File tree

8 files changed

+74
-41
lines changed

8 files changed

+74
-41
lines changed

.github/workflows/ci-cd.yaml

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ jobs:
2525
- macos-latest
2626
- windows-latest
2727
node-version:
28-
- 16
29-
- 18
30-
- 20
28+
- "16"
29+
- "18"
30+
- "20"
31+
- "22"
32+
- "24"
3133

3234
steps:
3335
- name: Checkout source
@@ -107,9 +109,11 @@ jobs:
107109
fail-fast: false
108110
matrix:
109111
node-version:
110-
- 16
111-
- 18
112-
- 20
112+
- "16"
113+
- "18"
114+
- "20"
115+
- "22"
116+
- "24"
113117

114118
services:
115119
verdaccio:
@@ -173,6 +177,21 @@ jobs:
173177
shell: bash
174178
run: ./e2e/01-setup-package.sh "${{ steps.setup.outputs.package }}" 0.0.3
175179

180+
- id: action-publish-dry-before
181+
name: Publish a dry run before publish
182+
uses: ./
183+
with:
184+
registry: http://localhost:4873
185+
package: ${{ steps.setup.outputs.package }}/package.json
186+
token: ${{ steps.setup.outputs.token }}
187+
dry-run: true
188+
189+
- name: Check release output
190+
if: ${{ steps.action-publish-dry-before.outputs.type != 'patch' }}
191+
run: |
192+
echo "::error::Expected release type to be 'patch', got '${{ steps.action-publish-dry-before.outputs.type }}'"
193+
exit 1
194+
176195
- id: action-publish
177196
name: Publish a new version
178197
uses: ./
@@ -187,8 +206,8 @@ jobs:
187206
echo "::error::Expected release type to be 'patch', got '${{ steps.action-publish.outputs.type }}'"
188207
exit 1
189208
190-
- id: action-publish-dry
191-
name: Publish a dry run
209+
- id: action-publish-dry-after
210+
name: Publish a dry run after publish
192211
uses: ./
193212
with:
194213
registry: http://localhost:4873
@@ -197,9 +216,9 @@ jobs:
197216
dry-run: true
198217

199218
- name: Check release output
200-
if: ${{ steps.action-publish-dry.outputs.type }}
219+
if: ${{ steps.action-publish-dry-after.outputs.type }}
201220
run: |
202-
echo "::error::Expected release type to be '', got '${{ steps.action-publish-dry.outputs.type }}'"
221+
echo "::error::Expected release type to be '', got '${{ steps.action-publish-dry-after.outputs.type }}'"
203222
exit 1
204223
205224
deploy:

dist/main.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compare-and-publish/__tests__/compare-and-publish.test.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,35 @@ describe("compareAndPublish", () => {
224224
await expect(result).rejects.toThrow(errors.NpmCallError);
225225
});
226226

227-
it("should allow an EPUBLISHCONFLICT from npm publish", async () => {
228-
when(callNpmCli<"publish">)
229-
.calledWith("publish", ["."], {
230-
logger,
231-
environment,
232-
ignoreScripts: false,
233-
})
234-
.thenResolve({
235-
successData: undefined,
236-
errorCode: "EPUBLISHCONFLICT",
237-
error: new errors.NpmCallError("publish", 1, "oh no"),
227+
it.each(["EPUBLISHCONFLICT", "E409"])(
228+
"should allow an %s error from npm publish",
229+
async (errorCode) => {
230+
when(callNpmCli<"publish">)
231+
.calledWith("publish", ["."], {
232+
logger,
233+
environment,
234+
ignoreScripts: false,
235+
})
236+
.thenResolve({
237+
successData: undefined,
238+
errorCode,
239+
error: new errors.NpmCallError("publish", 1, "oh no"),
240+
});
241+
242+
const result = await subject.compareAndPublish(
243+
manifest,
244+
normalizedOptions,
245+
environment
246+
);
247+
248+
expect(result).toEqual({
249+
id: undefined,
250+
files: [],
251+
oldVersion: "0.0.1",
252+
type: undefined,
238253
});
239-
240-
const result = await subject.compareAndPublish(
241-
manifest,
242-
normalizedOptions,
243-
environment
244-
);
245-
246-
expect(result).toEqual({
247-
id: undefined,
248-
files: [],
249-
oldVersion: "0.0.1",
250-
type: undefined,
251-
});
252-
});
254+
}
255+
);
253256

254257
it("should raise a non-EPUBLISHCONFLIG from npm publish", async () => {
255258
when(callNpmCli<"publish">)

src/compare-and-publish/__tests__/get-arguments.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe("get command arguments", () => {
4444
"restricted",
4545
"--provenance",
4646
"--dry-run",
47+
"--force",
4748
]);
4849
});
4950

src/compare-and-publish/compare-and-publish.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
VIEW,
55
PUBLISH,
66
E404,
7+
E409,
78
EPUBLISHCONFLICT,
89
callNpmCli,
910
type NpmCliEnvironment,
@@ -66,7 +67,11 @@ export async function compareAndPublish(
6667
? await callNpmCli(PUBLISH, publishArguments, cliOptions)
6768
: { successData: undefined, errorCode: undefined, error: undefined };
6869

69-
if (publishCall.error && publishCall.errorCode !== EPUBLISHCONFLICT) {
70+
if (
71+
publishCall.error &&
72+
publishCall.errorCode !== EPUBLISHCONFLICT &&
73+
publishCall.errorCode !== E409
74+
) {
7075
throw publishCall.error;
7176
}
7277

src/compare-and-publish/get-arguments.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ export function getPublishArguments(
5454
}
5555

5656
if (!dryRun.isDefault && dryRun.value) {
57-
publishArguments.push("--dry-run");
57+
// NOTE: `--force` does not override `--dry-run`,
58+
// but does bypass package existence check in npm >=11
59+
// because we do our own existence checks separately
60+
publishArguments.push("--dry-run", "--force");
5861
}
5962

6063
return publishArguments;

src/npm/call-npm-cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const VIEW = "view";
3636
export const PUBLISH = "publish";
3737

3838
export const E404 = "E404";
39+
export const E409 = "E409";
3940
export const EPUBLISHCONFLICT = "EPUBLISHCONFLICT";
4041

4142
const IS_WINDOWS = os.platform() === "win32";

0 commit comments

Comments
 (0)