feat: add function for parsing arguments with quotes (#532)

* feat: add `parse` function

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* chore: remoev unused shell-quote from dependencies

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: update `context.test.ts` test

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
This commit is contained in:
Alfi Maulana
2024-11-26 17:43:30 +07:00
committed by GitHub
parent 73aae93ffb
commit 8f79315420
7 changed files with 43 additions and 372 deletions

View File

@@ -99,7 +99,7 @@ describe("get action context", () => {
{
name: "with additional options specified",
inputs: {
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON FOO="BAR BAZ"`,
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
},
expectedContext: {
configure: {
@@ -150,7 +150,7 @@ describe("get action context", () => {
"cxx-compiler": "clang++",
"c-flags": "-Werror -Wall\n-Wextra",
"cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic",
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON FOO="BAR BAZ"`,
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
args: `-Wdev -Wdeprecated\n--fresh --foo "bar baz"`,
"run-build": "true",
"build-args": `--target foo\n--parallel 8 --foo "bar baz"`,

View File

@@ -1,6 +1,6 @@
import { getInput } from "gha-utils";
import path from "node:path";
import { parse } from "shell-quote";
import { parse } from "./utils.js";
export interface Context {
sourceDir: string;

18
src/utils.ts Normal file
View File

@@ -0,0 +1,18 @@
const regex = /"([^"]*)"|'([^']*)'|`([^`]*)`|(\S+)/g;
/**
* Converts a space-separated string into a list of arguments.
*
* This function parses the provided string, which contains arguments separated by spaces and possibly enclosed in quotes, into a list of arguments.
*
* @param str - The space-separated string to parse.
* @returns A list of arguments.
*/
export function parse(str: string): string[] {
const args: string[] = [];
let match: RegExpExecArray | null;
while ((match = regex.exec(str)) !== null) {
args.push(match[1] ?? match[2] ?? match[3] ?? match[4]);
}
return args;
}