1.6.3 • Published 6 years ago

executive v1.6.3

Weekly downloads
1,184
License
MIT
Repository
github
Last release
6 years ago

executive

npm build dependencies downloads license chat

An elegant child_process.spawn

Executive is simple and intuitive interface to child_process.spawn with zero depdencies. Built-in support for async and sync process creation, built-in flow control and automatic shell make working with external processes in Node easy.

Features

  • Async callback, promise and sync APIs
  • Automatically pipes stderr and stdout by default
  • Automatically uses shell when commands use builtins, globs or operators
  • Built-in control flow with support for parallel and serial execution
  • Mix simple string commands with functions and promises returning commands
  • Multi-line strings parsed as multiple commands and executed sequentially
  • Streams stderr and stdout rather than blocking on command completion
  • Included TypeScript type definition
  • Improved Windows support
  • No external dependencies

Install

$ npm install executive --save-dev

Usage

No need to echo as stderr and stdout are piped by default.

import exec from 'executive'

exec('uglifyjs foo.js --compress --mangle > foo.min.js')

It's easy to be quiet too.

exec.quiet('uglifyjs foo.js --compress --mangle > foo.min.js')

Callbacks and promises are both supported.

exec('ls', (err, stdout, stderr) => console.log(stdout))
exec('ls').then(res => console.log(res.stdout))

Automatically serializes commands.

exec(['ls', 'ls', 'ls']) // All three ls commands will be executed in order

exec(`ls -l
      ls -lh
      ls -lha`) // Also executed in order

Want to execute your commands in parallel? No problem.

exec.parallel(['ls', 'ls', 'ls'])

Want to collect individual results? Easy.

{a, b, c} = await exec.parallel({
  a: 'echo a',
  b: 'echo b',
  c: 'echo c'
})

Want to blend in Promises or pure functions? You got it.

exec.parallel([
  'ls',

  // Promises can be blended directly in
  exec('ls'),

  // Promises returned by functions are automatically consumed
  () => exec('ls'),

  // Functions which return a string are assumed to be commands
  () => 'ls',

  // Functions and promises can return objects with stdout, stderr or status
  () => ({ stdout: 'huzzah', stderr: '', status: 0 }),

  'ls'
])

Options

Options are passed as the second argument to exec. Helper methods for quiet, interactive, parallel and sync do what you expect.

exec('ls', { options: 'quiet' })

and

exec.quiet('ls')

are equivalent.

options.interactive | exec.interactive

default false

If you need to interact with a program (your favorite text editor for instance) or watch the output of a long running process (tail -f), or just don't care about checking stderr and stdout, set interactive to true:

exec.interactive('vim', err => {
  // Edit your commit message
})

options.quiet | exec.quiet

default false

If you'd prefer not to pipe stdout and stderr set quiet to true:

exec.quiet(['ls', 'ls'], (err, stdout, stderr) => {
  // You can still inspect stdout, stderr of course
})

options.sync | exec.sync

default false

Blocking version of exec. Returns {stdout, stderr} or throws an error.

options.parallel | exec.parallel

default false

Uses parallel rather than serial execution of commands.

options.shell

default null

Force a shell to be used for command execution.

options.strict

default false

Any non-zero exit status is treated as an error. Promises will be rejected and an error will be thrown with exec.sync if syncThrows is enabled.

options.syncThrows

default false

Will cause exec.sync to throw errors rather than returning them.

Extra

Great with sake, grunt, gulp and other task runners. Even nicer with async and await.

Fancy example using sake:

task('package', 'Package project', => {
  // Create dist folder
  await exec(`
    mkdir -p dist/
    rm   -rf dist/*
  `)

  // Copy assets to dist in parallel
  await exec.parallel(`
    cp manifest.json dist/
    cp -rf assets/   dist/
    cp -rf lib/      dist/
    cp -rf views/    dist/
  `)

  // Get current git commit hash
  let {stdout} = await exec('git rev-parse HEAD')
  let hash     = stdout.substring(0, 8)

  # Zip up dist
  exec(`zip -r package-${hash}.zip dist/`)
})

You can find more usage examples in the tests.

License

MIT

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.5.13

7 years ago

1.5.12

7 years ago

1.5.11

8 years ago

1.5.10

8 years ago

1.5.9

8 years ago

1.5.8

8 years ago

1.5.7

8 years ago

1.5.6

8 years ago

1.5.5

8 years ago

1.5.4

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.7

8 years ago

1.2.6

8 years ago

1.2.5

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.11

8 years ago

1.1.10

8 years ago

1.1.9

8 years ago

1.1.8

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.8

9 years ago

0.4.7

11 years ago

0.4.6

11 years ago

0.4.5

11 years ago

0.4.4

11 years ago

0.4.3

11 years ago

0.4.2

11 years ago

0.4.1

11 years ago

0.4.0

11 years ago

0.3.4

11 years ago

0.3.3

11 years ago

0.3.2

11 years ago

0.3.1

11 years ago

0.3.0

12 years ago

0.2.8

12 years ago

0.2.6

12 years ago

0.2.5

12 years ago

0.2.4

12 years ago

0.2.3

12 years ago

0.2.2

12 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.14

12 years ago

0.1.13

12 years ago

0.1.12

12 years ago

0.1.11

12 years ago

0.1.10

12 years ago

0.1.9

12 years ago

0.1.8

12 years ago

0.1.7

12 years ago

0.1.6

12 years ago

0.1.5

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago

0.0.0

12 years ago