fix cli --settings

This commit is contained in:
musistudio
2025-12-26 11:29:57 +08:00
parent d13bbed827
commit 18654b60ce
2 changed files with 28 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import { spawn, type StdioOptions } from "child_process"; import { spawn, type StdioOptions } from "child_process";
import { readConfigFile } from "."; import {getSettingsPath, readConfigFile} from ".";
import { import {
decrementReferenceCount, decrementReferenceCount,
incrementReferenceCount, incrementReferenceCount,
@@ -24,7 +24,7 @@ export async function executeCodeCommand(args: string[] = []) {
padding: 0, padding: 0,
} }
} }
// args.push('--settings', `${JSON.stringify(settingsFlag)}`); args.push('--settings', getSettingsPath(`${JSON.stringify(settingsFlag)}`));
// Non-interactive mode for automation environments // Non-interactive mode for automation environments
if (config.NON_INTERACTIVE_MODE) { if (config.NON_INTERACTIVE_MODE) {
@@ -70,7 +70,6 @@ export async function executeCodeCommand(args: string[] = []) {
{ {
env: { env: {
...process.env, ...process.env,
...env
}, },
stdio: stdioConfig, stdio: stdioConfig,
shell: true, shell: true,

View File

@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import readline from "node:readline"; import readline from "node:readline";
import JSON5 from "json5"; import JSON5 from "json5";
import path from "node:path"; import path from "node:path";
import { createHash } from "node:crypto";
import { import {
CONFIG_FILE, CONFIG_FILE,
HOME_DIR, PID_FILE, HOME_DIR, PID_FILE,
@@ -241,3 +242,28 @@ export const restartService = async () => {
startProcess.unref(); startProcess.unref();
console.log("✅ Service started successfully in the background."); console.log("✅ Service started successfully in the background.");
}; };
/**
* 获取设置文件的临时路径
* 对内容进行 hash如果临时目录中已存在该 hash 的文件则直接返回,否则创建新文件
* @param content 设置内容字符串
* @returns 临时文件的完整路径
*/
export const getSettingsPath = (content: string): string => {
// 对内容进行 hash使用 SHA256 算法)
const hash = createHash('sha256').update(content, 'utf-8').digest('hex');
const fileName = `ccr-settings-${hash}.json`;
const tempFilePath = path.join(HOME_DIR, fileName);
// 检查文件是否已存在
if (existsSync(tempFilePath)) {
return tempFilePath;
}
// 文件不存在,创建并写入内容
writeFileSync(tempFilePath, content, 'utf-8');
return tempFilePath;
}