Skip to content

Commit

Permalink
add skip npm option
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Feb 24, 2025
1 parent e4b166c commit dc89c2d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
15 changes: 15 additions & 0 deletions fixtures/pnpm/star-package/do-not-publish/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "do-not-publish",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"release-plan": {
"skipNpmPublish": true
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"prepare": "pnpm run build",
"test": "vitest",
"test": "vitest run",
"coverage": "vitest run --coverage"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/interdep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('interdep', function () {

expect(Array(...answer.keys())).toMatchInlineSnapshot(`
[
"do-not-publish",
"star-package",
]
`);
Expand Down
60 changes: 59 additions & 1 deletion src/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IssueReporter,
} from './publish.js';
import { Solution } from './plan.js';
import { getPackages } from './interdep.js';

// we aren't currently using this so we can just ignore for now
const reporter = new IssueReporter();
Expand Down Expand Up @@ -217,7 +218,12 @@ describe('publish', function () {
new Map([
[
'release-plan',
{ oldVersion: '0.8.1', newVersion: '0.9.0', impact: 'minor' },
{
oldVersion: '0.8.1',
newVersion: '0.9.0',
impact: 'minor',
pkgJSONPath: './package.json',
},
],
]) as Solution,
reporter,
Expand All @@ -241,6 +247,58 @@ describe('publish', function () {
}
`);
});

it('skips publishing if npmSkipPublish is specified in package.json', async function () {
const consoleSpy = vi.spyOn(process.stdout, 'write');
const packages = getPackages('./fixtures/pnpm/star-package');
const publishResult = await npmPublish(
new Map([
[
'do-not-publish',
{
oldVersion: '0.8.1',
newVersion: '0.9.0',
impact: 'minor',
pkgJSONPath: packages.get('do-not-publish')?.pkgJSONPath,
},
],
[
'star-package',
{
oldVersion: '0.8.1',
newVersion: '0.9.0',
impact: 'minor',
pkgJSONPath: packages.get('star-package')?.pkgJSONPath,
},
],
]) as Solution,
reporter,
{
tag: 'best-tag',
dryRun: true,
},
'face',
);

expect(consoleSpy.mock.lastCall?.[0]).toMatchInlineSnapshot(`
"
ℹ️ --dryRun active. Adding \`--dry-run\` flag to \`face publish\` for star-package, which would publish version 0.9.0
"
`);

expect(publishResult).toMatchInlineSnapshot(`
{
"args": [
"publish",
"--tag=best-tag",
"--dry-run",
],
"released": Map {
"star-package" => "0.9.0",
},
}
`);
});
});

describe('createGithubRelease', function () {
Expand Down
11 changes: 10 additions & 1 deletion src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import latestVersion from 'latest-version';
import { dirname } from 'path';
import PackageJson from '@npmcli/package-json';
import parseGithubUrl from 'parse-github-repo-url';
import fsExtra from 'fs-extra';
import fsExtra, { readJSONSync } from 'fs-extra';

const { existsSync } = fsExtra;

Expand Down Expand Up @@ -266,6 +266,14 @@ export async function npmPublish(
continue;
}

const pkg = readJSONSync(entry.pkgJSONPath);
if (pkg['release-plan']?.skipNpmPublish) {
info(
`skipping publish for ${pkgName}, as config option skipNpmPublish is set in its package.json`,
);
continue;
}

const preExisting = await doesVersionExist(
pkgName,
entry.newVersion,
Expand Down Expand Up @@ -294,6 +302,7 @@ export async function npmPublish(
stderr: 'inherit',
stdout: 'inherit',
});
released.set(pkgName, entry.newVersion);
} catch (err) {
reporter.reportFailure(
`Failed to ${packageManager} publish ${pkgName} - Error: ${err.message}`,
Expand Down

0 comments on commit dc89c2d

Please sign in to comment.