Skip to content

Commit

Permalink
Filter package list based on tracking private packages config
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Aug 11, 2022
1 parent 28223ae commit 9e4c45a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
42 changes: 39 additions & 3 deletions packages/cli/src/commands/add/createChangeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function confirmMajorRelease(pkgJSON: PackageJSON) {

async function getPackagesToRelease(
changedPackages: Array<string>,
allPackages: Array<Package>
allPackages: Array<Package>,
trackPrivatePackages: boolean
) {
function askInitialReleaseQuestion(defaultChoiceList: Array<any>) {
return cli.askCheckboxPlus(
Expand All @@ -58,6 +59,20 @@ async function getPackagesToRelease(
);
}

const pkgJsonsByName = getPkgJsonByName(allPackages);

// filter out packages which changesets is not tracking
allPackages = allPackages.filter(
pkg => !isValidChangesetPackage(pkg.packageJson, trackPrivatePackages)
);
changedPackages = changedPackages.filter(
pkgName =>
!isValidChangesetPackage(
pkgJsonsByName.get(pkgName)!,
trackPrivatePackages
)
);

if (allPackages.length > 1) {
const unchangedPackagesNames = allPackages
.map(({ packageJson }) => packageJson.name)
Expand Down Expand Up @@ -92,6 +107,25 @@ async function getPackagesToRelease(
return [allPackages[0].packageJson.name];
}

function getPkgJsonByName(packages: Package[]) {
return new Map(
packages.map(({ packageJson }) => [packageJson.name, packageJson])
);
}

function isValidChangesetPackage(
packageJson: PackageJSON,
trackPrivatePackages: boolean
) {
const hasVersionField = !!packageJson.version;

if (trackPrivatePackages) {
return hasVersionField;
}

return !packageJson.private && hasVersionField;
}

function formatPkgNameAndVersion(pkgName: string, version: string) {
return `${bold(pkgName)}@${bold(version)}`;
}
Expand All @@ -104,14 +138,16 @@ function getPkgJsonByName(packages: Package[]) {

export default async function createChangeset(
changedPackages: Array<string>,
allPackages: Package[]
allPackages: Package[],
trackPrivatePackages: boolean
): Promise<{ confirmed: boolean; summary: string; releases: Array<Release> }> {
const releases: Array<Release> = [];

if (allPackages.length > 1) {
const packagesToRelease = await getPackagesToRelease(
changedPackages,
allPackages
allPackages,
trackPrivatePackages
);

let pkgJsonsByName = getPkgJsonByName(allPackages);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function add(
.filter(pkg => isListablePackage(config, pkg.packageJson))
.map(pkg => pkg.packageJson.name);

newChangeset = await createChangeset(changedPackagesName, packages);
newChangeset = await createChangeset(changedPackagesName, packages, config.enablePrivateTracking);
printConfirmationMessage(newChangeset, packages.length > 1);

if (!newChangeset.confirmed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ describe("publishPackages", () => {
await publishPackages({
packages: (await getPackages(cwd)).packages,
access: "public",
preState: undefined
preState: undefined,
trackPrivatePackages: true
});
expect(npmUtils.getTokenIsRequired).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit 9e4c45a

Please sign in to comment.