Skip to content

Commit 028f348

Browse files
authored
fix(gatsby-dev-cli): use package name not directory name (#35447)
1 parent 027acf8 commit 028f348

File tree

10 files changed

+342
-137
lines changed

10 files changed

+342
-137
lines changed

packages/gatsby-dev-cli/src/__tests__/watch.js

+222-103
Large diffs are not rendered by default.

packages/gatsby-dev-cli/src/index.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,34 @@ gatsby-dev --set-path-to-repo /path/to/my/cloned/version/gatsby
8282
}
8383

8484
// get list of packages from monorepo
85-
const monoRepoPackages = fs.readdirSync(path.join(gatsbyLocation, `packages`))
85+
const packageNameToPath = new Map()
86+
const monoRepoPackages = fs
87+
.readdirSync(path.join(gatsbyLocation, `packages`))
88+
.map(dirName => {
89+
try {
90+
const localPkg = JSON.parse(
91+
fs.readFileSync(
92+
path.join(gatsbyLocation, `packages`, dirName, `package.json`)
93+
)
94+
)
95+
96+
if (localPkg?.name) {
97+
packageNameToPath.set(
98+
localPkg.name,
99+
path.join(gatsbyLocation, `packages`, dirName)
100+
)
101+
return localPkg.name
102+
}
103+
} catch (error) {
104+
// fallback to generic one
105+
}
106+
107+
packageNameToPath.set(
108+
dirName,
109+
path.join(gatsbyLocation, `packages`, dirName)
110+
)
111+
return dirName
112+
})
86113

87114
const localPkg = JSON.parse(fs.readFileSync(`package.json`))
88115
// intersect dependencies with monoRepoPackages to get list of packages that are used
@@ -120,4 +147,5 @@ watch(gatsbyLocation, argv.packages, {
120147
scanOnce: argv.scanOnce,
121148
forceInstall: argv.forceInstall,
122149
monoRepoPackages,
150+
packageNameToPath,
123151
})

