Skip to content

Commit

Permalink
fix(gatsby-dev-cli): use package name not directory name (#35447)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Apr 21, 2022
1 parent 027acf8 commit 028f348
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 137 deletions.
325 changes: 222 additions & 103 deletions packages/gatsby-dev-cli/src/__tests__/watch.js

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion packages/gatsby-dev-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,34 @@ gatsby-dev --set-path-to-repo /path/to/my/cloned/version/gatsby
}

// get list of packages from monorepo
const monoRepoPackages = fs.readdirSync(path.join(gatsbyLocation, `packages`))
const packageNameToPath = new Map()
const monoRepoPackages = fs
.readdirSync(path.join(gatsbyLocation, `packages`))
.map(dirName => {
try {
const localPkg = JSON.parse(
fs.readFileSync(
path.join(gatsbyLocation, `packages`, dirName, `package.json`)
)
)

if (localPkg?.name) {
packageNameToPath.set(
localPkg.name,
path.join(gatsbyLocation, `packages`, dirName)
)
return localPkg.name
}
} catch (error) {
// fallback to generic one
}

packageNameToPath.set(
dirName,
path.join(gatsbyLocation, `packages`, dirName)
)
return dirName
})

const localPkg = JSON.parse(fs.readFileSync(`package.json`))
// intersect dependencies with monoRepoPackages to get list of packages that are used
Expand Down Expand Up @@ -120,4 +147,5 @@ watch(gatsbyLocation, argv.packages, {
scanOnce: argv.scanOnce,
forceInstall: argv.forceInstall,
monoRepoPackages,
packageNameToPath,
})
4 changes: 2 additions & 2 deletions packages/gatsby-dev-cli/src/local-npm-registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.startVerdaccio = startServer
exports.publishPackagesLocallyAndInstall = async ({
packagesToPublish,
localPackages,
root,
packageNameToPath,
ignorePackageJSONChanges,
yarnWorkspaceRoot,
}) => {
Expand All @@ -59,7 +59,7 @@ exports.publishPackagesLocallyAndInstall = async ({
newlyPublishedPackageVersions[packageName] = await publishPackage({
packageName,
packagesToPublish,
root,
packageNameToPath,
versionPostFix,
ignorePackageJSONChanges,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const adjustPackageJson = ({
versionPostFix,
packagesToPublish,
ignorePackageJSONChanges,
root,
packageNameToPath,
}) => {
// we need to check if package depend on any other package to will be published and
// adjust version selector to point to dev version of package so local registry is used
Expand All @@ -49,7 +49,7 @@ const adjustPackageJson = ({
fs.readFileSync(
getMonorepoPackageJsonPath({
packageName: packageThatWillBePublished,
root,
packageNameToPath,
}),
`utf-8`
)
Expand Down Expand Up @@ -99,19 +99,19 @@ const createTemporaryNPMRC = ({ pathToPackage }) => {
const publishPackage = async ({
packageName,
packagesToPublish,
root,
versionPostFix,
ignorePackageJSONChanges,
packageNameToPath,
}) => {
const monoRepoPackageJsonPath = getMonorepoPackageJsonPath({
packageName,
root,
packageNameToPath,
})

const { unadjustPackageJson, newPackageVersion } = adjustPackageJson({
monoRepoPackageJsonPath,
packageName,
root,
packageNameToPath,
versionPostFix,
packagesToPublish,
ignorePackageJSONChanges,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const { getDependantPackages } = require(`../get-dependant-packages`)

function createMockPackageNameToPath(packageNames) {
const packageNameToPath = new Map()

for (const packageName of packageNames) {
packageNameToPath.set(packageName, `/test/${packageName}`)
}

return packageNameToPath
}

describe(`getDependantPackages`, () => {
it(`handles deep dependency chains`, () => {
const packagesToPublish = getDependantPackages({
Expand All @@ -9,6 +19,13 @@ describe(`getDependantPackages`, () => {
"package-a-dep1-dep1": new Set([`package-a-dep1`]),
"not-related": new Set([`also-not-related`]),
},
packageNameToPath: createMockPackageNameToPath([
`package-a`,
`package-a-dep1`,
`package-a-dep1-dep1`,
`not-related`,
`also-not-related`,
]),
})

expect(packagesToPublish).toEqual(
Expand All @@ -23,6 +40,10 @@ describe(`getDependantPackages`, () => {
"package-a": new Set([`package-b`]),
"package-b": new Set([`package-a`]),
},
packageNameToPath: createMockPackageNameToPath([
`package-a`,
`package-b`,
]),
})
expect(packagesToPublish).toEqual(new Set([`package-a`, `package-b`]))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ jest.doMock(

describe(`traversePackageDeps`, () => {
it(`handles deep dependency chains`, () => {
const monoRepoPackages = [
`package-a`,
`package-a-dep1`,
`package-a-dep1-dep1`,
`package-not-used`,
]
const packageNameToPath = new Map()
for (const packageName of monoRepoPackages) {
packageNameToPath.set(
packageName,
path.join(...`<monorepo-path>/packages/${packageName}`.split(`/`))
)
}

const { seenPackages, depTree } = traversePackagesDeps({
root: `<monorepo-path>`,
packages: [`package-a`, `doesnt-exist`],
monoRepoPackages: [
`package-a`,
`package-a-dep1`,
`package-a-dep1-dep1`,
`package-not-used`,
],
monoRepoPackages,
packageNameToPath,
})

expect(seenPackages).toEqual([
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-dev-cli/src/utils/check-deps-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ exports.checkDepsChanges = async ({
newPath,
packageName,
monoRepoPackages,
root,
isInitialScan,
ignoredPackageJSON,
packageNameToPath,
}) => {
let localPKGjson
let packageNotInstalled = false
Expand Down Expand Up @@ -80,7 +80,7 @@ exports.checkDepsChanges = async ({

const monoRepoPackageJsonPath = getMonorepoPackageJsonPath({
packageName,
root,
packageNameToPath,
})
const monorepoPKGjsonString = fs.readFileSync(
monoRepoPackageJsonPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require(`path`)

exports.getMonorepoPackageJsonPath = ({ packageName, root }) =>
path.join(root, `packages`, packageName, `package.json`)
exports.getMonorepoPackageJsonPath = ({ packageName, packageNameToPath }) =>
path.join(packageNameToPath.get(packageName), `package.json`)
18 changes: 13 additions & 5 deletions packages/gatsby-dev-cli/src/utils/traverse-package-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,26 @@ const path = require(`path`)
* @return {TraversePackagesDepsReturn}
*/
const traversePackagesDeps = ({
root,
packages,
monoRepoPackages,
seenPackages = [...packages],
depTree = {},
packageNameToPath,
}) => {
packages.forEach(p => {
let pkgJson
try {
pkgJson = require(path.join(root, `packages`, p, `package.json`))
} catch {
console.error(`"${p}" package doesn't exist in monorepo.`)
const packageRoot = packageNameToPath.get(p)
if (packageRoot) {
pkgJson = require(path.join(packageRoot, `package.json`))
} else {
console.error(`"${p}" package doesn't exist in monorepo.`)
// remove from seenPackages
seenPackages = seenPackages.filter(seenPkg => seenPkg !== p)
return
}
} catch (e) {
console.error(`"${p}" package doesn't exist in monorepo.`, e)
// remove from seenPackages
seenPackages = seenPackages.filter(seenPkg => seenPkg !== p)
return
Expand All @@ -69,11 +77,11 @@ const traversePackagesDeps = ({
})

traversePackagesDeps({
root,
packages: fromMonoRepo,
monoRepoPackages,
seenPackages,
depTree,
packageNameToPath,
})
}
})
Expand Down
41 changes: 30 additions & 11 deletions packages/gatsby-dev-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ const MAX_COPY_RETRIES = 3
async function watch(
root,
packages,
{ scanOnce, quiet, forceInstall, monoRepoPackages, localPackages }
{
scanOnce,
quiet,
forceInstall,
monoRepoPackages,
localPackages,
packageNameToPath,
}
) {
setDefaultSpawnStdio(quiet ? `ignore` : `inherit`)
// determine if in yarn workspace - if in workspace, force using verdaccio
Expand Down Expand Up @@ -123,6 +130,7 @@ async function watch(
root,
packages: _.uniq(localPackages),
monoRepoPackages,
packageNameToPath,
})

const allPackagesToWatch = packages
Expand All @@ -143,7 +151,7 @@ async function watch(
if (allPackagesToWatch.length > 0) {
await publishPackagesLocallyAndInstall({
packagesToPublish: allPackagesToWatch,
root,
packageNameToPath,
localPackages,
ignorePackageJSONChanges,
yarnWorkspaceRoot,
Expand Down Expand Up @@ -186,7 +194,7 @@ async function watch(
)
const watchers = _.uniq(
allPackagesToWatch
.map(p => path.join(root, `/packages/`, p))
.map(p => path.join(packageNameToPath.get(p)))
.filter(p => fs.existsSync(p))
)

Expand All @@ -199,7 +207,7 @@ async function watch(
let anyPackageNotInstalled = false

const watchEvents = [`change`, `add`]

const packagePathMatchingEntries = Array.from(packageNameToPath.entries())
chokidar
.watch(watchers, {
ignored: [filePath => _.some(ignored, reg => reg.test(filePath))],
Expand All @@ -209,11 +217,22 @@ async function watch(
return
}

const [packageName] = filePath
.split(/packages[/\\]/)
.pop()
.split(/[/\\]/)
const prefix = path.join(root, `/packages/`, packageName)
// match against paths
let packageName

for (const [_packageName, packagePath] of packagePathMatchingEntries) {
const relativeToThisPackage = path.relative(packagePath, filePath)
if (!relativeToThisPackage.startsWith(`..`)) {
packageName = _packageName
break
}
}

if (!packageName) {
return
}

const prefix = packageNameToPath.get(packageName)

// Copy it over local version.
// Don't copy over the Gatsby bin file as that breaks the NPM symlink.
Expand Down Expand Up @@ -241,7 +260,7 @@ async function watch(
newPath,
packageName,
monoRepoPackages,
root,
packageNameToPath,
isInitialScan,
ignoredPackageJSON,
})
Expand Down Expand Up @@ -317,7 +336,7 @@ async function watch(
isPublishing = true
await publishPackagesLocallyAndInstall({
packagesToPublish: Array.from(packagesToPublish),
root,
packageNameToPath,
localPackages,
ignorePackageJSONChanges,
})
Expand Down

0 comments on commit 028f348

Please sign in to comment.