Code Monkey home page Code Monkey logo

split-cmd's Introduction

npm (tag) Build Status License npm

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 ); // [ "git", "commit", "-m", "some message with spaces" ]

Splitting into an object

import { splitToObject } from 'split-cmd';

const 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

import { splitToObject } from 'split-cmd';
import { execa } from '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 ): { command?: string, args?: string[] }

License

MIT © Thiago Delgado Pinto

split-cmd's People

Contributors

dependabot[bot] avatar thiagodp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

clevercanyon

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.