workspace-utils 1.0.0

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 (49) hide show
  1. package/.github/workflows/mdbook.yml +64 -0
  2. package/.prettierignore +22 -0
  3. package/.prettierrc +13 -0
  4. package/LICENSE +21 -0
  5. package/README.md +278 -0
  6. package/docs/book.toml +10 -0
  7. package/docs/src/SUMMARY.md +24 -0
  8. package/docs/src/commands/build.md +110 -0
  9. package/docs/src/commands/dev.md +118 -0
  10. package/docs/src/commands/overview.md +239 -0
  11. package/docs/src/commands/run.md +153 -0
  12. package/docs/src/configuration.md +249 -0
  13. package/docs/src/examples.md +567 -0
  14. package/docs/src/installation.md +148 -0
  15. package/docs/src/introduction.md +117 -0
  16. package/docs/src/quick-start.md +278 -0
  17. package/docs/src/troubleshooting.md +533 -0
  18. package/index.ts +84 -0
  19. package/package.json +54 -0
  20. package/src/commands/build.ts +158 -0
  21. package/src/commands/dev.ts +192 -0
  22. package/src/commands/run.test.ts +329 -0
  23. package/src/commands/run.ts +118 -0
  24. package/src/core/dependency-graph.ts +262 -0
  25. package/src/core/process-runner.ts +355 -0
  26. package/src/core/workspace.test.ts +404 -0
  27. package/src/core/workspace.ts +228 -0
  28. package/src/package-managers/bun.test.ts +209 -0
  29. package/src/package-managers/bun.ts +79 -0
  30. package/src/package-managers/detector.test.ts +199 -0
  31. package/src/package-managers/detector.ts +111 -0
  32. package/src/package-managers/index.ts +10 -0
  33. package/src/package-managers/npm.ts +79 -0
  34. package/src/package-managers/pnpm.ts +101 -0
  35. package/src/package-managers/types.ts +42 -0
  36. package/src/utils/output.ts +301 -0
  37. package/src/utils/package-utils.ts +243 -0
  38. package/tests/bun-workspace/apps/web-app/package.json +18 -0
  39. package/tests/bun-workspace/bun.lockb +0 -0
  40. package/tests/bun-workspace/package.json +18 -0
  41. package/tests/bun-workspace/packages/shared-utils/package.json +15 -0
  42. package/tests/bun-workspace/packages/ui-components/package.json +17 -0
  43. package/tests/npm-workspace/package-lock.json +0 -0
  44. package/tests/npm-workspace/package.json +18 -0
  45. package/tests/npm-workspace/packages/core/package.json +15 -0
  46. package/tests/pnpm-workspace/package.json +14 -0
  47. package/tests/pnpm-workspace/packages/utils/package.json +15 -0
  48. package/tests/pnpm-workspace/pnpm-workspace.yaml +3 -0
  49. package/tsconfig.json +29 -0
