dependency-grouper 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +185 -196
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -84,227 +84,221 @@ Run `dependency-grouper generate` and it merges the groups with automatic sortin
84
84
  }
85
85
  ```
86
86
 
87
- ## Usage
87
+ ## Quick Start
88
88
 
89
- ### Quick Start (Existing Monorepo)
89
+ ### Option A: Bootstrap from Existing Monorepo (Recommended)
90
90
 
91
- Bootstrap from your existing monorepo dependencies:
91
+ If you already have a monorepo with dependencies:
92
92
 
93
93
  ```bash
94
- # 1. Install
94
+ # 1. Install at workspace root
95
95
  pnpm add -D dependency-grouper
96
96
 
97
- # 2. Add empty depGroups array to all package.json files
98
- # Root package.json:
99
- {
100
- "depGroups": [] // ← Will become ["root"]
101
- }
97
+ # 2. Add "depGroups": [] to ALL package.json files
98
+ # ├── package.json → "depGroups": []
99
+ # └── packages/
100
+ # ├── app1/package.json → "depGroups": []
101
+ # └── app2/package.json → "depGroups": []
102
102
 
103
- # Sub-package package.json files:
104
- {
105
- "depGroups": [] // ← Will become ["common"]
106
- }
107
-
108
- # 3. Run initial generate - this does EVERYTHING automatically:
103
+ # 3. Run generate (does everything automatically):
109
104
  npx dependency-grouper generate
110
105
  ```
111
106
 
112
- **What initial `generate` does:**
113
- 1. **Scans** all package.json files and collects existing dependencies
114
- 2. **Creates** `.dep-groups.yaml` with separate "root" and "common" groups
115
- 3. **Auto-populates** empty `depGroups: []` arrays:
116
- - Root package.json `["root"]`
117
- - Sub-packages → `["common"]`
118
- 4. **Merges** group dependencies back into all package.json files
119
- 5. **Injects** `"preinstall": "dependency-grouper generate"` script (or appends to existing)
120
-
121
- After this one command, everything is set up! Future `pnpm install` will auto-sync.
107
+ **What just happened:**
108
+ 1. Created `.dep-groups.yaml` with all your dependencies organized
109
+ 2. Root dependencies `root` group, sub-packages → `common` group
110
+ 3. Auto-populated `depGroups: ["root"]` or `["common"]` in all files
111
+ 4. Injected `preinstall` script for automatic sync
122
112
 
113
+ **Next steps:**
123
114
  ```bash
124
- # 4. (Optional) Review and reorganize .dep-groups.yaml
125
- nano .dep-groups.yaml
126
-
127
- # 5. Run generate again if you changed groups
115
+ # 4. Review and reorganize the generated .dep-groups.yaml
116
+ code .dep-groups.yaml
117
+
118
+ # Example: Split 'common' into specific groups:
119
+ # groups:
120
+ # common:
121
+ # dependencies:
122
+ # axios: ^1.6.0 → Move to 'shared-utils'
123
+ # react: ← New group
124
+ # dependencies:
125
+ # react: ^18.2.0
126
+ # react-dom: ^18.2.0
127
+
128
+ # 5. Update package.json files to use new groups:
129
+ # packages/react-app/package.json:
130
+ # "depGroups": ["react", "shared-utils"]
131
+
132
+ # 6. Regenerate to apply changes:
128
133
  npx dependency-grouper generate
129
- ```
130
134
 
131
- ### Fresh Setup
135
+ # 7. Install and you're done!
136
+ pnpm install
137
+ ```
132
138
 
133
- ### 1. Install
139
+ ### Option B: Fresh Setup (New Monorepo)
134
140
 
135
- ```bash
136
- # pnpm
137
- pnpm add -D dependency-grouper
141
+ Starting from scratch:
138
142
 
139
- # npm
140
- npm install -D dependency-grouper
143
+ #### 1. Install
141
144
 
142
- # yarn
143
- yarn add -D dependency-grouper
145
+ ```bash
146
+ pnpm add -D dependency-grouper # or npm / yarn
144
147
  ```
145
148
 
146
- ### 2. Create `.dep-groups.yaml` at workspace root
149
+ #### 2. Create `.dep-groups.yaml` at workspace root
150
+
151
+ Define your dependency groups:
147
152
 
148
153
  ```yaml
149
154
  groups:
150
- webpack:
155
+ react:
151
156
  dependencies:
152
- webpack: ^5.95.0
153
- webpack-cli: ^5.1.4
157
+ react: ^18.2.0
158
+ react-dom: ^18.2.0
159
+ devDependencies:
160
+ '@types/react': ^18.2.0
154
161
 
