Skip to content

Commit 4908739

Browse files
committed
Use stream.pipeline()
1 parent 3bcc0d6 commit 4908739

File tree

6 files changed

+120
-90
lines changed

6 files changed

+120
-90
lines changed

README.md

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ technical lead for 2.5 years. I am available for full-time remote positions.
5151
`gulpfile.js`:
5252

5353
```js
54+
import { pipeline } from 'node:stream/promises'
55+
5456
import gulp from 'gulp'
55-
import { task, exec, stream } from 'gulp-execa'
57+
import { exec, stream, task } from 'gulp-execa'
5658

5759
export const audit = task('npm audit')
5860

@@ -61,10 +63,11 @@ export const outdated = async () => {
6163
}
6264

6365
export const sort = () =>
64-
gulp
65-
.src('*.txt')
66-
.pipe(stream(({ path }) => `sort ${path}`))
67-
.pipe(gulp.dest('sorted'))
66+
pipeline(
67+
gulp.src('*.txt'),
68+
stream(({ path }) => `sort ${path}`),
69+
gulp.dest('sorted'),
70+
)
6871
```
6972

7073
# Install
@@ -128,14 +131,17 @@ Returns a stream that executes a `command` on each input file.
128131
- `undefined`
129132

130133
```js
134+
import { pipeline } from 'node:stream/promises'
135+
131136
import gulp from 'gulp'
132137
import { stream } from 'gulp-execa'
133138

134139
export const sort = () =>
135-
gulp
136-
.src('*.txt')
137-
.pipe(stream(({ path }) => `sort ${path}`))
138-
.pipe(gulp.dest('sorted'))
140+
pipeline(
141+
gulp.src('*.txt'),
142+
stream(({ path }) => `sort ${path}`),
143+
gulp.dest('sorted'),
144+
)
139145
```
140146

