fix: fix matchGitArgs (#174)
* fix: fix matchGitArgs * fix: improve matchGitArgs * chore: add ref in JSDoc
This commit is contained in:
21
src/util.ts
21
src/util.ts
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user