Skip to content

Commit

Permalink
fix: fix workspace deps when loading root.config.ts (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Nov 5, 2024
1 parent 2cdb8e9 commit 8022259
Show file tree
Hide file tree
Showing 20 changed files with 438 additions and 161 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cougars-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root': patch
---

fix: fix workspace deps when loading root.config.ts
5 changes: 5 additions & 0 deletions .changeset/happy-candles-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root': patch
---

chore: remove devDependencies from prod package.json
24 changes: 24 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"mode": "exit",
"tag": "debug",
"initialVersions": {
"@private/docs": "0.0.0",
"@examples/basepath": "0.0.0",
"@examples/blog": "0.0.0",
"@examples/cms": "0.0.0",
"@examples/minimal": "0.0.0",
"@examples/starter": "0.0.0",
"@blinkk/create-root": "1.3.12",
"@blinkk/eslint-config-root": "0.1.0",
"@blinkk/rds": "1.3.12",
"@blinkk/root": "1.3.12",
"@blinkk/root-cms": "1.3.12",
"@blinkk/root-password-protect": "1.3.12"
},
"changesets": [
"fuzzy-cougars-unite",
"happy-candles-sniff",
"small-dryers-grow",
"wise-eyes-grab"
]
}
8 changes: 8 additions & 0 deletions packages/create-root/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @blinkk/create-root

## 1.3.13-debug.3

## 1.3.13-debug.2

## 1.3.13-debug.1

## 1.3.13-debug.0

## 1.3.12

## 1.3.11
Expand Down
2 changes: 1 addition & 1 deletion packages/create-root/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blinkk/create-root",
"version": "1.3.12",
"version": "1.3.13-debug.3",
"description": "",
"author": "[email protected]",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-root/src/root-version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is autogenerated by update-root-version.mjs.
export const ROOT_VERSION = '1.3.2';
export const ROOT_VERSION = '1.3.13-debug.3';
28 changes: 28 additions & 0 deletions packages/rds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# @blinkk/rds

## 1.3.13-debug.3

### Patch Changes

- Updated dependencies [cf9b8c2]
- @blinkk/root@1.3.13-debug.3

## 1.3.13-debug.2

### Patch Changes

- Updated dependencies [30adc8c]
- @blinkk/root@1.3.13-debug.2

## 1.3.13-debug.1

### Patch Changes

- Updated dependencies [edada31]
- @blinkk/root@1.3.13-debug.1

## 1.3.13-debug.0

### Patch Changes

- Updated dependencies [84ff15d]
- @blinkk/root@1.3.13-debug.0

