fix(medusa-dev): include packages/ subdirectories in discovery (#3293)

## What

Include packages/ subdirectories in medusa-dev packages discovery.

## Why

We started to use subdirectories in the monorepo `packages/` directory in order to better organize packages.

Packages in subdirectories were invisible to `medusa-dev` and could not be copied.

## How

Rely on monorepo package.json workspace glob patterns to discover packages that can be published to the local npm registry.
This commit is contained in:
Patrick
2023-02-21 16:12:02 +00:00
committed by GitHub
parent f43e9f0f20
commit e8e7d7bb53
5 changed files with 58 additions and 28 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"medusa-dev-cli": patch
---
fix(medusa-dev): include packages/ subdirectories in discovery
+1
View File
@@ -17,6 +17,7 @@
"execa": "^4.1.0", "execa": "^4.1.0",
"find-yarn-workspace-root": "^2.0.0", "find-yarn-workspace-root": "^2.0.0",
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"glob": "^8.1.0",
"got": "^11.8.6", "got": "^11.8.6",
"is-absolute": "^1.0.0", "is-absolute": "^1.0.0",
"jest": "^25.5.4", "jest": "^25.5.4",
+42 -26
View File
@@ -7,6 +7,7 @@ const yargs = require(`yargs/yargs`)
const path = require(`path`) const path = require(`path`)
const os = require(`os`) const os = require(`os`)
const fs = require(`fs-extra`) const fs = require(`fs-extra`)
const glob = require("glob")
const watch = require(`./watch`) const watch = require(`./watch`)
const { getVersionInfo } = require(`./utils/version`) const { getVersionInfo } = require(`./utils/version`)
const { buildFFCli } = require("./feature-flags") const { buildFFCli } = require("./feature-flags")
@@ -94,35 +95,50 @@ medusa-dev --set-path-to-repo /path/to/my/cloned/version/medusa
process.exit() process.exit()
} }
// get list of directories to crawl for package declarations
const monoRepoPackagesDirs = []
try {
const monoRepoPkg = JSON.parse(
fs.readFileSync(path.join(medusaLocation, "package.json"))
)
for (const workspace of monoRepoPkg.workspaces.packages) {
if (!workspace.startsWith("packages")) {
continue
}
const workspacePackageDirs = glob.sync(workspace, {
cwd: medusaLocation.toString(),
})
monoRepoPackagesDirs.push(...workspacePackageDirs)
}
} catch (err) {
console.error(
`Unable to read and parse the workspace definition from medusa package.json`
)
process.exit(1)
}
// get list of packages from monorepo // get list of packages from monorepo
const packageNameToPath = new Map() const packageNameToPath = new Map()
const monoRepoPackages = fs const monoRepoPackages = monoRepoPackagesDirs.map((dirName) => {
.readdirSync(path.join(medusaLocation, `packages`)) try {
.map((dirName) => { const localPkg = JSON.parse(
try { fs.readFileSync(path.join(medusaLocation, dirName, `package.json`))
const localPkg = JSON.parse(
fs.readFileSync(
path.join(medusaLocation, `packages`, dirName, `package.json`)
)
)
if (localPkg?.name) {
packageNameToPath.set(
localPkg.name,
path.join(medusaLocation, `packages`, dirName)
)
return localPkg.name
}
} catch (error) {
// fallback to generic one
}
packageNameToPath.set(
dirName,
path.join(medusaLocation, `packages`, dirName)
) )
return dirName
}) if (localPkg?.name) {
packageNameToPath.set(
localPkg.name,
path.join(medusaLocation, dirName)
)
return localPkg.name
}
} catch (error) {
// fallback to generic one
}
packageNameToPath.set(dirName, path.join(medusaLocation, dirName))
return dirName
})
const localPkg = JSON.parse(fs.readFileSync(`package.json`)) const localPkg = JSON.parse(fs.readFileSync(`package.json`))
// intersect dependencies with monoRepoPackages to get list of packages that are used // intersect dependencies with monoRepoPackages to get list of packages that are used
@@ -24,9 +24,16 @@ const installPackages = async ({
// in workspaces which should preserve node_modules structure // in workspaces which should preserve node_modules structure
// (packages being mostly hoisted to top-level node_modules) // (packages being mostly hoisted to top-level node_modules)
const { stdout: yarnVersion } = await promisifiedSpawn([
`yarn`,
[`--version`],
{ stdio: `pipe` },
])
const workspaceCommand = !yarnVersion.startsWith("1") ? "list" : "info"
const { stdout } = await promisifiedSpawn([ const { stdout } = await promisifiedSpawn([
`yarn`, `yarn`,
[`workspaces`, `info`, `--json`], [`workspaces`, workspaceCommand, `--json`],
{ stdio: `pipe` }, { stdio: `pipe` },
]) ])
+2 -1
View File
@@ -19150,7 +19150,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"glob@npm:^8.0.3": "glob@npm:^8.0.3, glob@npm:^8.1.0":
version: 8.1.0 version: 8.1.0
resolution: "glob@npm:8.1.0" resolution: "glob@npm:8.1.0"
dependencies: dependencies:
@@ -25415,6 +25415,7 @@ __metadata:
execa: ^4.1.0 execa: ^4.1.0
find-yarn-workspace-root: ^2.0.0 find-yarn-workspace-root: ^2.0.0
fs-extra: ^9.0.1 fs-extra: ^9.0.1
glob: ^8.1.0
got: ^11.8.6 got: ^11.8.6
is-absolute: ^1.0.0 is-absolute: ^1.0.0
jest: ^25.5.4 jest: ^25.5.4