155
- vue:
162
+ shared-utils:
156
163
  dependencies:
157
- vue: ^3.5.17
164
+ axios: ^1.6.0
165
+ lodash: ^4.17.21
158
166
  ```
159
167
 
160
- ### 3. Reference groups in package.json
168
+ #### 3. Add `depGroups` to package.json files
161
169
 
162
170
  ```json
163
171
  {
164
- "name": "my-project",
165
- "depGroups": ["webpack", "vue"],
166
- "dependencies": {
167
- "my-custom-dep": "^1.0.0"
168
- }
172
+ "name": "my-react-app",
173
+ "depGroups": ["react", "shared-utils"]
169
174
  }
170
175
  ```
171
176
 
172
- ### 4. Generate dependencies
177
+ #### 4. Generate and install
173
178
 
174
179
  ```bash
175
- dependency-grouper generate
180
+ npx dependency-grouper generate
181
+ pnpm install
176
182
  ```
177
183
 
178
- ### 5. Install dependencies
184
+ ---
179
185
 
180
- ```bash
181
- # pnpm
182
- pnpm install
186
+ ## How It Works
183
187
 
184
- # npm
185
- npm install
188
+ 1. **You define groups** in `.dep-groups.yaml` (shared dependency sets)
189
+ 2. **You reference groups** in `package.json` with `"depGroups": ["group1", "group2"]`
190
+ 3. **Run `generate`** → Merges group dependencies into each package.json
191
+ 4. **Run package manager** → Installs the merged dependencies
186
192
 
187
- # yarn
188
- yarn install
189
- ```
193
+ **Bidirectional sync:**
194
+ - `.dep-groups.yaml` → `package.json` (group deps added to packages)
195
+ - `package.json` → `.dep-groups.yaml` (new deps captured in groups)
196
+
197
+ ---
198
+
199
+ ## CLI Commands
200
+
201
+ ### `dependency-grouper generate`
202
+
203
+ **Full bidirectional sync** - Use this most of the time.
204
+
205
+ 1. Scans all package.json files
206
+ 2. Updates `.dep-groups.yaml` with new dependencies
207
+ 3. Merges group dependencies back into package.json files
208
+ 4. Auto-populates empty `depGroups: []` arrays
209
+ 5. Injects preinstall scripts
210
+
211
+ **When to use:**
212
+ - Initial setup
213
+ - After reorganizing groups
214
+ - In preinstall hooks
215
+
216
+ ### `dependency-grouper sync`
217
+
218
+ **One-way sync only** - package.json → `.dep-groups.yaml`
219
+
220
+ Captures new dependencies without modifying package.json files. Faster, good for postinstall hooks.
221
+
222
+ **When to use:**
223
+ - Postinstall hooks to capture new deps
224
+ - CI/CD to keep `.dep-groups.yaml` updated
225
+
226
+ ---
227
+
228
+ ## Automation (Optional)
190
229
 
191
- ## Automatic Generation (Optional)
230
+ Add hooks to your **workspace root** `package.json` for automatic syncing:
192
231
 
193
- ### Option 1: Preinstall Hook (Full Generation)
232
+ ### Preinstall Hook (Recommended)
194
233
 
195
- To automatically generate dependencies before every install, add to your **workspace root** `package.json`:
234
+ Runs `generate` before every `pnpm install`:
196
235
 
197
236
  ```json
198
237
  {
199
- "name": "my-monorepo",
200
- "private": true,
201
238
  "scripts": {
202
239
  "preinstall": "dependency-grouper generate"
203
240
  }
204
241
  }
205
242
  ```
206
243
 
207
- ### Option 2: Postinstall Hook (Sync Only) - Recommended for Development
244
+ Always in sync
245
+ ✅ Team members don't need to remember
246
+ ❌ Slower if you install frequently
208
247
 
209
- To automatically sync new dependencies after install without regenerating all files:
248
+ ### Postinstall Hook (Lightweight Alternative)
249
+
250
+ Runs `sync` after install to capture new deps:
210
251
 
211
252
  ```json
212
253
  {
213
- "name": "my-monorepo",
214
- "private": true,
215
254
  "scripts": {
216
255
  "postinstall": "dependency-grouper sync"
217
256
  }
218
257
  }