141147
Each file in the stream will spawn a separate process. This can consume lots of
@@ -251,21 +257,22 @@ With [`stream()`](#streamfunction-options), whether the command result should:
251257
<!-- eslint-disable unicorn/no-null -->
252258

253259
```js
260+
import { pipeline } from 'node:stream/promises'
261+
254262
import gulp from 'gulp'
255263
import { stream } from 'gulp-execa'
256264
import through from 'through2'
257265

258266
export const task = () =>
259-
gulp
260-
.src('*.js')
267+
pipeline(
268+
gulp.src('*.js'),
261269
// Prints the number of lines of each file
262-
.pipe(stream(({ path }) => `wc -l ${path}`, { result: 'save' }))
263-
.pipe(
264-
through.obj((file, encoding, func) => {
265-
console.log(file.execa[0].stdout)
266-
func(null, file)
267-
}),
268-
)
270+
stream(({ path }) => `wc -l ${path}`, { result: 'save' }),
271+
through.obj((file, encoding, func) => {
272+
console.log(file.execa[0].stdout)
273+
func(null, file)
274+
}),
275+
)
269276
```
270277

271278
## from
@@ -279,23 +286,22 @@ Which output stream to use with [`result: 'replace'`](#result).
279286
<!-- eslint-disable unicorn/no-null -->
280287

281288
```js
289+
import { pipeline } from 'node:stream/promises'
290+
282291
import gulp from 'gulp'
283292
import { stream } from 'gulp-execa'
284293
import through from 'through2'
285294

286295
export const task = () =>
287-
gulp
288-
.src('*.js')
296+
pipeline(
297+
gulp.src('*.js'),
289298
// Prints the number of lines of each file, including `stderr`
290-
.pipe(
291-
stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
292-
)
293-
.pipe(
294-
through.obj((file, encoding, func) => {
295-
console.log(file.contents.toString())
296-
func(null, file)
297-
}),
298-
)
299+
stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
300+
through.obj((file, encoding, func) => {
301+
console.log(file.contents.toString())
302+
func(null, file)
303+
}),
304+
)
299305
```
300306

301307
## maxConcurrency

src/helpers/gulpfiles/stream.test.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Buffer } from 'node:buffer'
2+
import { pipeline } from 'node:stream/promises'
23
import { fileURLToPath } from 'node:url'
34
import { callbackify } from 'node:util'
45

@@ -20,66 +21,74 @@ const DUMMY_TWO = fileURLToPath(
2021

2122
// Task used in most tests
2223
export const main = () =>
23-
gulp
24-
.src(DUMMY, { buffer })
25-
.pipe(stream(() => command, opts))
26-
.pipe(through.obj(execVinyl))
24+
pipeline(
25+
gulp.src(DUMMY, { buffer }),
26+
stream(() => command, opts),
27+
through.obj(execVinyl),
28+
)
2729

2830
// `input` should be an async function
2931
export const inputAsync = () =>
30-
gulp
31-
.src(DUMMY, { buffer })
32-
.pipe(stream(() => Promise.resolve(command), opts))
33-
.pipe(through.obj(execVinyl))
32+
pipeline(
33+
gulp.src(DUMMY, { buffer }),
34+
stream(() => Promise.resolve(command), opts),
35+
through.obj(execVinyl),
36+
)
3437

3538
// `input` should be fired with the Vinyl file
3639
export const inputFile = () =>
37-
gulp
38-
.src(DUMMY, { buffer })
39-
.pipe(stream(({ basename }) => `${command} ${basename}`, opts))
40-
.pipe(through.obj(execVinyl))
40+
pipeline(
41+
gulp.src(DUMMY, { buffer }),
42+
stream(({ basename }) => `${command} ${basename}`, opts),
43+
through.obj(execVinyl),
44+
)
4145

4246
const noop = () => {}
4347

4448
// File should be skipped when returning a non-string
4549
export const inputUndefined = () =>
46-
gulp
47-
.src(DUMMY, { buffer })
48-
.pipe(stream(noop, opts))
49-
.pipe(through.obj(execVinyl))
50+
pipeline(
51+
gulp.src(DUMMY, { buffer }),
52+
stream(noop, opts),
53+
through.obj(execVinyl),
54+
)
5055

5156
// Should allow several files
5257
export const severalFiles = () =>
53-
gulp
54-
.src([DUMMY, DUMMY_TWO], { buffer })
55-
.pipe(stream(() => command, opts))
56-
.pipe(through.obj(execVinyl))
58+
pipeline(
59+
gulp.src([DUMMY, DUMMY_TWO], { buffer }),
60+
stream(() => command, opts),
61+
through.obj(execVinyl),
62+
)
5763

5864
// Should allow doing several times
5965
export const severalTimes = () =>
60-
gulp
61-
.src(DUMMY, { buffer })
62-
.pipe(stream(() => command, opts))
63-
.pipe(stream(() => command, opts))
64-
.pipe(through.obj(execVinyl))
66+
pipeline(
67+
gulp.src(DUMMY, { buffer }),
68+
stream(() => command, opts),
69+
stream(() => command, opts),
70+
through.obj(execVinyl),
71+
)
6572

6673
// `input` should be a function
6774
export const inputNotFunc = () =>
68-
gulp.src(DUMMY, { buffer }).pipe(stream(command, opts))
75+
pipeline(gulp.src(DUMMY, { buffer }), stream(command, opts))
6976

7077
// `input` exceptions should be propagated
7178
export const inputThrows = () =>
72-
gulp.src(DUMMY, { buffer }).pipe(
79+
pipeline(
80+
gulp.src(DUMMY, { buffer }),
7381
stream(() => {
7482
throw new Error('error')
7583
}, opts),
7684
)
7785

7886
// `input` async exceptions should be propagated
7987
export const inputThrowsAsync = () =>
80-
gulp
81-
.src(DUMMY, { buffer })
82-
.pipe(stream(() => Promise.reject(new Error('error')), opts))
88+
pipeline(
89+
gulp.src(DUMMY, { buffer }),
90+
stream(() => Promise.reject(new Error('error')), opts),
91+
)
8392

8493
const cExecVinyl = async (file) => {
8594
// When `file.contents` is a stream and an `error` event should be emitted,

src/helpers/methods.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ export const TASK_METHODS = [
1010
]
1111

1212
export const STREAM_METHODS = [
13-
// `gulp.src(...).pipe(stream(..., { result: 'replace' }))`
13+
// `pipeline(
14+
// gulp.src(...),
15+
// stream(..., { result: 'replace' }),
16+
// )`
1417
{ title: 'stream-buffer', method: 'stream' },
15-
// `gulp.src(..., { buffer: false }).pipe(stream(..., { result: 'replace' }))`
18+
// `pipeline(
19+
// gulp.src(..., { buffer: false }),
20+
// stream(..., { result: 'replace' }),
21+
// )`
1622
{ title: 'stream-stream', method: 'stream', buffer: false },
17-
// `gulp.src(...).pipe(stream(..., { result: 'save' }))`
23+
// `pipeline(
24+
// gulp.src(...),
25+
// stream(..., { result: 'save' }),
26+
// )`
1827
{ title: 'stream-save', method: 'stream', opts: { result: 'save' } },
1928
]
2029

src/main.d.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ type StreamOptions = Omit<
4040
*
4141
* @example
4242
* ```js
43+
* import { pipeline } from 'node:stream/promises'
44+
*
4345
* import gulp from 'gulp'
46+
* import { stream } from 'gulp-execa'
4447
* import through from 'through2'
4548
*
4649
* export const task = () =>
47-
* gulp
48-
* .src('*.js')
50+
* pipeline(
51+
* gulp.src('*.js'),
4952
* // Prints the number of lines of each file
50-
* .pipe(stream(({ path }) => `wc -l ${path}`, { result: 'save' }))
51-
* .pipe(
52-
* through.obj((file, encoding, func) => {
53-
* console.log(file.execa[0].stdout)
54-
* func(null, file)
55-
* }),
56-
* )
53+
* stream(({ path }) => `wc -l ${path}`, { result: 'save' }),
54+
* through.obj((file, encoding, func) => {
55+
* console.log(file.execa[0].stdout)
56+
* func(null, file)
57+
* }),
58+
* )
5759
* ```
5860
*/
5961
result: 'save' | 'replace'
@@ -65,22 +67,22 @@ type StreamOptions = Omit<
6567
*
6668
* @example
6769
* ```js
70+
* import { pipeline } from 'node:stream/promises'
71+
*
6872
* import gulp from 'gulp'
73+
* import { stream } from 'gulp-execa'
6974
* import through from 'through2'
7075
*
7176
* export const task = () =>
72-
* gulp
73-
* .src('*.js')
77+
* pipeline(
78+
* gulp.src('*.js'),
7479
* // Prints the number of lines of each file, including `stderr`
75-
* .pipe(
76-
* stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
77-
* )
78-
* .pipe(
79-
* through.obj((file, encoding, func) => {
80-
* console.log(file.contents.toString())
81-
* func(null, file)
82-
* }),
83-
* )
80+
* stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
81+
* through.obj((file, encoding, func) => {
82+
* console.log(file.contents.toString())
83+
* func(null, file)
84+
* }),
85+
* )
8486
* ```
8587
*/
8688
from: 'stdout' | 'stderr' | 'all'
@@ -137,13 +139,17 @@ export function task<CallOptions extends NonStreamOptions = object>(
137139
*
138140
* @example
139141
* ```js
142+
* import { pipeline } from 'node:stream/promises'
143+
*
140144
* import gulp from 'gulp'
145+
* import { stream } from 'gulp-execa'
141146
*
142147
* export const sort = () =>
143-
* gulp
144-
* .src('*.txt')
145-
* .pipe(stream(({ path }) => `sort ${path}`))
146-
* .pipe(gulp.dest('sorted'))
148+
* pipeline(
149+
* gulp.src('*.txt'),
150+
* stream(({ path }) => `sort ${path}`),
151+
* gulp.dest('sorted'),
152+
* )
147153
* ```
148154
*/
149155
export function stream(

src/main.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Readable, Transform } from 'node:stream'
22

3-
import { exec, task, stream, type Options } from 'gulp-execa'
4-
import { expectType, expectAssignable, expectNotAssignable } from 'tsd'
3+
import { exec, stream, task, type Options } from 'gulp-execa'
4+
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
55
// eslint-disable-next-line n/no-extraneous-import, @typescript-eslint/no-shadow
66
import type File from 'vinyl'
77

src/stream/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import through from 'through2-concurrent'
66
import { throwError } from '../error.js'
77
import { parseOpts } from '../options/main.js'
88

9-
import { getDefaultOpts, forcedOpts } from './options.js'
9+
import { forcedOpts, getDefaultOpts } from './options.js'
1010
import { setResult } from './result.js'
1111

1212
// Creates a stream that fires child processes on each file:
13-
// gulp.src(...).pipe(stream(({ path }) => `command ${path}`))
13+
// pipeline(gulp.src(...), stream(({ path }) => `command ${path}`))
1414
export const stream = (getInput, opts) => {
1515
validateGetInput(getInput)
1616

0 commit comments

Comments
 (0)