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.
⚡ Just 0.4 KB uncompressed, no external dependencies.
🎯 Version 1.1
+ works with NodeJS, DenoJS and browsers. JavaScript and TypeScript.
Installation
npm i split-cmd
Usage
Splitting into an array
import { split } from 'split-cmd';
const arr = split( 'git commit -m "some message with spaces"' );
console.log( arr );
Splitting into an object
import { splitToObject } from 'split-cmd';
const obj = splitToObject( 'git commit -m "some message with spaces"' );
console.log( obj.command );
console.log( obj.args );
Using it with execa
import { splitToObject } from 'split-cmd';
import { execa } from 'execa';
const obj = splitToObject( 'echo "I see unicorns"' );
execa( obj.command, obj.args )
.then( result => console.log( result.stdout ) )
.catch( error => console.log( error ) );
[
'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( command: string ): string[]
splitToObject( command: string ): { command?: string, args?: string[] }
License
MIT © Thiago Delgado Pinto