-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
5,350 additions
and
4,865 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} | ||
} |
Oops, something went wrong.