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,239 @@
1
+ # Commands Overview
2
+
3
+ workspace-utils provides three main commands designed to handle different aspects of monorepo workflow management. Each command is optimized for specific use cases while sharing common functionality like package filtering and concurrency control.
4
+
5
+ ## Command Summary
6
+
7
+ | Command | Purpose | Default Mode | Use Case |
8
+ | --------------------- | -------------------------------------- | -------------------- | ------------------------------ |
9
+ | [`run`](./run.md) | Execute scripts across packages | **Parallel** | Tests, linting, custom scripts |
10
+ | [`build`](./build.md) | Build packages respecting dependencies | **Dependency order** | Production builds, CI/CD |
11
+ | [`dev`](./dev.md) | Start development servers | **Parallel** | Local development |
12
+
13
+ ## Quick Reference
14
+
15
+ ### wsu run
16
+
17
+ ```bash
18
+ wsu run <script> [options]
19
+ ```
20
+
21
+ Execute any script across multiple packages in parallel (by default) or sequentially.
22
+
23
+ **Package.json setup:**
24
+
25
+ ```json
26
+ {
27
+ "scripts": {
28
+ "test": "wsu run test",
29
+ "lint": "wsu run lint --sequential",
30
+ "typecheck": "wsu run typecheck --concurrency 8"
31
+ }
32
+ }
33
+ ```
34
+
35
+ **Usage:**
36
+
37
+ ```bash
38
+ npm run test # Run tests in parallel
39
+ npm run lint # Run linting sequentially
40
+ npm run typecheck # Custom concurrency
41
+ ```
42
+
43
+ ### wsu build
44
+
45
+ ```bash
46
+ wsu build [options]
47
+ ```
48
+
49
+ Build packages in dependency order using topological sorting. Packages are built in batches where each batch can run in parallel.
50
+
51
+ **Package.json setup:**
52
+
53
+ ```json
54
+ {
55
+ "scripts": {
56
+ "build": "wsu build",
57
+ "build:apps": "wsu build --filter 'apps/*'",
58
+ "build:slow": "wsu build --concurrency 2"
59
+ }
60
+ }
61
+ ```
62
+
63
+ **Usage:**
64
+
65
+ ```bash
66
+ npm run build # Build all packages
67
+ npm run build:apps # Build only apps
68
+ npm run build:slow # Limit batch concurrency
69
+ ```
70
+
71
+ ### wsu dev
72
+
73
+ ```bash
74
+ wsu dev [options]
75
+ ```
76
+
77
+ Start development servers with live log streaming. Designed for long-running processes with real-time output.
78
+
79
+ **Package.json setup:**
80
+
81
+ ```json
82
+ {
83
+ "scripts": {
84
+ "dev": "wsu dev",
85
+ "dev:scope": "wsu dev --filter '@scope/*'",
86
+ "dev:limited": "wsu dev --concurrency 3"
87
+ }
88
+ }
89
+ ```
90
+
91
+ **Usage:**
92
+
93
+ ```bash
94
+ npm run dev # Start all dev servers
95
+ npm run dev:scope # Start scoped packages
96
+ npm run dev:limited # Limit concurrent servers
97
+ ```
98
+
99
+ ## Global Options
100
+
101
+ All commands support these common options:
102
+
103
+ ### Filtering
104
+
105
+ - `--filter <pattern>` - Filter packages using glob patterns
106
+ - Examples: `"@scope/*"`, `"apps/*"`, `"*-utils"`, `"*frontend*"`
107
+
108
+ ### Concurrency
109
+
110
+ - `--concurrency <number>` - Maximum concurrent processes (default: 4)
111
+ - Applies to parallel execution and batch processing
112
+
113
+ ### Help
114
+
115
+ - `--help` - Show command-specific help and options
116
+
117
+ ## Execution Modes
118
+
119
+ ### Parallel Execution
120
+
121
+ **Used by:** `run` (default), `dev`
122
+ **Behavior:** All matching packages execute simultaneously
123
+ **Best for:** Tests, linting, development servers
124
+
125
+ ```bash
126
+ npm run test # with "test": "wsu run test" in package.json
127
+ # ✅ Runs tests in all packages at the same time
128
+ ```
129
+
130
+ ### Sequential Execution
131
+
132
+ **Used by:** `run` (with --sequential flag)
133
+ **Behavior:** Packages execute one after another
134
+ **Best for:** Resource-intensive tasks, ordered operations
135
+
136
+ ```bash
137
+ npm run build:sequential # with "build:sequential": "wsu run build --sequential"
138
+ # ✅ Runs builds one package at a time
139
+ ```
140
+
141
+ ### Dependency-Aware Execution
142
+
143
+ **Used by:** `build`
144
+ **Behavior:** Packages are grouped into batches based on dependencies
145
+ **Best for:** Building, publishing, dependency-critical operations
146
+
147
+ ```bash
148
+ npm run build # with "build": "wsu build" in package.json
149
+ # ✅ Builds dependencies first, then dependents
150
+ ```
151
+
152
+ ## Package Manager Detection
153
+
154
+ workspace-utils automatically detects your package manager:
155
+
156
+ | Package Manager | Detection Criteria |
157
+ | --------------- | ----------------------------------------------------------------- |
158
+ | **Bun** | `bun.lockb`, `bunfig.toml`, or bun-specific `package.json` fields |
159
+ | **pnpm** | `pnpm-lock.yaml`, `pnpm-workspace.yaml` |
160
+ | **npm** | `package-lock.json`, `.npmrc` |
161
+
162
+ The detected package manager is used for all script execution (e.g., `bun run test`, `pnpm run test`, `npm run test`).
163
+
164
+ ## Error Handling
165
+
166
+ ### Exit Behavior
167
+
168
+ - **Single failure in parallel mode:** Other packages continue running
169
+ - **Single failure in sequential mode:** Execution stops immediately
170
+ - **Single failure in build mode:** Current batch fails, subsequent batches are skipped
171
+
172
+ ### Exit Codes
173
+
174
+ - `0` - All packages executed successfully
175
+ - `1` - One or more packages failed
176
+ - `2` - Configuration or setup error
177
+
178
+ ## Output Format
179
+
180
+ All commands provide consistent, color-coded output:
181
+
182
+ ```
183
+ 🚀 Starting operation...
184
+ 📁 Workspace root: /path/to/workspace
185
+ 📦 Found X packages
186
+ ✅ Running "script" in Y packages
187
+ 🔧 Package manager: detected-pm
188
+ ⚡ Execution mode: parallel/sequential/dependency-order
189
+
190
+ [package-1] Starting: pm run script
191
+ [package-2] Starting: pm run script
192
+ [package-1] ✅ Completed in 1,234ms
193
+ [package-2] ❌ Failed with exit code 1
194
+
195
+ 📊 Execution Summary:
196
+ ✅ Successful: 1
197
+ ❌ Failed: 1
198
+ ⏱️ Total duration: 2,345ms
199
+ ```
200
+
201
+ ## Best Practices
202
+
203
+ ### Choose the Right Command
204
+
205
+ - **`run`** for most script execution (tests, linting, utilities)
206
+ - **`build`** when package dependencies matter
207
+ - **`dev`** for long-running development processes
208
+
209
+ ### Use Filtering Effectively
210
+
211
+ ```json
212
+ {
213
+ "scripts": {
214
+ "test:packages": "wsu run test --filter 'packages/*'",
215
+ "dev:apps": "wsu dev --filter 'apps/*'",
216
+ "build:company": "wsu build --filter '@company/*'"
217
+ }
218
+ }
219
+ ```
220
+
221
+ ### Optimize Concurrency
222
+
223
+ ```json
224
+ {
225
+ "scripts": {
226
+ "build:slow": "wsu run build --concurrency 2",
227
+ "test:fast": "wsu run test --concurrency 8",
228
+ "dev": "wsu dev --concurrency 4"
229
+ }
230
+ }
231
+ ```
232
+
233
+ ## Next Steps
234
+
235
+ Dive deeper into each command:
236
+
237
+ - [**run command**](./run.md) - Detailed script execution options
238
+ - [**build command**](./build.md) - Dependency-aware building
239
+ - [**dev command**](./dev.md) - Development server management
@@ -0,0 +1,153 @@
1
+ # run Command
2
+
3
+ The `run` command executes scripts across multiple packages in your workspace, with parallel execution by default.
4
+
5
+ ## Recommended Usage
6
+
7
+ Add to your `package.json` scripts:
8
+
9
+ ```json
10
+ {
11
+ "scripts": {
12
+ "test": "wsu run test",
13
+ "lint": "wsu run lint",
14
+ "typecheck": "wsu run typecheck"
15
+ }
16
+ }
17
+ ```
18
+
19
+ Then run:
20
+
21
+ ```bash
22
+ npm run test # Run tests across packages
23
+ npm run lint # Run linting across packages
24
+ npm run typecheck # Run type checking across packages
25
+ ```
26
+
27
+ ## Direct Usage (if needed)
28
+
29
+ ```bash
30
+ wsu run <script> [options]
31
+ ```
32
+
33
+ ## Arguments
34
+
35
+ - `<script>` - The npm script name to execute (e.g., `test`, `lint`, `build`)
36
+
37
+ ## Options
38
+
39
+ | Option | Description | Default |
40
+ | ------------------------ | ------------------------------------ | ------------ |
41
+ | `--filter <pattern>` | Filter packages by glob pattern | All packages |
42
+ | `--concurrency <number>` | Max concurrent processes | `4` |
43
+ | `--sequential` | Run sequentially instead of parallel | `false` |
44
+
45
+ ## Examples
46
+
47
+ ### Basic Usage
48
+
49
+ Add scripts to your `package.json`:
50
+
51
+ ```json
52
+ {
53
+ "scripts": {
54
+ "test": "wsu run test",
55
+ "test:sequential": "wsu run test --sequential",
56
+ "lint": "wsu run lint --concurrency 8"
57
+ }
58
+ }
59
+ ```
60
+
61
+ Then run:
62
+
63
+ ```bash
64
+ npm run test # Run tests in parallel (default)
65
+ npm run test:sequential # Run tests sequentially
66
+ npm run lint # Run linting with custom concurrency
67
+ ```
68
+
69
+ ### Package Filtering
70
+
71
+ Add filtered scripts to your `package.json`:
72
+
73
+ ```json
74
+ {
75
+ "scripts": {
76
+ "test:company": "wsu run test --filter '@company/*'",
77
+ "dev:apps": "wsu run dev --filter 'apps/*'",
78
+ "build:backend": "wsu run build --filter '*backend*'"
79
+ }
80
+ }
81
+ ```
82
+
83
+ Then run:
84
+
85
+ ```bash
86
+ npm run test:company # Run tests for scoped packages
87
+ npm run dev:apps # Run dev scripts for apps only
88
+ npm run build:backend # Run builds for backend packages
89
+ ```
90
+
91
+ ### Example Output
92
+
93
+ ```
94
+ 🚀 Running script "test" across packages...
95
+
96
+ 📦 Found 3 packages
97
+ ✅ Running "test" in 3 packages:
98
+ • @company/utils
99
+ • @company/ui-components
100
+ • apps/web-app
101
+
102
+ 🔧 Package manager: pnpm
103
+ ⚡ Execution mode: parallel (concurrency: 4)
104
+
105
+ [@company/utils] Starting: pnpm run test
106
+ [@company/ui-components] Starting: pnpm run test
107
+ [apps/web-app] Starting: pnpm run test
108
+ [@company/utils] ✅ Completed in 1,234ms
109
+ [@company/ui-components] ✅ Completed in 2,456ms
110
+ [apps/web-app] ✅ Completed in 3,789ms
111
+
112
+ 📊 Execution Summary:
113
+ ✅ Successful: 3
114
+ ⏱️ Total duration: 3,789ms
115
+ ```
116
+
117
+ ## Execution Modes
118
+
119
+ ### Parallel (Default)
120
+
121
+ - Runs all packages simultaneously
122
+ - Faster for I/O bound tasks like tests and linting
123
+ - Other packages continue if one fails
124
+
125
+ ### Sequential
126
+
127
+ - Runs packages one at a time
128
+ - Stops on first failure
129
+ - Better for resource-intensive tasks
130
+
131
+ ## When to Use
132
+
133
+ - **Tests** - Parallel execution for fast feedback
134
+ - **Linting** - High concurrency for quick checks
135
+ - **Type checking** - Parallel across packages
136
+ - **Custom scripts** - Any script that exists in package.json
137
+
138
+ ## Package.json Integration
139
+
140
+ Add common patterns to your root `package.json`:
141
+
142
+ ```json
143
+ {
144
+ "scripts": {
145
+ "test": "wsu run test",
146
+ "test:watch": "wsu run test:watch",
147
+ "lint": "wsu run lint --concurrency 8",
148
+ "lint:fix": "wsu run lint:fix",
149
+ "typecheck": "wsu run typecheck",
150
+ "clean": "wsu run clean --sequential"
151
+ }
152
+ }
153
+ ```
@@ -0,0 +1,249 @@
1
+ # Configuration
2
+
3
+ workspace-utils is designed to work with **zero configuration** by default. It automatically detects your workspace setup and package manager, then runs with sensible defaults. However, you can customize behavior through command-line options.
4
+
5
+ ## No Configuration Files
6
+
7
+ Unlike many other tools, workspace-utils **does not use configuration files**. All behavior is controlled through:
8
+
9
+ 1. **Command-line flags** (primary method)
10
+ 2. **Workspace structure** (automatic detection)
11
+ 3. **Package manager detection** (automatic)
12
+
13
+ This approach keeps things simple and ensures your commands are explicit and reproducible.
14
+
15
+ ## Command-Line Configuration
16
+
17
+ All configuration is done through command-line options. Here are the most commonly used options:
18
+
19
+ ### Global Options
20
+
21
+ Available for all commands:
22
+
23
+ | Option | Description | Default | Example |
24
+ | ------------------------ | ------------------------------- | ------------ | --------------------- |
25
+ | `--filter <pattern>` | Filter packages by glob pattern | All packages | `--filter "@scope/*"` |
26
+ | `--concurrency <number>` | Max concurrent processes | `4` | `--concurrency 8` |
27
+ | `--help` | Show command help | - | `--help` |
28
+
29
+ ### Command-Specific Options
30
+
31
+ #### run command
32
+
33
+ ```bash
34
+ workspace-utils run <script> [options]
35
+ ```
36
+
37
+ | Option | Description | Default |
38
+ | -------------- | ------------------------------------ | ------- |
39
+ | `--sequential` | Run sequentially instead of parallel | `false` |
40
+
41
+ #### build command
42
+
43
+ ```bash
44
+ workspace-utils build [options]
45
+ ```
46
+
47
+ | Option | Description | Default |
48
+ | ------------------ | -------------------------------- | ------- |
49
+ | `--skip-unchanged` | Skip unchanged packages (future) | `false` |
50
+
51
+ #### dev command
52
+
53
+ ```bash
54
+ workspace-utils dev [options]
55
+ ```
56
+
57
+ No additional options - uses global options only.
58
+
59
+ ## Environment Variables
60
+
61
+ workspace-utils respects these environment variables:
62
+
63
+ | Variable | Description | Default |
64
+ | ------------- | -------------------- | ------------- |
65
+ | `NODE_ENV` | Node environment | `development` |
66
+ | `FORCE_COLOR` | Force colored output | `1` (enabled) |
67
+
68
+ ## Package Manager Detection
69
+
70
+ workspace-utils automatically detects your package manager based on lock files and configuration:
71
+
72
+ ### Detection Priority
73
+
74
+ 1. **Bun** - if `bun.lockb` exists or bun-specific fields in `package.json`
75
+ 2. **pnpm** - if `pnpm-lock.yaml` or `pnpm-workspace.yaml` exists
76
+ 3. **npm** - if `package-lock.json` exists
77
+
78
+ ### Workspace Configuration
79
+
80
+ The tool reads workspace configuration from:
81
+
82
+ | Package Manager | Configuration Source |
83
+ | --------------- | ---------------------------------------- |
84
+ | **Bun** | `package.json` → `workspaces` field |
85
+ | **pnpm** | `pnpm-workspace.yaml` → `packages` field |
86
+ | **npm** | `package.json` → `workspaces` field |
87
+
88
+ ## Workspace Structure Requirements
89
+
90
+ ### Supported Workspace Formats
91
+
92
+ #### Bun Workspaces
93
+
94
+ ```json
95
+ {
96
+ "name": "my-monorepo",
97
+ "workspaces": ["packages/*", "apps/*"]
98
+ }
99
+ ```
100
+
101
+ #### pnpm Workspaces
102
+
103
+ ```yaml
104
+ # pnpm-workspace.yaml
105
+ packages:
106
+ - 'packages/*'
107
+ - 'apps/*'
108
+ - '!**/test/**'
109
+ ```
110
+
111
+ #### npm Workspaces
112
+
113
+ ```json
114
+ {
115
+ "name": "my-monorepo",
116
+ "workspaces": {
117
+ "packages": ["packages/*", "apps/*"]
118
+ }
119
+ }
120
+ ```
121
+
122
+ ## Common Configuration Patterns
123
+
124
+ ### Package.json Scripts
125
+
126
+ Add workspace-utils commands to your root `package.json`:
127
+
128
+ ```json
129
+ {
130
+ "scripts": {
131
+ "test": "workspace-utils run test",
132
+ "test:sequential": "workspace-utils run test --sequential",
133
+ "build": "workspace-utils build",
134
+ "build:apps": "workspace-utils build --filter 'apps/*'",
135
+ "dev": "workspace-utils dev",
136
+ "lint": "workspace-utils run lint --concurrency 8",
137
+ "typecheck": "workspace-utils run typecheck --parallel"
138
+ }
139
+ }
140
+ ```
141
+
142
+ ### CI/CD Configuration
143
+
144
+ Optimize for continuous integration:
145
+
146
+ ```bash
147
+ # Lower concurrency for CI resources
148
+ workspace-utils run test --concurrency 2
149
+
150
+ # Sequential builds for predictable resource usage
151
+ workspace-utils build --concurrency 1
152
+
153
+ # Parallel linting for speed
154
+ workspace-utils run lint --concurrency 4
155
+ ```
156
+
157
+ ### Development Workflow
158
+
159
+ Optimize for local development:
160
+
161
+ ```bash
162
+ # Start all dev servers
163
+ workspace-utils dev
164
+
165
+ # Run tests with higher concurrency on powerful dev machines
166
+ workspace-utils run test --concurrency 8
167
+
168
+ # Filter to work on specific features
169
+ workspace-utils dev --filter "*frontend*"
170
+ ```
171
+
172
+ ## Performance Tuning
173
+
174
+ ### Concurrency Guidelines
175
+
176
+ | Task Type | Recommended Concurrency | Reasoning |
177
+ | --------------- | ----------------------- | -------------------------------- |
178
+ | **Tests** | 4-8 | Usually I/O bound, can run many |
179
+ | **Builds** | 2-4 | CPU intensive, fewer parallel |
180
+ | **Linting** | 6-12 | Fast, lightweight processes |
181
+ | **Dev servers** | 2-6 | Resource intensive, long-running |
182
+
183
+ ### System Resources
184
+
185
+ Consider your system when setting concurrency:
186
+
187
+ ```bash
188
+ # For 4-core machines
189
+ workspace-utils run build --concurrency 2
190
+
191
+ # For 8+ core machines
192
+ workspace-utils run test --concurrency 8
193
+
194
+ # For CI with limited resources
195
+ workspace-utils run lint --concurrency 2
196
+ ```
197
+
198
+ ## Filtering Patterns
199
+
200
+ ### Glob Patterns
201
+
202
+ workspace-utils supports standard glob patterns:
203
+
204
+ | Pattern | Matches | Example |
205
+ | ------------ | ------------------------------ | ----------------------------------- |
206
+ | `@scope/*` | All packages in scope | `@company/utils`, `@company/ui` |
207
+ | `apps/*` | All packages in apps directory | `apps/web`, `apps/mobile` |
208
+ | `*-utils` | Packages ending with -utils | `date-utils`, `string-utils` |
209
+ | `*frontend*` | Packages containing frontend | `my-frontend-app`, `frontend-utils` |
210
+
211
+ ### Multiple Filters
212
+
213
+ Use multiple invocations for complex filtering:
214
+
215
+ ```bash
216
+ # Run tests on backend packages only
217
+ workspace-utils run test --filter "@company/backend-*"
218
+
219
+ # Build all apps and shared libraries
220
+ workspace-utils build --filter "apps/*"
221
+ workspace-utils build --filter "@company/shared-*"
222
+ ```
223
+
224
+ ## Error Handling Configuration
225
+
226
+ ### Exit Behavior
227
+
228
+ workspace-utils has different exit behaviors by command:
229
+
230
+ | Command | Failure Behavior |
231
+ | ------------------ | --------------------------------------------- |
232
+ | `run` (parallel) | Continue other packages, exit 1 if any failed |
233
+ | `run` (sequential) | Stop on first failure, exit with that code |
234
+ | `build` | Stop current batch on failure, skip remaining |
235
+ | `dev` | Stop all servers on any failure |
236
+
237
+ ### Error Codes
238
+
239
+ | Exit Code | Meaning |
240
+ | --------- | ------------------------------------------ |
241
+ | `0` | All operations successful |
242
+ | `1` | One or more package operations failed |
243
+ | `2` | Configuration or workspace detection error |
244
+
245
+ ## Next Steps
246
+
247
+ - Learn about [Workspace Detection](./concepts/workspace-detection.md)
248
+ - Explore [Package Manager Support](./concepts/package-managers.md)
249
+ - See [Performance Tuning](./advanced/performance.md) for optimization tips