@@ -0,0 +1,533 @@
1
+ # Troubleshooting
2
+
3
+ This page covers common issues you might encounter when using workspace-utils and how to resolve them.
4
+
5
+ ## Installation Issues
6
+
7
+ ### Package Not Found
8
+
9
+ If you get errors about workspace-utils not being found:
10
+
11
+ ```bash
12
+ # Check if it's installed
13
+ npm ls workspace-utils
14
+
15
+ # If not installed, add it as a dev dependency
16
+ npm install --save-dev workspace-utils
17
+
18
+ # Verify it's in your package.json
19
+ cat package.json | grep workspace-utils
20
+ ```
21
+
22
+ ### Permission Errors
23
+
24
+ If you encounter permission errors:
25
+
26
+ ```bash
27
+ # Clear npm cache
28
+ npm cache clean --force
29
+
30
+ # Remove node_modules and reinstall
31
+ rm -rf node_modules package-lock.json
32
+ npm install
33
+ ```
34
+
35
+ ### Version Conflicts
36
+
37
+ If you have conflicting versions:
38
+
39
+ ```bash
40
+ # Check installed version
41
+ npm ls workspace-utils
42
+
43
+ # Update to latest version
44
+ npm install --save-dev workspace-utils@latest
45
+ ```
46
+
47
+ ## Workspace Detection Issues
48
+
49
+ ### No Workspace Found
50
+
51
+ If workspace-utils can't detect your workspace:
52
+
53
+ **Error:** `No workspace configuration found`
54
+
55
+ **Solution:** Ensure you have one of these files in your root:
56
+
57
+ - `pnpm-workspace.yaml` (for pnpm)
58
+ - `package.json` with `workspaces` field (for npm/Bun)
59
+
60
+ **Example for pnpm:**
61
+
62
+ ```yaml
63
+ # pnpm-workspace.yaml
64
+ packages:
65
+ - 'packages/*'
66
+ - 'apps/*'
67
+ ```
68
+
69
+ **Example for npm/Bun:**
70
+
71
+ ```json
72
+ {
73
+ "workspaces": ["packages/*", "apps/*"]
74
+ }
75
+ ```
76
+
77
+ ### Mixed Package Managers
78
+
79
+ If you have multiple lock files:
80
+
81
+ **Error:** `Multiple package managers detected`
82
+
83
+ **Solution:** Remove conflicting lock files:
84
+
85
+ ```bash
86
+ # Keep only one lock file type
87
+ rm package-lock.json yarn.lock # Keep pnpm-lock.yaml
88
+ # OR
89
+ rm pnpm-lock.yaml yarn.lock # Keep package-lock.json
90
+ # OR
91
+ rm pnpm-lock.yaml package-lock.json # Keep yarn.lock
92
+ ```
93
+
94
+ ### Wrong Package Manager Detected
95
+
96
+ If the wrong package manager is detected:
97
+
98
+ **Check detection logic:**
99
+
100
+ 1. Bun: `bun.lockb` exists
101
+ 2. pnpm: `pnpm-lock.yaml` or `pnpm-workspace.yaml` exists
102
+ 3. npm: `package-lock.json` exists
103
+
104
+ **Solution:** Remove unwanted lock files or use the correct package manager for your setup.
105
+
106
+ ## Script Execution Issues
107
+
108
+ ### No Packages Found with Script
109
+
110
+ If you get `No packages found with the "script-name" script`:
111
+
112
+ **Check which packages have the script:**
113
+
114
+ ```bash
115
+ # Manually check package.json files
116
+ find . -name "package.json" -not -path "./node_modules/*" -exec grep -l "script-name" {} \;
117
+ ```
118
+
119
+ **Solution:** Add the script to package.json files where needed:
120
+
121
+ ```json
122
+ {
123
+ "scripts": {
124
+ "test": "jest",
125
+ "build": "tsc",
126
+ "dev": "vite dev"
127
+ }
128
+ }
129
+ ```
130
+
131
+ ### Scripts Failing
132
+
133
+ If individual package scripts fail:
134
+
135
+ **Debug a specific package:**
136
+
137
+ ```bash
138
+ # Navigate to the failing package
139
+ cd packages/failing-package
140
+
141
+ # Run the script directly
142
+ npm run script-name
143
+ ```
144
+
145
+ **Common issues:**
146
+
147
+ - Missing dependencies: `npm install`
148
+ - TypeScript errors: Check `tsconfig.json`
149
+ - Build output conflicts: `npm run clean` first
150
+
151
+ ### Permission Denied Errors
152
+
153
+ If you get permission errors running scripts:
154
+
155
+ ```bash
156
+ # On macOS/Linux, ensure scripts are executable
157
+ find . -name "*.sh" -exec chmod +x {} \;
158
+
159
+ # Check if using the right Node.js version
160
+ node --version
161
+ npm --version
162
+ ```
163
+
164
+ ## Build Issues
165
+
166
+ ### Dependency Cycle Detected
167
+
168
+ If you get circular dependency errors:
169
+
170
+ **Error:** `Dependency cycle detected: package-a -> package-b -> package-a`
171
+
172
+ **Solution:** Review and break circular dependencies:
173
+
174
+ 1. Check `dependencies` and `devDependencies` in package.json files
175
+ 2. Consider extracting shared code to a separate package
176
+ 3. Use `peerDependencies` where appropriate
177
+
178
+ **Debug dependency graph:**
179
+
180
+ ```bash
181
+ # Create a simple script to visualize dependencies
182
+ # deps.js
183
+ const fs = require('fs');
184
+ const path = require('path');
185
+
186
+ // Find all package.json files and show dependencies
187
+ // ... (custom script to analyze dependencies)
188
+ ```
189
+
190
+ ### Build Order Issues
191
+
192
+ If builds fail due to missing dependencies:
193
+
194
+ **Use `wsu build` instead of `wsu run build`:**
195
+
196
+ ```json
197
+ {
198
+ "scripts": {
199
+ "build": "wsu build", // ✅ Dependency-aware
200
+ "build:parallel": "wsu run build" // ❌ May fail
201
+ }
202
+ }
203
+ ```
204
+
205
+ ### Memory Issues During Build
206
+
207
+ If builds run out of memory:
208
+
209
+ ```json
210
+ {
211
+ "scripts": {
212
+ "build": "wsu build --concurrency 1",
213
+ "build:heavy": "NODE_OPTIONS='--max-old-space-size=4096' wsu build --concurrency 2"
214
+ }
215
+ }
216
+ ```
217
+
218
+ ## Development Server Issues
219
+
220
+ ### Ports Already in Use
221
+
222
+ If dev servers can't start due to port conflicts:
223
+
224
+ **Check what's using ports:**
225
+
226
+ ```bash
227
+ # On macOS/Linux
228
+ lsof -i :3000
229
+ lsof -i :4000
230
+
231
+ # On Windows
232
+ netstat -ano | findstr :3000
233
+ ```
234
+
235
+ **Solution:** Kill conflicting processes or change ports in package.json:
236
+
237
+ ```json
238
+ {
239
+ "scripts": {
240
+ "dev": "vite dev --port 3001"
241
+ }
242
+ }
243
+ ```
244
+
245
+ ### Dev Servers Not Starting
246
+
247
+ If development servers fail to start:
248
+
249
+ **Check individual packages:**
250
+
251
+ ```bash
252
+ cd packages/problematic-package
253
+ npm run dev
254
+ ```
255
+
256
+ **Common issues:**
257
+
258
+ - Missing dev dependencies
259
+ - Incorrect script configuration
260
+ - Environment variable issues
261
+
262
+ ### Hot Reload Not Working
263
+
264
+ If hot reload isn't working across packages:
265
+
266
+ **Ensure proper file watching:**
267
+
268
+ - Check if files are being watched correctly
269
+ - Verify symlinks are set up properly
270
+ - Check if your bundler supports monorepo setups
271
+
272
+ ## Performance Issues
273
+
274
+ ### Slow Execution
275
+
276
+ If commands are running slowly:
277
+
278
+ **Reduce concurrency:**
279
+
280
+ ```json
281
+ {
282
+ "scripts": {
283
+ "test:slow": "wsu run test --concurrency 2",
284
+ "build:slow": "wsu build --concurrency 1"
285
+ }
286
+ }
287
+ ```
288
+
289
+ **Use filtering to run fewer packages:**
290
+
291
+ ```json
292
+ {
293
+ "scripts": {
294
+ "test:changed": "wsu run test --filter 'packages/changed-*'",
295
+ "dev:minimal": "wsu dev --filter 'apps/main-app'"
296
+ }
297
+ }
298
+ ```
299
+
300
+ ### High Memory Usage
301
+
302
+ If workspace-utils uses too much memory:
303
+
304
+ ```bash
305
+ # Monitor memory usage
306
+ top -p $(pgrep -f workspace-utils)
307
+
308
+ # Reduce concurrency
309
+ # Add to package.json scripts
310
+ "build:low-mem": "wsu build --concurrency 1"
311
+ ```
312
+
313
+ ## Character Encoding Issues
314
+
315
+ ### Garbled Characters in Output
316
+
317
+ If you see strange characters instead of emojis:
318
+
319
+ **Examples of issues:**
320
+
321
+ ```
322
+ â Completed in 54428ms # Should be ✅ or [OK]
323
+ â±ï¸ Total build time # Should be ⏱️ or [TIME]
324
+ â ï¸ Warning message # Should be ⚠️ or [WARN]
325
+ ```
326
+
327
+ **Quick Solutions:**
328
+
329
+ **Option 1: Force ASCII mode (recommended)**
330
+
331
+ ```json
332
+ {
333
+ "scripts": {
334
+ "build": "wsu build --ascii",
335
+ "test": "wsu run test --ascii",
336
+ "dev": "wsu dev --ascii"
337
+ }
338
+ }
339
+ ```
340
+
341
+ **Option 2: Set environment variable**
342
+
343
+ ```bash
344
+ # In your shell
345
+ export WSU_ASCII=1
346
+
347
+ # Or in package.json
348
+ "build": "WSU_ASCII=1 wsu build"
349
+ ```
350
+
351
+ **Option 3: Terminal fixes**
352
+ If you prefer Unicode characters:
353
+
354
+ 1. Update your terminal to a modern version
355
+ 2. Set proper environment variables:
356
+ ```bash
357
+ export LANG=en_US.UTF-8
358
+ export LC_ALL=en_US.UTF-8
359
+ ```
360
+ 3. Force Unicode mode:
361
+ ```bash
362
+ export WSU_UNICODE=1
363
+ ```
364
+
365
+ **Debug what's being detected:**
366
+
367
+ ```bash
368
+ # Check your environment
369
+ node -e "console.log(process.platform, process.env.TERM, process.env.TERM_PROGRAM)"
370
+ ```
371
+
372
+ ## Filtering Issues
373
+
374
+ ### Filter Not Matching Packages
375
+
376
+ If `--filter` doesn't match expected packages:
377
+
378
+ **Debug filtering:**
379
+
380
+ ```bash
381
+ # List all packages first
382
+ find . -name "package.json" -not -path "./node_modules/*" | head -10
383
+
384
+ # Check package names in workspace
385
+ ```
386
+
387
+ **Common filter patterns:**
388
+
389
+ ```json
390
+ {
391
+ "scripts": {
392
+ "test:scope": "wsu run test --filter '@company/*'",
393
+ "test:apps": "wsu run test --filter 'apps/*'",
394
+ "test:utils": "wsu run test --filter '*-utils'",
395
+ "test:frontend": "wsu run test --filter '*frontend*'"
396
+ }
397
+ }
398
+ ```
399
+
400
+ ### Case Sensitivity
401
+
402
+ Filters are case-sensitive. Use exact casing:
403
+
404
+ ```json
405
+ {
406
+ "scripts": {
407
+ "test:React": "wsu run test --filter '*React*'", // Correct
408
+ "test:react": "wsu run test --filter '*react*'" // Different results
409
+ }
410
+ }
411
+ ```
412
+
413
+ ## Environment Issues
414
+
415
+ ### Node.js Version
416
+
417
+ workspace-utils requires Node.js 18+:
418
+
419
+ ```bash
420
+ # Check version
421
+ node --version
422
+
423
+ # If too old, update Node.js
424
+ # Use nvm, fnm, or download from nodejs.org
425
+ ```
426
+
427
+ ### Environment Variables
428
+
429
+ If environment variables aren't being passed:
430
+
431
+ **In your package scripts:**
432
+
433
+ ```json
434
+ {
435
+ "scripts": {
436
+ "test:env": "NODE_ENV=test wsu run test",
437
+ "build:prod": "NODE_ENV=production wsu build"
438
+ }
439
+ }
440
+ ```
441
+
442
+ **For complex environments, use .env files in individual packages.**
443
+
444
+ ## Getting Help
445
+
446
+ ### Enable Debug Mode
447
+
448
+ If you need more information about what's happening:
449
+
450
+ ```json
451
+ {
452
+ "scripts": {
453
+ "debug:build": "DEBUG=workspace-utils wsu build",
454
+ "debug:test": "DEBUG=* wsu run test"
455
+ }
456
+ }
457
+ ```
458
+
459
+ ### Verbose Output
460
+
461
+ For more detailed logging:
462
+
463
+ ```bash
464
+ # Run with verbose npm logging
465
+ npm run build --verbose
466
+
467
+ # Check the exact commands being run
468
+ ```
469
+
470
+ ### Common Command Issues
471
+
472
+ **Command not found:**
473
+
474
+ ```bash
475
+ # Make sure workspace-utils is installed
476
+ npm ls workspace-utils
477
+
478
+ # Try running directly
479
+ npx wsu --version
480
+ ```
481
+
482
+ **Script syntax errors:**
483
+
484
+ ```json
485
+ {
486
+ "scripts": {
487
+ // ❌ Wrong quotes
488
+ "test": "wsu run test",
489
+
490
+ // ✅ Correct quotes
491
+ "test": "wsu run test"
492
+ }
493
+ }
494
+ ```
495
+
496
+ ## Reporting Issues
497
+
498
+ If you encounter a bug or need help:
499
+
500
+ 1. **Check this troubleshooting guide first**
501
+ 2. **Search existing issues** in the repository
502
+ 3. **Provide minimal reproduction** when reporting:
503
+ - Your `package.json` scripts
504
+ - Workspace structure
505
+ - Full error message
506
+ - Node.js and package manager versions
507
+
508
+ **Useful info to include:**
509
+
510
+ ```bash
511
+ # System information
512
+ node --version
513
+ npm --version # or pnpm --version, bun --version
514
+ npx wsu --version
515
+
516
+ # Workspace structure
517
+ tree -I node_modules -L 3
518
+ ```
519
+
520
+ ## Quick Fixes Checklist
521
+
522
+ When things go wrong, try these in order:
523
+
524
+ 1. **Check installation:** `npm ls workspace-utils`
525
+ 2. **Verify workspace setup:** Ensure workspace config files exist
526
+ 3. **Clean and reinstall:** `rm -rf node_modules && npm install`
527
+ 4. **Check package.json syntax:** Ensure scripts are properly quoted
528
+ 5. **Test individual packages:** `cd package && npm run script-name`
529
+ 6. **Reduce concurrency:** Add `--concurrency 1` to scripts
530
+ 7. **Check Node.js version:** `node --version` (should be 18+)
531
+ 8. **Clear caches:** `npm cache clean --force`
532
+
533
+ Most issues are resolved by steps 1-4!
package/index.ts ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env bun
2
+ import { Command } from 'commander';
3
+ import { readFileSync } from 'fs';
4
+ import { join, dirname } from 'path';
5
+ import { fileURLToPath } from 'url';
6
+ import { runCommand } from './src/commands/run.ts';
7
+ import { buildCommand } from './src/commands/build.ts';
8
+ import { devCommand } from './src/commands/dev.ts';
9
+
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = dirname(__filename);
12
+
13
+ // Read package.json for version by traversing up directories
14
+ function findPackageJson(startDir: string): Record<string, unknown> {
15
+ let currentDir = startDir;
16
+ while (currentDir !== dirname(currentDir)) {
17
+ try {
18
+ const packageJsonPath = join(currentDir, 'package.json');
19
+ const content = readFileSync(packageJsonPath, 'utf8');
20
+ return JSON.parse(content) as Record<string, unknown>;
21
+ } catch {
22
+ currentDir = dirname(currentDir);
23
+ }
24
+ }
25
+ throw new Error('package.json not found in any parent directory');
26
+ }
27
+
28
+ const packageJson = findPackageJson(__dirname);
29
+
30
+ const program = new Command();
31
+
32
+ program
33
+ .name('workspace-utils')
34
+ .description('CLI tool to orchestrate scripts across monorepo workspaces (Bun, pnpm, npm)')
35
+ .version(packageJson.version as string)
36
+ .option('--ascii', 'Force ASCII output (no Unicode/emoji characters)', false);
37
+
38
+ // Run command - execute a script across packages
39
+ program
40
+ .command('run <script>')
41
+ .description('Run a script across multiple packages')
42
+ .option('-c, --concurrency <number>', 'Maximum number of concurrent processes', '4')
43
+ .option('-f, --filter <pattern>', 'Filter packages by pattern (e.g., @scope/*)')
44
+ .option('--sequential', 'Run scripts sequentially (default is parallel)', false)
45
+ .action((script, options) => {
46
+ if (program.opts().ascii) {
47
+ process.env.WSU_ASCII = '1';
48
+ }
49
+ return runCommand(script, options);
50
+ });
51
+
52
+ // Build command - build packages in dependency order
53
+ program
54
+ .command('build')
55
+ .description('Build packages in dependency order')
56
+ .option('-f, --filter <pattern>', 'Filter packages by pattern')
57
+ .option('-c, --concurrency <number>', 'Maximum number of concurrent builds', '4')
58
+ .option('--skip-unchanged', "Skip packages that haven't changed (future feature)", false)
59
+ .action(options => {
60
+ if (program.opts().ascii) {
61
+ process.env.WSU_ASCII = '1';
62
+ }
63
+ return buildCommand(options);
64
+ });
65
+
66
+ // Dev command - run dev scripts in parallel with live logs
67
+ program
68
+ .command('dev')
69
+ .description('Run dev scripts across packages with live log streaming')
70
+ .option('-f, --filter <pattern>', 'Filter packages by pattern')
71
+ .option('-c, --concurrency <number>', 'Maximum number of concurrent processes', '4')
72
+ .action(options => {
73
+ if (program.opts().ascii) {
74
+ process.env.WSU_ASCII = '1';
75
+ }
76
+ return devCommand(options);
77
+ });
78
+
79
+ // Default to help if no command provided
80
+ if (process.argv.length <= 2) {
81
+ program.help();
82
+ }
83
+
84
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "workspace-utils",
3
+ "version": "1.0.0",
4
+ "description": "A CLI tool to orchestrate scripts across monorepo workspaces (Bun, pnpm, npm) with parallel execution and dependency-aware builds.",
5
+ "module": "index.ts",
6
+ "type": "module",
7
+ "bin": {
8
+ "workspace-utils": "./dist/index.js",
9
+ "wsu": "./dist/index.js"
10
+ },
11
+ "scripts": {
12
+ "build": "bun build index.ts --outdir dist --target node && cp package.json dist/",
13
+ "dev": "bun run index.ts",
14
+ "prepublishOnly": "bun run build",
15
+ "format": "prettier --write .",
16
+ "format:check": "prettier --check .",
17
+ "test": "bun test",
18
+ "test:watch": "bun test --watch",
19
+ "docs:build": "cd docs && mdbook build",
20
+ "docs:serve": "cd docs && mdbook serve",
21
+ "docs:watch": "cd docs && mdbook serve --open"
22
+ },
23
+ "keywords": [
24
+ "monorepo",
25
+ "workspaces",
26
+ "bun",
27
+ "pnpm",
28
+ "npm",
29
+ "cli",
30
+ "parallel",
31
+ "dependency-graph",
32
+ "build-orchestration"
33
+ ],
34
+ "author": "",
35
+ "license": "MIT",
36
+ "dependencies": {
37
+ "commander": "^11.1.0",
38
+ "fast-glob": "^3.3.2",
39
+ "picocolors": "^1.0.0",
40
+ "yaml": "^2.8.1"
41
+ },
42
+ "devDependencies": {
43
+ "@types/bun": "^1.2.20",
44
+ "@types/node": "^20.10.0",
45
+ "prettier": "^3.6.2"
46
+ },
47
+ "peerDependencies": {
48
+ "typescript": "^5"
49
+ },
50
+ "engines": {
51
+ "node": ">=18",
52
+ "bun": ">=1.0.0"
53
+ }
54
+ }