packages/gatsby-dev-cli/src/local-npm-registry/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports.startVerdaccio = startServer
4545
exports.publishPackagesLocallyAndInstall = async ({
4646
packagesToPublish,
4747
localPackages,
48-
root,
48+
packageNameToPath,
4949
ignorePackageJSONChanges,
5050
yarnWorkspaceRoot,
5151
}) => {
@@ -59,7 +59,7 @@ exports.publishPackagesLocallyAndInstall = async ({
5959
newlyPublishedPackageVersions[packageName] = await publishPackage({
6060
packageName,
6161
packagesToPublish,
62-
root,
62+
packageNameToPath,
6363
versionPostFix,
6464
ignorePackageJSONChanges,
6565
})

packages/gatsby-dev-cli/src/local-npm-registry/publish-package.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const adjustPackageJson = ({
2727
versionPostFix,
2828
packagesToPublish,
2929
ignorePackageJSONChanges,
30-
root,
30+
packageNameToPath,
3131
}) => {
3232
// we need to check if package depend on any other package to will be published and
3333
// adjust version selector to point to dev version of package so local registry is used
@@ -49,7 +49,7 @@ const adjustPackageJson = ({
4949
fs.readFileSync(
5050
getMonorepoPackageJsonPath({
5151
packageName: packageThatWillBePublished,
52-
root,
52+
packageNameToPath,
5353
}),
5454
`utf-8`
5555
)
@@ -99,19 +99,19 @@ const createTemporaryNPMRC = ({ pathToPackage }) => {
9999
const publishPackage = async ({
100100
packageName,
101101
packagesToPublish,
102-
root,
103102
versionPostFix,
104103
ignorePackageJSONChanges,
104+
packageNameToPath,
105105
}) => {
106106
const monoRepoPackageJsonPath = getMonorepoPackageJsonPath({
107107
packageName,
108-
root,
108+
packageNameToPath,
109109
})
110110

111111
const { unadjustPackageJson, newPackageVersion } = adjustPackageJson({
112112
monoRepoPackageJsonPath,
113113
packageName,
114-
root,
114+
packageNameToPath,
115115
versionPostFix,
116116
packagesToPublish,
117117
ignorePackageJSONChanges,

packages/gatsby-dev-cli/src/utils/__tests__/get-dependant-packages.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
const { getDependantPackages } = require(`../get-dependant-packages`)
22

3+
function createMockPackageNameToPath(packageNames) {
4+
const packageNameToPath = new Map()
5+
6+
for (const packageName of packageNames) {
7+
packageNameToPath.set(packageName, `/test/${packageName}`)
8+
}
9+
10+
return packageNameToPath
11+
}
12+
313
describe(`getDependantPackages`, () => {
414
it(`handles deep dependency chains`, () => {
515
const packagesToPublish = getDependantPackages({
@@ -9,6 +19,13 @@ describe(`getDependantPackages`, () => {
919
"package-a-dep1-dep1": new Set([`package-a-dep1`]),
1020
"not-related": new Set([`also-not-related`]),
1121
},
22+
packageNameToPath: createMockPackageNameToPath([
23+
`package-a`,
24+
`package-a-dep1`,
25+
`package-a-dep1-dep1`,
26+
`not-related`,
27+
`also-not-related`,
28+
]),
1229
})
1330

1431
expect(packagesToPublish).toEqual(
@@ -23,6 +40,10 @@ describe(`getDependantPackages`, () => {
2340
"package-a": new Set([`package-b`]),
2441
"package-b": new Set([`package-a`]),
2542
},
43+
packageNameToPath: createMockPackageNameToPath([
44+
`package-a`,
45+
`package-b`,
46+
]),
2647
})
2748
expect(packagesToPublish).toEqual(new Set([`package-a`, `package-b`]))
2849
})

packages/gatsby-dev-cli/src/utils/__tests__/traverse-package-deps.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,25 @@ jest.doMock(
4343

4444
describe(`traversePackageDeps`, () => {
4545
it(`handles deep dependency chains`, () => {
46+
const monoRepoPackages = [
47+
`package-a`,
48+
`package-a-dep1`,
49+
`package-a-dep1-dep1`,
50+
`package-not-used`,
51+
]
52+
const packageNameToPath = new Map()
53+
for (const packageName of monoRepoPackages) {
54+
packageNameToPath.set(
55+
packageName,
56+
path.join(...`<monorepo-path>/packages/${packageName}`.split(`/`))
57+
)
58+
}
59+
4660
const { seenPackages, depTree } = traversePackagesDeps({
4761
root: `<monorepo-path>`,
4862
packages: [`package-a`, `doesnt-exist`],
49-
monoRepoPackages: [
50-
`package-a`,
51-
`package-a-dep1`,
52-
`package-a-dep1-dep1`,
53-
`package-not-used`,
54-
],
63+
monoRepoPackages,
64+
packageNameToPath,
5565
})
5666

5767
expect(seenPackages).toEqual([

packages/gatsby-dev-cli/src/utils/check-deps-changes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ exports.checkDepsChanges = async ({
3131
newPath,
3232
packageName,
3333
monoRepoPackages,
34-
root,
3534
isInitialScan,
3635
ignoredPackageJSON,
36+
packageNameToPath,
3737
}) => {
3838
let localPKGjson
3939
let packageNotInstalled = false
@@ -80,7 +80,7 @@ exports.checkDepsChanges = async ({
8080

8181
const monoRepoPackageJsonPath = getMonorepoPackageJsonPath({
8282
packageName,
83-
root,
83+
packageNameToPath,
8484
})
8585
const monorepoPKGjsonString = fs.readFileSync(
8686
monoRepoPackageJsonPath,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const path = require(`path`)
22

3-
exports.getMonorepoPackageJsonPath = ({ packageName, root }) =>
4-
path.join(root, `packages`, packageName, `package.json`)
3+
exports.getMonorepoPackageJsonPath = ({ packageName, packageNameToPath }) =>
4+
path.join(packageNameToPath.get(packageName), `package.json`)

packages/gatsby-dev-cli/src/utils/traverse-package-deps.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,26 @@ const path = require(`path`)
3434
* @return {TraversePackagesDepsReturn}
3535
*/
3636
const traversePackagesDeps = ({
37-
root,
3837
packages,
3938
monoRepoPackages,
4039
seenPackages = [...packages],
4140
depTree = {},
41+
packageNameToPath,
4242
}) => {
4343
packages.forEach(p => {
4444
let pkgJson
4545
try {
46-
pkgJson = require(path.join(root, `packages`, p, `package.json`))
47-
} catch {
48-
console.error(`"${p}" package doesn't exist in monorepo.`)
46+
const packageRoot = packageNameToPath.get(p)
47+
if (packageRoot) {
48+
pkgJson = require(path.join(packageRoot, `package.json`))
49+
} else {
50+
console.error(`"${p}" package doesn't exist in monorepo.`)
51+
// remove from seenPackages
52+
seenPackages = seenPackages.filter(seenPkg => seenPkg !== p)
53+
return
54+
}
55+
} catch (e) {
56+
console.error(`"${p}" package doesn't exist in monorepo.`, e)
4957
// remove from seenPackages
5058
seenPackages = seenPackages.filter(seenPkg => seenPkg !== p)
5159
return
@@ -69,11 +77,11 @@ const traversePackagesDeps = ({
6977
})
7078

7179
traversePackagesDeps({
72-
root,
7380
packages: fromMonoRepo,
7481
monoRepoPackages,
7582
seenPackages,
7683
depTree,
84+
packageNameToPath,
7785
})
7886
}
7987
})

packages/gatsby-dev-cli/src/watch.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ const MAX_COPY_RETRIES = 3
3030
async function watch(
3131
root,
3232
packages,
33-
{ scanOnce, quiet, forceInstall, monoRepoPackages, localPackages }
33+
{
34+
scanOnce,
35+
quiet,
36+
forceInstall,
37+
monoRepoPackages,
38+
localPackages,
39+
packageNameToPath,
40+
}
3441
) {
3542
setDefaultSpawnStdio(quiet ? `ignore` : `inherit`)
3643
// determine if in yarn workspace - if in workspace, force using verdaccio
@@ -123,6 +130,7 @@ async function watch(
123130
root,
124131
packages: _.uniq(localPackages),
125132
monoRepoPackages,
133+
packageNameToPath,
126134
})
127135

128136
const allPackagesToWatch = packages
@@ -143,7 +151,7 @@ async function watch(
143151
if (allPackagesToWatch.length > 0) {
144152
await publishPackagesLocallyAndInstall({
145153
packagesToPublish: allPackagesToWatch,
146-
root,
154+
packageNameToPath,
147155
localPackages,
148156
ignorePackageJSONChanges,
149157
yarnWorkspaceRoot,
@@ -186,7 +194,7 @@ async function watch(
186194
)
187195
const watchers = _.uniq(
188196
allPackagesToWatch
189-
.map(p => path.join(root, `/packages/`, p))
197+
.map(p => path.join(packageNameToPath.get(p)))
190198
.filter(p => fs.existsSync(p))
191199
)
192200

@@ -199,7 +207,7 @@ async function watch(
199207
let anyPackageNotInstalled = false
200208

201209
const watchEvents = [`change`, `add`]
202-
210+
const packagePathMatchingEntries = Array.from(packageNameToPath.entries())
203211
chokidar
204212
.watch(watchers, {
205213
ignored: [filePath => _.some(ignored, reg => reg.test(filePath))],
@@ -209,11 +217,22 @@ async function watch(
209217
return
210218
}
211219

212-
const [packageName] = filePath
213-
.split(/packages[/\\]/)
214-
.pop()
215-
.split(/[/\\]/)
216-
const prefix = path.join(root, `/packages/`, packageName)
220+
// match against paths
221+
let packageName
222+
223+
for (const [_packageName, packagePath] of packagePathMatchingEntries) {
224+
const relativeToThisPackage = path.relative(packagePath, filePath)
225+
if (!relativeToThisPackage.startsWith(`..`)) {
226+
packageName = _packageName
227+
break
228+
}
229+
}
230+
231+
if (!packageName) {
232+
return
233+
}
234+
235+
const prefix = packageNameToPath.get(packageName)
217236

218237
// Copy it over local version.
219238
// Don't copy over the Gatsby bin file as that breaks the NPM symlink.
@@ -241,7 +260,7 @@ async function watch(
241260
newPath,
242261
packageName,
243262
monoRepoPackages,
244-
root,
263+
packageNameToPath,
245264
isInitialScan,
246265
ignoredPackageJSON,
247266
})
@@ -317,7 +336,7 @@ async function watch(
317336
isPublishing = true
318337
await publishPackagesLocallyAndInstall({
319338
packagesToPublish: Array.from(packagesToPublish),
320-
root,
339+
packageNameToPath,
321340
localPackages,
322341
ignorePackageJSONChanges,
323342
})

0 commit comments

Comments
 (0)