219
258
  ```
220
259
 
221
- **How it works:**
222
- 1. You run `pnpm add axios` in any package
223
- 2. pnpm installs axios
224
- 3. postinstall hook automatically runs `dependency-grouper sync`
225
- 4. axios is captured in .dep-groups.yaml
226
- 5. Next time you/anyone runs `generate`, all packages in that group get axios
260
+ Faster
261
+ Captures new deps automatically
262
+ ⚠️ Need to run `generate` manually to share with other packages
227
263
 
228
- **Perfect for development** - captures new dependencies automatically without slowing down every install.
264
+ ---
229
265
 
230
- ### Comparison
266
+ ---
231
267
 
232
- | Hook | Command | When | Updates .dep-groups.yaml | Updates package.json |
233
- |------|---------|------|--------------------------|---------------------|
234
- | preinstall | `generate` | Before install | ✅ | ✅ |
235
- | postinstall | `sync` | After install | ✅ | ❌ |
268
+ ## Advanced Usage
236
269
 
237
- **Benefits:**
238
- - ✅ No manual `generate` command needed
239
- - ✅ Dependencies always up-to-date before install
240
- - ✅ Team members don't need to remember to run generate
270
+ ### Multiple Groups per Package
241
271
 
242
- **When to use:**
243
- - ✓ Production/CI workflows - ensures consistency
244
- - ✓ Team environments - automatic for everyone
245
- - Active development - can be slower if you install frequently
272
+ ```json
273
+ {
274
+ "name": "my-app",
275
+ "depGroups": ["react", "shared-utils", "testing"]
276
+ }
277
+ ```
246
278
 
247
- **Note:** Make sure `dependency-grouper` is installed before the preinstall hook runs. Add it as a devDependency at the workspace root.
279
+ All groups are merged together into the package.
248
280
 
249
- ## Features
281
+ ### Root vs Sub-Packages
250
282
 
251
- - **Multi-package manager** - Works with pnpm, npm, and yarn workspaces
252
- - **Define once, use everywhere** - Create dependency groups in one place
253
- - **Bidirectional sync** - Updates flow both ways automatically
254
- - ✅ **Automatic sorting** - package.json keys and dependencies alphabetically sorted
255
- - ✅ **Type-safe** - Full TypeScript support with type definitions
256
- - ✅ **Smart merging** - Preserves project-specific dependencies
257
- - ✅ **Version control** - Update versions in one place
258
- - ✅ **CI/CD ready** - Optional preinstall hook for automation
259
- - ✅ **Nested projects** - Recursively finds all package.json files in monorepo
260
- - ✅ **Bootstrap from existing** - Generate .dep-groups.yaml from current dependencies
283
+ When bootstrapping with empty `depGroups: []`:
284
+ - **Root** package.json Auto-assigned `["root"]` group
285
+ - **Sub-packages** → Auto-assigned `["common"]` group
261
286
 
262
- ## Configuration
287
+ This separates monorepo tooling (root) from app dependencies (common).
263
288
 
264
- ### .dep-groups.yaml
289
+ ### Nested Monorepos
265
290
 
266
- ```yaml
267
- groups:
268
- groupName:
269
- dependencies:
270
- package-name: version
271
- devDependencies:
272
- dev-package: version
291
+ Recursively finds ALL package.json files:
273
292
  ```
274
-
275
- ### package.json
276
-
277
- ```json
278
- {
279
- "depGroups": ["groupName1", "groupName2"]
280
- }
293
+ monorepo/
294
+ ├── packages/app1/package.json ← Found
295
+ ├── packages/nested/deep/app2/package.json ← Found
296
+ └── apps/frontend/utils/package.json ← Found
281
297
  ```
282
298
 
283
- ## How It Works
284
- Recursively finds all package.json files (including nested sub-projects)
285
- 3. Syncs new dependencies from package.json files to .dep-groups.yaml (bidirectional)
286
- 4. Finds all package.json files with `depGroups` field
287
- 5. Merges specified groups with existing dependencies
288
- 6. Sorts dependencies alphabetically within each section
289
- 7. Formats entire package.json with proper key ordering
290
- 8. Writes updated package.json files
291
- 9. Preserves project-specific dependencies
292
-
293
- **Supports nested structures:**
294
- ```
295
- monorepo/
296
- ├── .dep-groups.yaml
297
- ├── packages/
298
- │ ├── app1/package.json
299
- │ └── nested/
300
- │ └── deep/
301
- │ └── app2/package.json ← Found!
302
- └── apps/
303
- └── frontend/
304
- └── utils/package.json ← Found!
305
- ``` key ordering
306
- 7. Writes updated package.json files
307
- 8. Preserves project-specific dependencies
299
+ Skips `node_modules/` and `.git/` automatically.
300
+
301
+ ---
308
302
 
309
303
  ## Development
310
304
 
@@ -353,75 +347,70 @@ dependency-grouper sync
353
347
  - Faster, useful for postinstall hooks to capture new dependencies
354
348
  - Good for CI/CD to keep .dep-groups.yaml in sync
355
349
 
356
- ## Example Workflow
357
-
358
- ### Initial Setup
350
+ ---
359
351
 
360
- ```bash
361
- # 1. Install at workspace root
362
- pnpm add -D dependency-grouper
352
+ ## Example Workflows
363
353
 
