fix: support quote wrapping in append-data

This commit is contained in:
Chen Asraf
2023-04-24 12:57:16 +03:00
committed by Chen Asraf
parent 222e1a04ca
commit 4fecca8483
4 changed files with 15 additions and 3 deletions

View File

@@ -5,7 +5,6 @@ import { LogLevel, ScaffoldCmdConfig } from "./types"
import { Scaffold } from "./scaffold"
import path from "path"
import fs from "fs/promises"
import { OptionsBase } from "massarg/types"
import { parseAppendData } from "./utils"
export async function parseCliArgs(args = process.argv.slice(2)) {
@@ -13,7 +12,11 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
return (
massarg<ScaffoldCmdConfig>()
.main(Scaffold)
.main((config) => {
config.data = { ...config.data, ...config.appendData }
delete config.appendData
return Scaffold(config)
})
.option({
name: "name",
aliases: ["n"],

View File

@@ -331,6 +331,7 @@ export interface ScaffoldCmdConfig {
output: string
createSubFolder: boolean
data?: Record<string, string>
appendData?: Record<string, string>
overwrite: boolean
quiet: boolean
verbose: LogLevel

View File

@@ -389,5 +389,9 @@ export function parseAppendData(value: string, options: ScaffoldCmdConfig & Opti
if (value.includes(":=") && !val.includes(":=")) {
return { ...data, [key]: JSON.parse(val) }
}
return { ...data, [key]: val }
return { ...data, [key]: isWrappedWithQuotes(val) ? val.substring(1, val.length - 1) : val }
}
function isWrappedWithQuotes(string: string): boolean {
return (string.startsWith('"') && string.endsWith('"')) || (string.startsWith("'") && string.endsWith("'"))
}

View File

@@ -116,5 +116,9 @@ describe("Utils", () => {
test("overwrites existing value", () => {
expect(parseAppendData("name:=123", blankCliConf)).toEqual({ name: 123 })
})
test("works with quotes", () => {
expect(parseAppendData('key="value test"', blankCliConf)).toEqual({ key: "value test", name: "test" })
})
})
})