Socket
Socket
Sign inDemoInstall

split-cmd

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    split-cmd

💦Split a command into an array or an object


Version published
Maintainers
1
Created

Readme

Source

split-cmd

💦 Split a command into an array or an object

Useful for splitting a command to use with NodeJS' child process methods, like spawn, exec, and execFile, as well with libs like execa. No external dependencies.

Install

npm install --save split-cmd

Examples

Splitting into an array

var split = require( 'split-cmd' ).split;
var arr = split( 'git commit -m "some message with spaces"' );
console.log( arr ); // [ "git", "commit", "-m", "some message with spaces" ]

Splitting into an object

var splitToObject = require( 'split-cmd' ).splitToObject;
var obj = splitToObject( 'git commit -m "some message with spaces"' );
console.log( obj.command ); // git
console.log( obj.args ); // [ "commit", "-m", "some message with spaces" ]

Using it with execa

const { splitToObject } = require( 'split-cmd' );
const execa = require( 'execa' );

// Executing a single command
const obj = splitToObject( 'echo "I see unicorns"' );
execa( obj.command, obj.args )
    .then( result => console.log( result.stdout ) ) // I see unicorns
    .catch( error => console.log( error ) );

// Executing multiple commands in batch
[
    'echo "I see unicorns"',
    'mkdir foo',
    'touch foo/bar.txt'
]
.map( s => splitToObject( s ) )
.forEach( obj => {
    execa( obj.command, obj.args )
        .then( result => console.log( result.stdout ) )
        .catch( error => console.log( error ) );
} );

API

/**
 * Split a command into an array.
 *
 * @param {string} command Command to split.
 * @returns {Array}
 */
split( command: string ): string[]

/**
 * Split a command into an object with the attributes `command` and `args`.
 *
 *
 * @param {string} command Command to split.
 * @returns {object}
 */
splitToObject( command: string ): object

License

MIT © Thiago Delgado Pinto

Keywords

FAQs

Last updated on 11 Jun 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc