command-stream 0.0.5 → 0.2.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.
- package/$.mjs +1522 -213
- package/README.md +44 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -137,6 +137,37 @@ console.log(result.stdout);
|
|
|
137
137
|
console.log(result.code); // exit code
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
+
### Execution Control (NEW!)
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
import { $ } from 'command-stream';
|
|
144
|
+
|
|
145
|
+
// Commands don't auto-start when created
|
|
146
|
+
const cmd = $`echo "hello"`;
|
|
147
|
+
|
|
148
|
+
// Three ways to start execution:
|
|
149
|
+
|
|
150
|
+
// 1. Explicit start with options
|
|
151
|
+
cmd.start(); // Default async mode
|
|
152
|
+
cmd.start({ mode: 'async' }); // Explicitly async
|
|
153
|
+
cmd.start({ mode: 'sync' }); // Synchronous execution
|
|
154
|
+
|
|
155
|
+
// 2. Convenience methods
|
|
156
|
+
cmd.async(); // Same as start({ mode: 'async' })
|
|
157
|
+
cmd.sync(); // Same as start({ mode: 'sync' })
|
|
158
|
+
|
|
159
|
+
// 3. Auto-start by awaiting (always async)
|
|
160
|
+
await cmd; // Auto-starts in async mode
|
|
161
|
+
|
|
162
|
+
// Event handlers can be attached before starting
|
|
163
|
+
const process = $`long-command`
|
|
164
|
+
.on('data', chunk => console.log('Received:', chunk))
|
|
165
|
+
.on('end', result => console.log('Done!'));
|
|
166
|
+
|
|
167
|
+
// Start whenever you're ready
|
|
168
|
+
process.start();
|
|
169
|
+
```
|
|
170
|
+
|
|
140
171
|
### Synchronous Execution
|
|
141
172
|
|
|
142
173
|
```javascript
|
|
@@ -169,6 +200,7 @@ for await (const chunk of $`long-running-command`.stream()) {
|
|
|
169
200
|
```javascript
|
|
170
201
|
import { $ } from 'command-stream';
|
|
171
202
|
|
|
203
|
+
// Attach event handlers then start execution
|
|
172
204
|
$`command`
|
|
173
205
|
.on('data', chunk => {
|
|
174
206
|
if (chunk.type === 'stdout') {
|
|
@@ -177,7 +209,13 @@ $`command`
|
|
|
177
209
|
})
|
|
178
210
|
.on('stderr', chunk => console.log('Stderr:', chunk))
|
|
179
211
|
.on('end', result => console.log('Done:', result))
|
|
180
|
-
.on('exit', code => console.log('Exit code:', code))
|
|
212
|
+
.on('exit', code => console.log('Exit code:', code))
|
|
213
|
+
.start(); // Explicitly start the command
|
|
214
|
+
|
|
215
|
+
// Or auto-start by awaiting
|
|
216
|
+
const cmd = $`another-command`
|
|
217
|
+
.on('data', chunk => console.log(chunk));
|
|
218
|
+
await cmd; // Auto-starts in async mode
|
|
181
219
|
```
|
|
182
220
|
|
|
183
221
|
### Mixed Pattern (Best of Both Worlds)
|
|
@@ -585,10 +623,13 @@ The enhanced `$` function returns a `ProcessRunner` instance that extends `Event
|
|
|
585
623
|
|
|
586
624
|
#### Methods
|
|
587
625
|
|
|
626
|
+
- `start(options)`: Explicitly start command execution
|
|
627
|
+
- `options.mode`: `'async'` (default) or `'sync'` - execution mode
|
|
628
|
+
- `async()`: Shortcut for `start({ mode: 'async' })` - start async execution
|
|
629
|
+
- `sync()`: Shortcut for `start({ mode: 'sync' })` - execute synchronously (blocks until completion)
|
|
588
630
|
- `stream()`: Returns an async iterator for real-time chunk processing
|
|
589
|
-
- `sync()`: Execute command synchronously (blocks until completion, events batched)
|
|
590
631
|
- `pipe(destination)`: Programmatically pipe output to another command (returns new ProcessRunner)
|
|
591
|
-
- `then()`, `catch()`, `finally()`: Promise interface for await support
|
|
632
|
+
- `then()`, `catch()`, `finally()`: Promise interface for await support (auto-starts in async mode)
|
|
592
633
|
|
|
593
634
|
#### Properties
|
|
594
635
|
|
package/package.json
CHANGED