mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-12-17 00:44:35 +08:00
统一格式化参数的方法
This commit is contained in:
@@ -85,7 +85,7 @@ const customComponentGuide = {
|
||||
`,
|
||||
objectCase: `
|
||||
// 对象参数的情况
|
||||
const formattedConfig = stringifyObject(config);
|
||||
const formattedConfig = stringifyArgv(config);
|
||||
return \`functionName(\${formattedConfig})\`;
|
||||
`,
|
||||
},
|
||||
@@ -182,7 +182,7 @@ const customComponentGuide = {
|
||||
`,
|
||||
},
|
||||
stringHandling: {
|
||||
stringifyObject: {
|
||||
stringifyArgv: {
|
||||
description: "将对象转换为代码字符串",
|
||||
usage: "用于生成包含对象参数的代码",
|
||||
example: "将 {key: 'value'} 转换为 '{key:\"value\"}'",
|
||||
@@ -246,12 +246,6 @@ const customComponentGuide = {
|
||||
args: "解析后的参数数组,根据路径规则处理变量格式",
|
||||
},
|
||||
},
|
||||
stringifyWithType: {
|
||||
description: "将带类型的值转换为字符串",
|
||||
usage: "用于处理 VariableInput 类型的值",
|
||||
example:
|
||||
"将 {value: 'text', isString: true, __varInputVal__: true,} 转换为 '\"text\"'",
|
||||
},
|
||||
parseToHasType: {
|
||||
description: "将字符串解析为带类型的值",
|
||||
usage: "用于解析 VariableInput 类型的值",
|
||||
|
||||
@@ -1,26 +1,12 @@
|
||||
import { parse } from "@babel/parser";
|
||||
|
||||
/**
|
||||
* 根据值的类型和属性将其转换为字符串
|
||||
* 1. 对于带有 __varInputVal__ 属性的对象,根据该属性决定是否添加引号
|
||||
* 2. 对于普通字符串,自动添加引号
|
||||
* 3. 对于其他类型(数字、布尔等),直接转换
|
||||
* @param {Object|string|number|boolean} argv 要转换的值
|
||||
* @returns {string} 转换后的字符串
|
||||
* 处理带有 __varInputVal__ 属性的对象
|
||||
* @param {Object} argv 要处理的对象
|
||||
* @returns {string} 处理后的字符串
|
||||
*/
|
||||
export const stringifyWithType = (argv) => {
|
||||
// 处理带有类型标记的对象
|
||||
if (typeof argv === "object" && argv.hasOwnProperty("__varInputVal__")) {
|
||||
return argv.isString ? `"${argv.value}"` : argv.value;
|
||||
}
|
||||
|
||||
// 处理普通字符串
|
||||
if (typeof argv === "string") {
|
||||
return `"${argv}"`;
|
||||
}
|
||||
|
||||
// 处理其他类型
|
||||
return argv;
|
||||
const stringifyVarInputVal = (argv) => {
|
||||
return argv.isString ? `"${argv.value}"` : argv.value;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -51,9 +37,9 @@ const removeEmptyValues = (obj) => {
|
||||
const processObject = (obj, parentPath = "") => {
|
||||
// 移除空值
|
||||
obj = removeEmptyValues(obj);
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
|
||||
if (obj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyWithType(obj);
|
||||
return stringifyVarInputVal(obj);
|
||||
}
|
||||
|
||||
let result = "{\n";
|
||||
@@ -64,16 +50,18 @@ const processObject = (obj, parentPath = "") => {
|
||||
|
||||
// 处理对象类型
|
||||
if (value && typeof value === "object") {
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
|
||||
if (value.hasOwnProperty("__varInputVal__")) {
|
||||
valueStr = stringifyWithType(value);
|
||||
valueStr = stringifyVarInputVal(value);
|
||||
} else {
|
||||
valueStr = processObject(value, parentPath + " ");
|
||||
}
|
||||
}
|
||||
// 处理其他类型
|
||||
else {
|
||||
valueStr = stringifyWithType(value);
|
||||
else if (value && typeof value === "string") {
|
||||
valueStr = `"${value}"`;
|
||||
} else {
|
||||
valueStr = value;
|
||||
}
|
||||
|
||||
// 添加缩进
|
||||
@@ -94,13 +82,13 @@ const processObject = (obj, parentPath = "") => {
|
||||
* @param {Object} jsonObj 要格式化的对象
|
||||
* @returns {string} 格式化后的JSON字符串
|
||||
*/
|
||||
export const stringifyObject = (jsonObj) => {
|
||||
const stringifyObject = (jsonObj) => {
|
||||
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyVarInputVal(jsonObj);
|
||||
}
|
||||
if (jsonObj instanceof Array) {
|
||||
return `[${jsonObj.map((item) => stringifyObject(item)).join(",")}]`;
|
||||
}
|
||||
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyWithType(jsonObj);
|
||||
}
|
||||
try {
|
||||
return processObject(jsonObj);
|
||||
} catch (e) {
|
||||
@@ -109,6 +97,24 @@ export const stringifyObject = (jsonObj) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化参数为字符串
|
||||
* @param {Object|string|number|boolean} argv 要格式化的参数
|
||||
* @returns {string} 格式化后的字符串
|
||||
*/
|
||||
export const stringifyArgv = (argv) => {
|
||||
// 处理普通字符串
|
||||
if (typeof argv === "string") {
|
||||
return `"${argv}"`;
|
||||
}
|
||||
// 处理对象
|
||||
if (typeof argv === "object") {
|
||||
return stringifyObject(argv);
|
||||
}
|
||||
// 处理其他类型
|
||||
return argv;
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析字符串为variableInput对象
|
||||
* @param {string} str 要解析的字符串
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
* @param {Array} selectedActions 已选择的操作列表
|
||||
* @returns {string} 生成的代码
|
||||
*/
|
||||
import { stringifyObject, stringifyWithType } from "./formatString";
|
||||
import { stringifyArgv } from "./formatString";
|
||||
|
||||
// 生成 goto 参数字符串
|
||||
function generateGotoArgs(goto) {
|
||||
const args = [];
|
||||
|
||||
// URL
|
||||
const urlStr = stringifyWithType(goto.url);
|
||||
const urlStr = stringifyArgv(goto.url);
|
||||
args.push(urlStr);
|
||||
|
||||
// Headers
|
||||
@@ -24,7 +24,7 @@ function generateGotoArgs(goto) {
|
||||
headers.userAgent = goto.headers.userAgent;
|
||||
}
|
||||
console.log("Headers:", JSON.stringify(headers, null, 2));
|
||||
args.push(stringifyObject(headers, true));
|
||||
args.push(stringifyArgv(headers, true));
|
||||
}
|
||||
|
||||
// Timeout
|
||||
@@ -121,7 +121,7 @@ function generateRunArgs(run) {
|
||||
if (run.proxy) options.proxy = run.proxy;
|
||||
if (run.viewport) options.viewport = run.viewport;
|
||||
|
||||
return Object.keys(options).length ? stringifyObject(options) : "";
|
||||
return Object.keys(options).length ? stringifyArgv(options) : "";
|
||||
}
|
||||
|
||||
// 生成操作参数字符串
|
||||
@@ -143,11 +143,11 @@ function generateActionArgs(action, config) {
|
||||
case "mousedown":
|
||||
case "mouseup":
|
||||
case "focus":
|
||||
result = stringifyWithType(config.selector);
|
||||
result = stringifyArgv(config.selector);
|
||||
break;
|
||||
case "css":
|
||||
case "paste":
|
||||
result = stringifyWithType(config.value);
|
||||
result = stringifyArgv(config.value);
|
||||
break;
|
||||
case "press":
|
||||
result = generatePressArgs(config);
|
||||
@@ -163,7 +163,7 @@ function generateActionArgs(action, config) {
|
||||
break;
|
||||
case "cookies":
|
||||
case "removeCookies":
|
||||
result = stringifyWithType(config.name);
|
||||
result = stringifyArgv(config.name);
|
||||
break;
|
||||
case "setCookies":
|
||||
result = generateSetCookiesArgs(config);
|
||||
@@ -190,7 +190,7 @@ function generateActionArgs(action, config) {
|
||||
result = generateDownloadArgs(config);
|
||||
break;
|
||||
case "devTools":
|
||||
result = stringifyWithType(config.mode);
|
||||
result = stringifyArgv(config.mode);
|
||||
break;
|
||||
default:
|
||||
result = "";
|
||||
@@ -208,7 +208,7 @@ function generateActionArgs(action, config) {
|
||||
function generateWaitArgs(config) {
|
||||
switch (config.type) {
|
||||
case "selector":
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
case "function":
|
||||
return config.function;
|
||||
case "time":
|
||||
@@ -220,7 +220,7 @@ function generateWaitArgs(config) {
|
||||
|
||||
// 生成 press 参数字符串
|
||||
function generatePressArgs(config) {
|
||||
const args = [stringifyWithType(config.key)];
|
||||
const args = [stringifyArgv(config.key)];
|
||||
if (config.modifiers?.length) {
|
||||
args.push(JSON.stringify(config.modifiers));
|
||||
}
|
||||
@@ -231,12 +231,12 @@ function generatePressArgs(config) {
|
||||
function generateScreenshotArgs(config) {
|
||||
const args = [];
|
||||
if (config.rect) {
|
||||
args.push(stringifyObject(config.rect));
|
||||
args.push(stringifyArgv(config.rect));
|
||||
} else if (config.selector) {
|
||||
args.push(stringifyWithType(config.selector));
|
||||
args.push(stringifyArgv(config.selector));
|
||||
}
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@@ -245,10 +245,10 @@ function generateScreenshotArgs(config) {
|
||||
function generatePdfArgs(config) {
|
||||
const args = [];
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
if (config.options) {
|
||||
args.push(stringifyObject(config.options));
|
||||
args.push(stringifyArgv(config.options));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@@ -256,26 +256,26 @@ function generatePdfArgs(config) {
|
||||
// 生成 device 参数字符串
|
||||
function generateDeviceArgs(config) {
|
||||
if (config.type === "preset") {
|
||||
return stringifyWithType(config.deviceName);
|
||||
return stringifyArgv(config.deviceName);
|
||||
} else {
|
||||
const options = {};
|
||||
if (config.size) options.size = config.size;
|
||||
if (config.useragent) options.userAgent = config.useragent;
|
||||
return stringifyObject(options);
|
||||
return stringifyArgv(options);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 setCookies 参数字符串
|
||||
function generateSetCookiesArgs(config) {
|
||||
if (!config.items?.length) return "[]";
|
||||
return stringifyObject(config.items);
|
||||
return stringifyArgv(config.items);
|
||||
}
|
||||
|
||||
// 生成 evaluate 参数字符串
|
||||
function generateEvaluateArgs(config) {
|
||||
const args = [config.function];
|
||||
if (config.args?.length) {
|
||||
args.push(...config.args.map(stringifyWithType));
|
||||
args.push(...config.args.map(stringifyArgv));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@@ -285,35 +285,35 @@ function generateWhenArgs(config) {
|
||||
if (config.type === "function") {
|
||||
return config.function;
|
||||
} else {
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 file 参数字符串
|
||||
function generateFileArgs(config) {
|
||||
const args = [stringifyWithType(config.selector)];
|
||||
const args = [stringifyArgv(config.selector)];
|
||||
if (config.files) {
|
||||
args.push(stringifyObject(config.files));
|
||||
args.push(stringifyArgv(config.files));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
|
||||
// 生成 value 参数字符串
|
||||
function generateValueArgs(config) {
|
||||
return `${stringifyWithType(config.selector)}, ${stringifyWithType(
|
||||
return `${stringifyArgv(config.selector)}, ${stringifyArgv(
|
||||
config.value
|
||||
)}`;
|
||||
}
|
||||
|
||||
// 生成 check 参数字符串
|
||||
function generateCheckArgs(config) {
|
||||
return `${stringifyWithType(config.selector)}, ${config.checked}`;
|
||||
return `${stringifyArgv(config.selector)}, ${config.checked}`;
|
||||
}
|
||||
|
||||
// 生成 scroll 参数字符串
|
||||
function generateScrollArgs(config) {
|
||||
if (config.type === "element") {
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
} else {
|
||||
if (config.x !== undefined) {
|
||||
return `${config.x}, ${config.y}`;
|
||||
@@ -325,9 +325,9 @@ function generateScrollArgs(config) {
|
||||
|
||||
// 生成 download 参数字符串
|
||||
function generateDownloadArgs(config) {
|
||||
const args = [stringifyWithType(config.url)];
|
||||
const args = [stringifyArgv(config.url)];
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user