Skip to content

Commit

Permalink
Upgrade library to use ESM and UMD
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodp committed Dec 21, 2023
1 parent 5354e70 commit 5790937
Show file tree
Hide file tree
Showing 9 changed files with 5,350 additions and 4,865 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [10, 12, 14]
node-version: [16, 18, 20]
steps:
- uses: actions/checkout@v2
with:
Expand Down
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
node_modules
node_modules
index*.js
index.d.ts
*.mjs
*.map
.vscode
index.html
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
Useful for splitting a command to use with NodeJS' [child process](https://nodejs.org/api/child_process.html) methods, like [spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), [exec](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback), and [execFile](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback), as well with libs like [execa](https://github.com/sindresorhus/execa).

* No external dependencies.
* Unit-tested.
⚡ Just 0.4 KB uncompressed, no external dependencies.

🎯 Version `1.1`+ works with NodeJS, DenoJS and browsers. JavaScript and TypeScript.

## Installation

Expand All @@ -24,25 +24,29 @@ npm i split-cmd
### Splitting into an array

```js
var split = require( 'split-cmd' ).split;
var arr = split( 'git commit -m "some message with spaces"' );
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

```js
var splitToObject = require( 'split-cmd' ).splitToObject;
var obj = splitToObject( 'git commit -m "some message with spaces"' );
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

```js
const { splitToObject } = require( 'split-cmd' );
const execa = require( 'execa' );
import { splitToObject } from 'split-cmd';
import { execa } from 'execa';

// Executing a single command
const obj = splitToObject( 'echo "I see unicorns"' );
Expand Down Expand Up @@ -82,7 +86,7 @@ split( command: string ): string[]
* @param {string} command Command to split.
* @returns {object}
*/
splitToObject( command: string ): object
splitToObject( command: string ): { command?: string, args?: string[] }
```

## License
Expand Down
6 changes: 2 additions & 4 deletions __tests__/test.js → __tests__/test.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Run these tests with Jest

var lib = require( '..' );
const lib = require( '..' );

describe( 'split-cmd', function () {

Expand Down Expand Up @@ -122,4 +120,4 @@ describe( 'split-cmd', function () {

} );

} );
} );
56 changes: 1 addition & 55 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Split a command into an array.
*
* @example
* ```js
* const arr = split( 'git commit -m "some message with spaces"' );
* console.log( arr ); // [ "git", "commit", "-m", "some message with spaces" ]
* ```
*
* @param {string} command Command to split.
* @returns {Array}
*/
export function split( command: string ): string[] {

if ( typeof command !== 'string' ) {
throw new Error( 'Command must be a string' );
}

const r = command.match( /[^"\s]+|"(?:\\"|[^"])*"/g );
if ( ! r ) {
return [];
}

return r.map( function ( expr ) {
const isQuoted = expr.charAt( 0 ) === '"' && expr.charAt( expr.length - 1 ) === '"';
return isQuoted ? expr.slice( 1, -1 ) : expr;
} );
}

/**
* Split a command into an object with the attributes `command` and `args`.
*
* @example
* ```js
* 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" ]
* ```
*
* @param {string} command Command to split.
* @returns {object}
*/
export function splitToObject( command: string ): { command?: string, args?: string[] } {
const cmds = split( command );
switch( cmds.length ) {
case 0: return {};
case 1: return { command: cmds[ 0 ] };
default: {
const first = cmds[ 0 ];
cmds.shift();
return { command: first, args: cmds };
}
}
}
Loading

0 comments on commit 5790937

Please sign in to comment.