优化保存配置信息到文件的代码,将标准json数据保存到文件中,方便编辑器打开来进行观察。

pull/274/head
王良 2024-02-22 01:28:34 +08:00
parent a7d8275b87
commit 144527c705
4 changed files with 19 additions and 7 deletions

View File

@ -17,12 +17,12 @@ function get () {
const getDefaultConfigBasePath = function () {
return get().server.setting.userBasePath
}
function _getRemoteSavePath () {
function _getRemoteSavePath (prefix = '') {
const dir = getDefaultConfigBasePath()
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return path.join(dir, 'remote_config.json5')
return path.join(dir, prefix + 'remote_config.json5')
}
function _getConfigPath () {
const dir = getDefaultConfigBasePath()
@ -64,6 +64,10 @@ const configApi = {
return
}
if (response && response.statusCode === 200) {
const originalRemoteSavePath = _getRemoteSavePath('original_')
fs.writeFileSync(originalRemoteSavePath, body)
log.info('保存原来的远程配置文件成功:', originalRemoteSavePath)
// 尝试解析远程配置,如果解析失败,则不保存它
let remoteConfig
try {
@ -74,7 +78,9 @@ const configApi = {
}
if (remoteConfig != null) {
fs.writeFileSync(_getRemoteSavePath(), body)
const remoteSavePath = _getRemoteSavePath()
fs.writeFileSync(remoteSavePath, JSON.stringify(remoteConfig, null, '\t'))
log.info('保存远程配置文件成功:', remoteSavePath)
}
resolve()
@ -120,7 +126,9 @@ const configApi = {
// 计算新配置与默认配置(启用远程配置时,含远程配置)的差异,并保存到 config.json5 中
const diffConfig = mergeApi.doDiff(defConfig, newConfig)
fs.writeFileSync(_getConfigPath(), JSON5.stringify(diffConfig, null, 2))
const configPath = _getConfigPath()
fs.writeFileSync(configPath, JSON.stringify(diffConfig, null, '\t'))
log.info('保存自定义配置文件成功:', configPath)
configApi.reload()
return diffConfig
},

View File

@ -70,7 +70,8 @@ const serverApi = {
// fireStatus('ing') // 启动中
const basePath = serverConfig.setting.userBasePath
const runningConfigPath = path.join(basePath, '/running.json')
fs.writeFileSync(runningConfigPath, JSON5.stringify(serverConfig, null, 2))
fs.writeFileSync(runningConfigPath, JSON.stringify(serverConfig, null, '\t'))
log.info('保存运行时配置文件成功:', runningConfigPath)
const serverProcess = fork(mitmproxyPath, [runningConfigPath])
server = {
id: serverProcess.pid,

View File

@ -3,9 +3,11 @@ const server = require('@docmirror/mitmproxy')
const JSON5 = require('json5')
const path = require('path')
const home = process.env.USER_HOME || process.env.HOME || 'C:/Users/Administrator/'
let configPath = path.join(home, '.dev-sidecar/running.json')
let configPath
if (process.argv && process.argv.length > 3) {
configPath = process.argv[2]
} else {
configPath = path.join(home, '.dev-sidecar/running.json')
}
const fs = require('fs')

View File

@ -64,7 +64,8 @@ const localApi = {
},
save (setting = {}) {
const settingPath = _getSettingsPath()
fs.writeFileSync(settingPath, JSON5.stringify(setting, null, 2))
fs.writeFileSync(settingPath, JSON.stringify(setting, null, '\t'))
log.info('保存setting配置文件成功', settingPath)
}
},
/**