Git Product home page Git Product logo

gulp-run's Introduction

gulp-run

Use shell commands in your gulp or vinyl pipeline.

Many command line interfaces are built around the idea of piping. Let's take advantage of that in our Gulp pipeline! To use gulp-run, simply tell it the command to process your files; gulp-run accepts any command you could write into your shell, including I/O redirection like python < baz.py | cat foo.txt - bar.txt. Additionally, node_modules/.bin is included on the path, so you can call programs supplied by your installed packages. Supports Unix and Windows.

This plugin is inspired by gulp-shell and gulp-spawn and attempts to improve upon their great work.

Usage

var run = require('gulp-run');


// Use gulp-run to start a pipeline
gulp.task('hello-world', function () {
  run('echo Hello World').exec()  // prints "Hello World\n".
    .pipe(gulp.dest('output'))    // Writes "Hello World\n" to output/echo.
})


// Use gulp-run in the middle of a pipeline:
gulp.task('even-lines', function () {
  gulp.src('path/to/input/*')           // Get input files.
    .pipe(run('awk "NR % 2 == 0"'))     // Use awk to extract the even lines.
    .pipe(gulp.dest('path/to/output'))  // Profit.
});


// Use gulp-run without gulp
var cmd = new run.Command('cat');  // Create a command object for `cat`.
cmd.exec('hello world');           // Call `cat` with 'hello world' on stdin.

API

run(template, [options])

Creates a Vinyl (gulp) stream that transforms its input by piping it to a shell command.

See run.Command for a description of the arguments.

Returns

(stream.Transform in Object Mode): Returns a Transform stream that receives Vinyl files. For each input, a subprocess is started taking the contents of the input on stdin. A new file is pushed downstream containing the process's stdout.

Example

gulp.task('even-lines', function () {
  gulp.src('path/to/input/*')           // Get input files.
    .pipe(run('awk "NR % 2 == 0"'))     // Use awk to extract the even lines.
    .pipe(gulp.dest('path/to/output'))  // Profit.
})

run(...).exec([stdin], [callback])

Start a gulp pipeline and execute the command immediately, pushing the results downstream.

Arguments

  1. [stdin] (String | Buffer | Vinyl): If given, this will be used as stdin for the command.

  2. [callback] (Function): The callback is called once the command has exited. An Error is passed if the exit status was non-zero. The error will have a status property set to the exit status.

Returns

(Stream.Readable in Object Mode): Returns a Vinyl (gulp) stream which will push downstream the stdout of the command as a Vinyl file. The default path of the Vinyl file is the first word of the template; use gulp-rename for more versatility.

Example

gulp.task('hello-world', function () {
  run('echo Hello World').exec()  // prints "[echo] Hello World\n".
    .pipe(gulp.dest('output'))    // Writes "Hello World\n" to output/echo.
})

new run.Command(template, [options])

Represents a command to be run in a subshell.

Arguments

  1. template (String): The command to run. It can be a template interpolating the variable file which references the Vinyl file being input. The template need not interpolate anything; a simple shell command will do just fine. The command is passed as an argument to sh -c, so I/O redirection and the like will work as you would expect from a terminal.

  2. options (Object):

    • env (Object): The environmental variables for the child process. Defaults to process.env.
    • cwd (String): The initial working directory for the child process. Defaults to process.cwd().
    • silent (Boolean): If true, do not print the command's output. This is the same as setting verbosity to 1. Defaults to false.
    • verbosity (Number): Sets the verbosity level. Defaults to 2.
      • 0: Do not print anything, ever.
      • 1: Print the command being run and its stderr.
      • 2: Print the command, its stderr, and its stdout.
      • 3: Print the command, its stderr, and its stdout progressivly. Not useful if you have concurrent gulp-run instances, as the outputs may get mixed.
    • usePowerShell (Boolean): Windows only. If true uses the PowerShell instead of cmd.exe for command execution.

run.Command#exec([stdin], [callback])

Spawn a subshell and execute the command.

Arguments

  1. [stdin] (String | Buffer | Vinyl): If given, this will be used as stdin for the command.

  2. [callback] (Function): The callback is called once the command has exited. An Error is passed if the exit status was non-zero. The error will have a status property set to the exit status.

Returns

(Vinyl): Returns a Vinyl file wrapping the stdout of the command.

Example

var cmd = new run.Command('cat');  // Create a command object for `cat`.
cmd.exec('hello world');           // Call `cat` with 'hello world' on stdin.

The ISC License

Copyright (c) 2014 Chris Barrick [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

gulp-run's People

Contributors

cbarrick avatar leipert avatar lichunqiang avatar

Watchers

 avatar

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.