diff --git a/.changeset/rotten-crabs-rest.md b/.changeset/rotten-crabs-rest.md new file mode 100644 index 000000000..841007847 --- /dev/null +++ b/.changeset/rotten-crabs-rest.md @@ -0,0 +1,5 @@ +--- +"@changesets/cli": patch +--- + +Private packages without a `version` field are no longer listed when adding a changeset. diff --git a/__fixtures__/private-package-without-version-field/.changeset/config.json b/__fixtures__/private-package-without-version-field/.changeset/config.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/__fixtures__/private-package-without-version-field/.changeset/config.json @@ -0,0 +1 @@ +{} diff --git a/__fixtures__/private-package-without-version-field/package.json b/__fixtures__/private-package-without-version-field/package.json new file mode 100644 index 000000000..b245108a3 --- /dev/null +++ b/__fixtures__/private-package-without-version-field/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "private-package-without-version-field", + "description": "Base yarn workspace work", + "version": "1.0.0", + "workspaces": [ + "packages/*" + ] +} diff --git a/__fixtures__/private-package-without-version-field/packages/pkg-a/package.json b/__fixtures__/private-package-without-version-field/packages/pkg-a/package.json new file mode 100644 index 000000000..ee09b194d --- /dev/null +++ b/__fixtures__/private-package-without-version-field/packages/pkg-a/package.json @@ -0,0 +1,4 @@ +{ + "name": "pkg-a", + "version": "1.0.0" +} diff --git a/__fixtures__/private-package-without-version-field/packages/pkg-b/package.json b/__fixtures__/private-package-without-version-field/packages/pkg-b/package.json new file mode 100644 index 000000000..1a17612fe --- /dev/null +++ b/__fixtures__/private-package-without-version-field/packages/pkg-b/package.json @@ -0,0 +1,4 @@ +{ + "name": "pkg-b", + "private": true +} diff --git a/__fixtures__/private-package-without-version-field/packages/pkg-c/package.json b/__fixtures__/private-package-without-version-field/packages/pkg-c/package.json new file mode 100644 index 000000000..888ba5c7e --- /dev/null +++ b/__fixtures__/private-package-without-version-field/packages/pkg-c/package.json @@ -0,0 +1,4 @@ +{ + "name": "pkg-c", + "version": "1.0.0" +} diff --git a/packages/cli/src/commands/add/__tests__/add.ts b/packages/cli/src/commands/add/__tests__/add.ts index d63be8326..b12d4260e 100644 --- a/packages/cli/src/commands/add/__tests__/add.ts +++ b/packages/cli/src/commands/add/__tests__/add.ts @@ -214,6 +214,16 @@ describe("Changesets", () => { { ...defaultConfig, ignore: ["pkg-b"] } ); + // @ts-ignore + const { choices } = askCheckboxPlus.mock.calls[0][1][0]; + expect(choices).toEqual(["pkg-a", "pkg-c"]); + }); + it("should not include private packages without a version in the prompt", async () => { + const cwd = await f.copy("private-package-without-version-field"); + + mockUserResponses({ releases: { "pkg-a": "patch" } }); + await addChangeset(cwd, { empty: false }, defaultConfig); + // @ts-ignore const { choices } = askCheckboxPlus.mock.calls[0][1][0]; expect(choices).toEqual(["pkg-a", "pkg-c"]); diff --git a/packages/cli/src/commands/add/createChangeset.ts b/packages/cli/src/commands/add/createChangeset.ts index 87aa9fe1f..3f6aab493 100644 --- a/packages/cli/src/commands/add/createChangeset.ts +++ b/packages/cli/src/commands/add/createChangeset.ts @@ -96,6 +96,12 @@ function formatPkgNameAndVersion(pkgName: string, version: string) { return `${bold(pkgName)}@${bold(version)}`; } +function getPkgJsonByName(packages: Package[]) { + return new Map( + packages.map(({ packageJson }) => [packageJson.name, packageJson]) + ); +} + export default async function createChangeset( changedPackages: Array, allPackages: Package[] @@ -108,9 +114,7 @@ export default async function createChangeset( allPackages ); - let pkgJsonsByName = new Map( - allPackages.map(({ packageJson }) => [packageJson.name, packageJson]) - ); + let pkgJsonsByName = getPkgJsonByName(allPackages); let pkgsLeftToGetBumpTypeFor = new Set(packagesToRelease); diff --git a/packages/cli/src/commands/add/index.ts b/packages/cli/src/commands/add/index.ts index eee5d8f3f..7a128791b 100644 --- a/packages/cli/src/commands/add/index.ts +++ b/packages/cli/src/commands/add/index.ts @@ -13,22 +13,26 @@ import { getCommitFunctions } from "../../commit/getCommitFunctions"; import createChangeset from "./createChangeset"; import printConfirmationMessage from "./messages"; import { ExternalEditor } from "external-editor"; +import { PackageJSON } from "@changesets/types"; -type UnwrapPromise> = T extends Promise - ? R - : never; +function isListablePackage(config: Config, packageJson: PackageJSON) { + return ( + !config.ignore.includes(packageJson.name) && + (packageJson.version || !packageJson.private) + ); +} export default async function add( cwd: string, { empty, open }: { empty?: boolean; open?: boolean }, config: Config ) { - const packages = (await getPackages(cwd)).packages.filter( - pkg => !config.ignore.includes(pkg.packageJson.name) + const packages = (await getPackages(cwd)).packages.filter(pkg => + isListablePackage(config, pkg.packageJson) ); const changesetBase = path.resolve(cwd, ".changeset"); - let newChangeset: UnwrapPromise>; + let newChangeset: Awaited>; if (empty) { newChangeset = { confirmed: true, @@ -40,11 +44,11 @@ export default async function add( cwd, ref: config.baseBranch }); - const changePackagesName = changedPackages - .map(pkg => pkg.packageJson.name) - .filter(pkgName => !config.ignore.includes(pkgName)); + const changedPackagesName = changedPackages + .filter(pkg => isListablePackage(config, pkg.packageJson)) + .map(pkg => pkg.packageJson.name); - newChangeset = await createChangeset(changePackagesName, packages); + newChangeset = await createChangeset(changedPackagesName, packages); printConfirmationMessage(newChangeset, packages.length > 1); if (!newChangeset.confirmed) {