364
- # 2. Create .dep-groups.yaml
365
- cat > .dep-groups.yaml << EOF
366
- groups:
367
- react:
368
- dependencies:
369
- react: "^18.2.0"
370
- react-dom: "^18.2.0"
371
- EOF
354
+ ### Sharing a New Dependency
372
355
 
373
- # 3. Add depGroups to a package
374
- cat > packages/my-app/package.json << EOF
375
- {
376
- "name": "my-app",
377
- "version": "1.0.0",
378
- "depGroups": ["react"]
379
- }
380
- EOF
356
+ ```bash
357
+ # 1. Add to one package
358
+ cd packages/app1
359
+ pnpm add axios
381
360
 
382
- # 4. Generate
383
- dependency-grouper generate
361
+ # 2. If you have postinstall hook, it's already in .dep-groups.yaml!
362
+ # Otherwise, run manually:
363
+ npx dependency-grouper sync
384
364
 
385
- # 5. Install
386
- pnpm install
365
+ # 3. Share with other packages:
366
+ npx dependency-grouper generate
387
367
  ```
388
368
 
389
- ### Daily Development
369
+ ### Reorganizing Groups
390
370
 
391
371
  ```bash
392
- # You manually add a package
393
- cd packages/my-app
394
- pnpm add axios
395
- # ↑ postinstall hook runs automatically!
396
- # → dependency-grouper sync
397
- # → axios is now in .dep-groups.yaml
372
+ # 1. Edit .dep-groups.yaml - split 'common' into specific groups
373
+ code .dep-groups.yaml
398
374
 
399
- # Later, share it with other packages
400
- dependency-grouper generate
401
- # → All packages with same depGroups get axios
375
+ # 2. Update package.json files with new group names
376
+ # "depGroups": ["react", "api-utils"] # was ["common"]
402
377
 
403
- # Or if you have preinstall hook, just:
404
- pnpm install
405
- # → generate runs automatically before install
378
+ # 3. Regenerate
379
+ npx dependency-grouper generate
406
380
  ```
407
381
 
408
- **With postinstall hook configured:**
382
+ ### Updating a Dependency Version
383
+
409
384
  ```bash
410
- pnpm add lodash
411
- # 1. lodash added to current package.json
412
- # 2. pnpm installs lodash
413
- # 3. postinstall runs: dependency-grouper sync
414
- # 4. lodash automatically added to .dep-groups.yaml
415
- # 5. Next time anyone runs generate, they get lodash too
416
- ```
385
+ # Just update the version in .dep-groups.yaml:
386
+ # react: ^18.2.0 → react: ^18.3.0
417
387
 
418
- ### Adding to Existing Groups
388
+ npx dependency-grouper generate
389
+ pnpm install
390
+ # All packages using the 'react' group get v18.3.0!
391
+ ```
419
392
 
420
- When you `pnpm add` a package to a project that has `depGroups`, the next time you run `generate`, it will:
421
- 1. Detect the new package in your package.json
422
- 2. Automatically add it to all groups listed in `depGroups`
423
- 3. Sync it across all packages using those groups
393
+ ---
424
394
 
425
395
  ## License
426
396
 
427
397
  MIT
398
+
399
+ ## Troubleshooting
400
+
401
+ **Q: `.dep-groups.yaml` wasn't created**
402
+ A: Add `"depGroups": []` to at least one package.json, then run `generate`.
403
+
404
+ **Q: Dependencies not merging**
405
+ A: Check that:
406
+ - `depGroups` field exists in package.json
407
+ - Group names match exactly (case-sensitive)
408
+ - You ran `generate` (not just `sync`)
409
+
410
+ **Q: How to use AI to organize dependencies?**
411
+ A:
412
+ 1. Run `npx dependency-grouper generate` → creates `.dep-groups.yaml`
413
+ 2. Give `.dep-groups.yaml` to AI: *"Reorganize into logical groups (react, testing, build-tools, etc.)"*
414
+ 3. AI rewrites with better organization
415
+ 4. Update `depGroups` in package.json files to use new group names
416
+ 5. Run `npx dependency-grouper generate` to apply
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dependency-grouper",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Group and reuse dependency sets across monorepo projects (pnpm, npm, yarn)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",