Code Monkey home page Code Monkey logo

gulp-execa's Introduction

gulp-execa logo

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 gulp from 'gulp'
import { task, exec, stream } from 'gulp-execa'

export const audit = task('npm audit')

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

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

Demo

You can try this library:

Install

npm install -D gulp-execa

This plugin requires Gulp 4 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 gulp from 'gulp'
import { stream } from 'gulp-execa'

export const sort = () =>
  gulp
    .src('*.txt')
    .pipe(stream(({ path }) => `sort ${path}`))
    .pipe(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: true for task() and exec(), false for stream().

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 gulp from 'gulp'
import { stream } from 'gulp-execa'
import through from 'through2'

export const task = () =>
  gulp
    .src('*.js')
    // Prints the number of lines of each file
    .pipe(stream(({ path }) => `wc -l ${path}`, { result: 'save' }))
    .pipe(
      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 gulp from 'gulp'
import { stream } from 'gulp-execa'
import through from 'through2'

export const task = () =>
  gulp
    .src('*.js')
    // Prints the number of lines of each file, including `stderr`
    .pipe(
      stream(({ path }) => `wc -l ${path}`, { result: 'replace', from: 'all' }),
    )
    .pipe(
      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:

ehmicky
ehmicky

๐Ÿ’ป ๐ŸŽจ ๐Ÿค” ๐Ÿ“–
Jonathan Haines
Jonathan Haines

๐Ÿ›

gulp-execa's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar ehmicky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gulp-execa's Issues

Error in plugin "gulp-execa"

Hi,

I am having a test and build error when running gulp-execa on Arch Linux:

src/preload.js โ†’ app/preload.js...
[!] Error: Could not resolve './preload/spellChecking' from src/preload.js
Error: Could not resolve './preload/spellChecking' from src/preload.js
    at error (/home/franck/Documents/Development/Build/Rocket.Chat.Electron/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at ModuleLoader.handleResolveId (/home/franck/Documents/Development/Build/Rocket.Chat.Electronnode_modules/rollup/dist/shared/node-entry.js:12389:24)
    at ModuleLoader.<anonymous> (/home/franck/Documents/Development/Build/Rocket.Chat.Electron/node_modules/rollup/dist/shared/node-entry.js:12277:30)
    at Generator.next (<anonymous>)
    at fulfilled (/home/franck/Documents/Development/Build/Rocket.Chat.Electron/node_modules/rollup/dist/shared/node-entry.js:38:28)

[00:36:35] 'build:bundle' errored after 5.14 s
[00:36:35] Error in plugin "gulp-execa"
Message:
    Command failed with exit code 1: rollup -c
Stack:
Error: Command failed with exit code 1: rollup -c
    at makeError (/home/franck/Documents/Development/Build/Rocket.Chat.Electron/node_modules/execa/lib/error.js:58:11)
    at handlePromise (/home/franck/Documents/Development/Build/Rocket.Chat.Electron/node_modules/execa/index.js:114:26)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
[00:36:35] 'build' errored after 5.15 s
[00:36:35] 'start' errored after 5.15 s
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Execa works fine on Mac OSX and Windows 10, it seems to be related only to linux.

Thanks for looking into this,
Kind regards

npm install failing on github repository

Running npm install gulp-execa is hanging while looking for sindresorhus/execa

Changing the reference to

"execa": "https://github.com/sindresorhus/execa",

fixes the issue, so it may be a problem with my local environment, or git setup. It could also be an issue with the npm cli. Or even github itseful.

I'm not entirely sure where the problem is, so I'm starting by opening a pull request here

Steps to reproduce

Run npm install --loglevel=silly gulp-execa in an npm project

The install gets stuck trying to

git ls-remote -h -t git://github.com/sindresorhus/execa.git

npm sill pacote Retrying git command: ls-remote -h -t git://github.com/sindresorhus/execa.git attempt # 2
npm sill pacote Retrying git command: ls-remote -h -t git://github.com/sindresorhus/execa.git attempt # 3

Expected behavior

npm install gulp-execa should install successfully

Environment

  System:
    OS: macOS 10.14.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 554.00 MB / 16.00 GB
    Shell: 5.6.2 - /usr/local/bin/zsh
  Binaries:
    Node: 10.15.0 - ~/.asdf/installs/nodejs/10.15.0/bin/node
    Yarn: 1.16.0 - ~/.asdf/installs/nodejs/10.15.0/.npm/bin/yarn
    npm: 6.4.1 - ~/.asdf/installs/nodejs/10.15.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 74.0.3729.169
    Firefox: 66.0.5
    Safari: 12.0.3
    Safari Technology Preview: 13.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.