Skip to content

Commit

Permalink
Use longest path to determine package (#352)
Browse files Browse the repository at this point in the history
* Use longest path to determine package 
close #351

* Changeset added

* Update green-buttons-switch.md

* Update package.json

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
  • Loading branch information
MichaelKapustey and emmatown committed May 6, 2020
1 parent 142dc7c commit 89f0c49
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-buttons-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/git": patch
---

Previously packages nested inside of other packages would show both the nested package and the outer package as changed. Now, only the nested package will show as changed.
1 change: 1 addition & 0 deletions __fixtures__/with-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"version": "1.0.0",
"bolt": {
"workspaces": [
"packages",
"packages/*"
]
}
Expand Down
4 changes: 4 additions & 0 deletions __fixtures__/with-git/packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "packages",
"version": "1.0.0"
}
12 changes: 7 additions & 5 deletions packages/git/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ describe("git", () => {
.split("\n")
.filter(a => a);

expect(stagedFiles).toHaveLength(4);
expect(stagedFiles[0]).toEqual("packages/pkg-a/index.js");
expect(stagedFiles[1]).toEqual("packages/pkg-a/package.json");
expect(stagedFiles[2]).toEqual("packages/pkg-b/index.js");
expect(stagedFiles[3]).toEqual("packages/pkg-b/package.json");
expect(stagedFiles).toEqual([
"packages/package.json",
"packages/pkg-a/index.js",
"packages/pkg-a/package.json",
"packages/pkg-b/index.js",
"packages/pkg-b/package.json"
]);
});
});

Expand Down
20 changes: 11 additions & 9 deletions packages/git/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import spawn from "spawndamnit";
import path from "path";
import { getPackages } from "@manypkg/get-packages";
import { getPackages, Package } from "@manypkg/get-packages";
import { GitError } from "@changesets/errors";
import isSubdir from "is-subdir";

const isInDir = (dir: string) => (subdir: string) => isSubdir(dir, subdir);

async function add(pathToFile: string, cwd: string) {
const gitCmd = await spawn("git", ["add", pathToFile], { cwd });

Expand Down Expand Up @@ -108,17 +110,17 @@ async function getChangedPackagesSinceRef({
const changedFiles = await getChangedFilesSince({ ref, cwd, fullPath: true });
let packages = await getPackages(cwd);

const fileNameToPackage = (fileName: string) =>
packages.packages.find(pkg => isSubdir(pkg.dir, fileName))!;
const fileToPackage: Record<string, Package> = {};

const fileExistsInPackage = (fileName: string) =>
!!fileNameToPackage(fileName);
packages.packages.forEach(pkg =>
changedFiles.filter(isInDir(pkg.dir)).forEach(fileName => {
const prevPkg = fileToPackage[fileName] || { dir: "" };
if (pkg.dir.length > prevPkg.dir.length) fileToPackage[fileName] = pkg;
})
);

return (
changedFiles
// ignore deleted files
.filter(fileExistsInPackage)
.map(fileNameToPackage)
Object.values(fileToPackage)
// filter, so that we have only unique packages
.filter((pkg, idx, packages) => packages.indexOf(pkg) === idx)
);
Expand Down

0 comments on commit 89f0c49

Please sign in to comment.