fix: fix matchGitArgs (#174)

* fix: fix matchGitArgs

* fix: improve matchGitArgs

* chore: add ref in JSDoc
This commit is contained in:
Federico Grandi
2021-04-16 08:15:25 +02:00
committed by GitHub
parent 6d400479e9
commit bc9a4cf987
4 changed files with 514 additions and 26 deletions

View File

@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import matchAll from 'string.prototype.matchall'
export type Input =
| 'add'
@@ -31,15 +32,31 @@ export function log(err: any | Error, data?: any) {
/**
* Matches the different pathspecs and arguments by removing spaces that are not inside quotes
* {@link https://stackoverflow.com/a/67103621/7133466}
* @example
* ```js
* matchGitArgs(' first second "third" \'fourth\'') => [ 'first', 'second', '"third"', "'fourth'" ]
* matchGitArgs(`--message "This is a 'quoted' message" --other 'This uses the "other" quotes' --foo 1234`) => ["--message", "This is a 'quoted' message", "--other", "This uses the \"other\" quotes", "--foo", "1234"]
* matchGitArgs(' ') => [ ]
* ```
* @returns An array, if there's no match it'll be empty
*/
export function matchGitArgs(string: string) {
return string.match(/(?:[^\s"]+|"[^"]*")+/g) || []
const tokens = String.raw`
(?<option> --\w+)
| (' (?<sq> (\\. | [^'])* ) ')
| (" (?<dq> (\\. | [^"])* ) ")
| (?<raw> \S+)
`
const re = tokens.replace(/\s+/g, '')
const result: string[] = []
for (const m of matchAll(string, re)) {
result.push(...Object.values(m.groups || {}).filter(Boolean))
}
return result
}
export function parseBool(value: any) {