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 11:12:02 -05:00
committed by GitHub
parent f43e9f0f20
commit e8e7d7bb53
5 changed files with 58 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ const yargs = require(`yargs/yargs`)
const path = require(`path`)
const os = require(`os`)
const fs = require(`fs-extra`)
const glob = require("glob")
const watch = require(`./watch`)
const { getVersionInfo } = require(`./utils/version`)
const { buildFFCli } = require("./feature-flags")
@@ -94,35 +95,50 @@ medusa-dev --set-path-to-repo /path/to/my/cloned/version/medusa
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
const packageNameToPath = new Map()
const monoRepoPackages = fs
.readdirSync(path.join(medusaLocation, `packages`))
.map((dirName) => {
try {
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)
const monoRepoPackages = monoRepoPackagesDirs.map((dirName) => {
try {
const localPkg = JSON.parse(
fs.readFileSync(path.join(medusaLocation, dirName, `package.json`))
)
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`))
// intersect dependencies with monoRepoPackages to get list of packages that are used

View File

@@ -24,9 +24,16 @@ const installPackages = async ({
// in workspaces which should preserve node_modules structure
// (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([
`yarn`,
[`workspaces`, `info`, `--json`],
[`workspaces`, workspaceCommand, `--json`],
{ stdio: `pipe` },
])