ProductPromotion
Logo

Cross.Platform

made by https://0x3d.site

GitHub - ehmicky/gulp-execa: Gulp.js command execution for humans
Gulp.js command execution for humans. Contribute to ehmicky/gulp-execa development by creating an account on GitHub.
Visit Site

GitHub - ehmicky/gulp-execa: Gulp.js command execution for humans

GitHub - ehmicky/gulp-execa: Gulp.js command execution for humans

Node TypeScript Codecov Mastodon Medium

Gulp.js command execution for humans.

As opposed to similar plugins or to child_process.exec(), this uses Execa which provides:

gulp-execa adds Gulp-specific features to Execa including:

Commands can be executed either directly or inside a files stream. In streaming mode, unlike other libraries:

Hire me

Please reach out if you're looking for a Node.js API or CLI engineer (11 years of experience). Most recently I have been Netlify Build's and Netlify Plugins' technical lead for 2.5 years. I am available for full-time remote positions.

Example

gulpfile.js:

import { pipeline } from 'node:stream/promises'

import gulp from 'gulp'
import { exec, stream, task } from 'gulp-execa'

export const audit = task('npm audit')

export const outdated = async () => {
  await exec('npm outdated')
}

export const sort = () =>
  pipeline(
    gulp.src('*.txt'),
    stream(({ path }) => `sort ${path}`),
    gulp.dest('sorted'),
  )

Install

npm install -D gulp-execa

This plugin requires Gulp 5 and Node.js >=18.18.0. It is an ES module and must be loaded using an import or import() statement, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.

Methods

task(command, [options])

Returns a Gulp task that executes command.

import { task } from 'gulp-execa'

export const audit = task('npm audit')

exec(command, [options])

Executes command. The return value is both a promise and a child_process instance.

The promise will be resolved with the command result. If the command failed, the promise will be rejected with a nice error. If the reject: false option was used, the promise will be resolved with that error instead.

import { exec } from 'gulp-execa'

export const outdated = async () => {
  await exec('npm outdated')
}

stream(function, [options])

Returns a stream that executes a command on each input file.

function must:

  • take a Vinyl file as argument. The most useful property is file.path but other properties are available as well.
  • return either:
    • a command string
    • an options object with a command property
    • undefined
import { pipeline } from 'node:stream/promises'

import gulp from 'gulp'
import { stream } from 'gulp-execa'

export const sort = () =>
  pipeline(
    gulp.src('*.txt'),
    stream(({ path }) => `sort ${path}`),
    gulp.dest('sorted'),
  )

Each file in the stream will spawn a separate process. This can consume lots of resources so you should only use this method when there are no alternatives such as:

  • firing a command programmatically instead of spawning a child process
  • passing several files, a directory or a globbing pattern as arguments to the command

The debug, stdout, stderr, all and stdio options cannot be used with this method.

Command

By default no shell interpreter (like Bash or cmd.exe) is used. This means command must be just the program and its arguments. No escaping/quoting is needed, except for significant spaces (with a backslash).

Shell features such as globbing, variables and operators (like && > ;) should not be used. All of this can be done directly in Node.js instead.

Shell interpreters are slower, less secure and less cross-platform. However, you can still opt-in to using them with the shell option.

import { writeFileStream } from 'node:fs'

import gulp from 'gulp'
import { task } from 'gulp-execa'

// Wrong
// export const check = task('npm audit && npm outdated')

// Correct
export const check = gulp.series(task('npm audit'), task('npm outdated'))

// Wrong
// export const install = task('npm install > log.txt')

// Correct
export const install = task('npm install', {
  stdout: writeFileStream('log.txt'),
})

Options

options is an optional object.

All Execa options can be used. Please refer to its documentation for a list of possible options.

The following options are available as well.

echo

Type: boolean
Default: debug option's value

Whether the command should be printed on the console.

$ gulp audit
[13:09:39] Using gulpfile ~/code/gulpfile.js
[13:09:39] Starting 'audit'...
[13:09:39] [gulp-execa] npm audit
[13:09:44] Finished 'audit' after 4.96 s

debug

Type: boolean
Default: true for task() and exec(), false for stream().

Whether both the command and its output (stdout/stderr) should be printed on the console instead of being returned in JavaScript.

$ gulp audit
[13:09:39] Using gulpfile ~/code/gulpfile.js
[13:09:39] Starting 'audit'...
[13:09:39] [gulp-execa] npm audit

                        == npm audit security report ===

found 0 vulnerabilities
 in 27282 scanned packages
[13:09:44] Finished 'audit' after 4.96 s

result

Type: string
Value: 'replace' or 'save'
Default: 'replace'

With stream(), whether the command result should:

  • replace the file's contents
  • save: be pushed to the file.execa array property
import { pipeline } from 'node:stream/promises'

import gulp from 'gulp'
import { stream } from 'gulp-execa'
import through from 'through2'

export const task = () =>
  pipeline(
    gulp.src('*.js'),
    // Prints the number of lines of each file
    stream(({ path }) => `wc -l ${path}`, { result: 'save' }),
    through.obj((file, encoding, func) => {
      console.log(file.execa[0].stdout)
      func(null, file)
    }),
  )

from

Type: string
Value: 'stdout', 'stderr' or 'all'
Default: 'stdout'

Which output stream to use with result: 'replace'.

import { pipeline } from 'node:stream/promises'

import gulp from 'gulp'
import { stream } from 'gulp-execa'
import through from 'through2'

export const task = () =>
  pipeline(
    gulp.src('*.js'),
    // Prints the number of lines of each file, including `stderr`
    stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
    through.obj((file, encoding, func) => {
      console.log(file.contents.toString())
      func(null, file)
    }),
  )

maxConcurrency

Type: integer
Default: 100

With stream(), how many commands to run in parallel at once.

See also

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

Thanks go to our wonderful contributors:

Articles
to learn more about the cross-platform concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Cross-Platform.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory