|
@ -13,7 +13,7 @@ const path = require('path'); |
|
|
function updateJsonFileField(filePath, fieldPath, newValue, options = {}) { |
|
|
function updateJsonFileField(filePath, fieldPath, newValue, options = {}) { |
|
|
const tabSize = options.tabSize || 2; |
|
|
const tabSize = options.tabSize || 2; |
|
|
if (!fs.existsSync(filePath)) { |
|
|
if (!fs.existsSync(filePath)) { |
|
|
console.warn(`[json-editor] ⚠️ 文件不存在: ${filePath}`); |
|
|
|
|
|
|
|
|
console.warn(`[json-editor] 文件不存在: ${filePath}`); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
try { |
|
|
try { |
|
@ -21,16 +21,16 @@ function updateJsonFileField(filePath, fieldPath, newValue, options = {}) { |
|
|
parse(raw); // 验证是否为合法 JSONC
|
|
|
parse(raw); // 验证是否为合法 JSONC
|
|
|
|
|
|
|
|
|
const edits = modify(raw, fieldPath, newValue, { |
|
|
const edits = modify(raw, fieldPath, newValue, { |
|
|
formattingOptions: { |
|
|
|
|
|
insertSpaces: true, |
|
|
|
|
|
tabSize, |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
formattingOptions: { |
|
|
|
|
|
insertSpaces: true, |
|
|
|
|
|
tabSize, |
|
|
|
|
|
}, |
|
|
}); |
|
|
}); |
|
|
const updated = applyEdits(raw, edits); |
|
|
const updated = applyEdits(raw, edits); |
|
|
fs.writeFileSync(filePath, updated, 'utf-8'); |
|
|
fs.writeFileSync(filePath, updated, 'utf-8'); |
|
|
console.log(`[json-editor] ✅ ${path.basename(filePath)} -> 修改 ${fieldPath.join('.')} = ${JSON.stringify(newValue)}`); |
|
|
|
|
|
|
|
|
console.log(`[json-editor] ${path.basename(filePath)} -> 修改 ${fieldPath.join('.')} = ${JSON.stringify(newValue)}`); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.error(`[json-editor] ❌ 修改失败: ${err.message}`); |
|
|
|
|
|
|
|
|
console.error(`[json-editor] 修改失败: ${err.message}`); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|