## 1.3.12

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/rds/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@blinkk/rds",
"description": "Root.js Design System",
"version": "1.3.12",
"version": "1.3.13-debug.3",
"author": "[email protected]",
"license": "MIT",
"engines": {
Expand Down Expand Up @@ -32,7 +32,7 @@
"typescript": "^5.0.4"
},
"peerDependencies": {
"@blinkk/root": "1.3.12",
"@blinkk/root": "1.3.13-debug.3",
"preact": "*"
},
"dependencies": {
Expand Down
28 changes: 28 additions & 0 deletions packages/root-cms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# @blinkk/root-cms

## 1.3.13-debug.3

### Patch Changes

- Updated dependencies [cf9b8c2]
- @blinkk/root@1.3.13-debug.3

## 1.3.13-debug.2

### Patch Changes

- Updated dependencies [30adc8c]
- @blinkk/root@1.3.13-debug.2

## 1.3.13-debug.1

### Patch Changes

- Updated dependencies [edada31]
- @blinkk/root@1.3.13-debug.1

## 1.3.13-debug.0

### Patch Changes

- Updated dependencies [84ff15d]
- @blinkk/root@1.3.13-debug.0

## 1.3.12

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/root-cms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blinkk/root-cms",
"version": "1.3.12",
"version": "1.3.13-debug.3",
"author": "[email protected]",
"license": "MIT",
"engines": {
Expand Down Expand Up @@ -131,7 +131,7 @@
"vitest": "0.34.6"
},
"peerDependencies": {
"@blinkk/root": "1.3.12",
"@blinkk/root": "1.3.13-debug.3",
"firebase-admin": ">=11",
"firebase-functions": ">=4",
"preact": ">=10",
Expand Down
28 changes: 28 additions & 0 deletions packages/root-password-protect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# @blinkk/root-password-protect

## 1.3.13-debug.3

### Patch Changes

- Updated dependencies [cf9b8c2]
- @blinkk/root@1.3.13-debug.3

## 1.3.13-debug.2

### Patch Changes

- Updated dependencies [30adc8c]
- @blinkk/root@1.3.13-debug.2

## 1.3.13-debug.1

### Patch Changes

- Updated dependencies [edada31]
- @blinkk/root@1.3.13-debug.1

## 1.3.13-debug.0

### Patch Changes

- Updated dependencies [84ff15d]
- @blinkk/root@1.3.13-debug.0

## 1.3.12

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/root-password-protect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blinkk/root-password-protect",
"version": "1.3.12",
"version": "1.3.13-debug.3",
"author": "[email protected]",
"license": "MIT",
"engines": {
Expand Down Expand Up @@ -42,7 +42,7 @@
"micromatch": "4.0.5"
},
"peerDependencies": {
"@blinkk/root": "1.3.12",
"@blinkk/root": "1.3.13-debug.3",
"preact": ">=10",
"preact-render-to-string": ">=5"
},
Expand Down
24 changes: 24 additions & 0 deletions packages/root/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# @blinkk/root

## 1.3.13-debug.3

### Patch Changes

- cf9b8c2: chore: add debug statements

## 1.3.13-debug.2

### Patch Changes

- 30adc8c: chore: remove devDependencies from prod package.json

## 1.3.13-debug.1

### Patch Changes

- edada31: chore: update wildcard deps from top-level package

## 1.3.13-debug.0

### Patch Changes

- 84ff15d: fix: fix workspace deps when loading root.config.ts

## 1.3.12

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/root/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blinkk/root",
"version": "1.3.12",
"version": "1.3.13-debug.3",
"author": "[email protected]",
"license": "MIT",
"engines": {
Expand Down Expand Up @@ -78,7 +78,8 @@
"sirv": "2.0.3",
"source-map-support": "0.5.21",
"tiny-glob": "0.2.9",
"vite": "5.0.8"
"vite": "5.0.8",
"workspace-tools": "0.37.0"
},
"peerDependencies": {
"firebase-admin": ">=11",
Expand Down
96 changes: 13 additions & 83 deletions packages/root/src/cli/create-package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {promises as fs} from 'node:fs';
import path from 'node:path';
import {build as esbuild} from 'esbuild';
import {flattenPackageDepsFromMonorepo} from '../node/monorepo.js';
import {
copyDir,
dirExists,
Expand Down Expand Up @@ -114,94 +115,23 @@ async function generatePackageJson(rootDir: string): Promise<PackageJson> {
path.resolve(rootDir, 'package.json')
);

// If the package.json has any peer deps, check the workspace root for any
// matching versions and move the peer deps to prod deps.
if (packageJson.peerDependencies) {
await updatePeerDepsFromWorkspace(rootDir, packageJson);
}

return packageJson;
}

async function updatePeerDepsFromWorkspace(
rootDir: string,
packageJson: PackageJson
) {
const requiredPeerDeps = getRequiredPeerDeps(packageJson);
if (requiredPeerDeps.length === 0) {
return;
}

const workspaceRoot = await findWorkspaceRoot(rootDir);
if (!workspaceRoot) {
return;
}

const workspacePackageJsonPath = path.resolve(workspaceRoot, 'package.json');
const workspacePackageJson = await loadJson<PackageJson>(
workspacePackageJsonPath
);
const workspaceDeps = workspacePackageJson.dependencies || {};

function setDep(name: string, value: string) {
if (!packageJson.dependencies) {
packageJson.dependencies = {};
}
packageJson.dependencies[name] = value;
}

for (const peerDep of requiredPeerDeps) {
const workspaceDepVersion = workspaceDeps[peerDep];
if (workspaceDepVersion) {
setDep(peerDep, workspaceDepVersion);
} else {
console.warn(
`could not find peer dep "${peerDep}" in workspace "${workspacePackageJsonPath}"`
);
// Flatten any deps from the monorepo, and remove peerDependencies and
// devDependencies.
const allDeps = flattenPackageDepsFromMonorepo(rootDir);
packageJson.dependencies ??= {};
for (const depName in allDeps) {
if (!packageJson.dependencies[depName]) {
packageJson.dependencies[depName] = allDeps[depName];
}
}
}

async function findWorkspaceRoot(rootDir: string): Promise<string | null> {
const parentDir = path.dirname(rootDir);

if (parentDir === '/') {
return null;
}

// PNPM uses `pnpm-workspace.yaml` for its workspace root.
const pnpmWorkspaceFile = path.resolve(parentDir, 'pnpm-workspace.yaml');
if (await fileExists(pnpmWorkspaceFile)) {
return parentDir;
if (packageJson.peerDependencies) {
delete packageJson.peerDependencies;
}

// YARN uses the "workspaces" key in package.json.
const packageJsonPath = path.resolve(parentDir, 'package.json');
if (await fileExists(packageJsonPath)) {
const parentPackageJson = await loadJson<PackageJson>(packageJsonPath);
if (parentPackageJson && parentPackageJson.workspaces) {
return parentDir;
}
if (packageJson.devDependencies) {
delete packageJson.devDependencies;
}

return findWorkspaceRoot(parentDir);
}

/**
* Returns all the `peerDependencies` that are not marked as
* `{"optional": true}` in `peerDependenciesMeta`.
*/
function getRequiredPeerDeps(packageJson: PackageJson) {
const requiredPeerDeps: string[] = [];
const peerDeps = packageJson.peerDependencies || {};
const peerDepsMeta = packageJson.peerDependenciesMeta || {};
for (const key in peerDeps) {
const meta = peerDepsMeta[key];
if (!meta?.optional) {
requiredPeerDeps.push(key);
}
}
return requiredPeerDeps;
return packageJson;
}

/**
Expand Down
Loading

0 comments on commit 8022259

Please sign in to comment.