Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Filter out private packages without a version field when adding a cha…
…ngeset (#720)

* fix: make cli skip private packages without version field

* juggle code and add a test

* add changeset

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
bhovhannes and Andarist committed Aug 5, 2022
1 parent 8627ec1 commit 0fd56e1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .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.
@@ -0,0 +1 @@
{}
@@ -0,0 +1,9 @@
{
"private": true,
"name": "private-package-without-version-field",
"description": "Base yarn workspace work",
"version": "1.0.0",
"workspaces": [
"packages/*"
]
}
@@ -0,0 +1,4 @@
{
"name": "pkg-a",
"version": "1.0.0"
}
@@ -0,0 +1,4 @@
{
"name": "pkg-b",
"private": true
}
@@ -0,0 +1,4 @@
{
"name": "pkg-c",
"version": "1.0.0"
}
10 changes: 10 additions & 0 deletions packages/cli/src/commands/add/__tests__/add.ts
Expand Up @@ -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"]);
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/commands/add/createChangeset.ts
Expand Up @@ -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<string>,
allPackages: Package[]
Expand All @@ -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);

Expand Down
24 changes: 14 additions & 10 deletions packages/cli/src/commands/add/index.ts
Expand Up @@ -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<any>> = T extends Promise<infer R>
? 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<ReturnType<typeof createChangeset>>;
let newChangeset: Awaited<ReturnType<typeof createChangeset>>;
if (empty) {
newChangeset = {
confirmed: true,
Expand All @@ -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) {
Expand Down

0 comments on commit 0fd56e1

Please sign in to comment.