Skip to content

Commit

Permalink
fix: add yamlEditor remove
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Aug 7, 2024
1 parent e9d5736 commit e12c05b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/cli/karin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import { program } from 'commander'
import { exec as execCmd, spawn, ChildProcess } from 'child_process'
import { KarinCfgInit } from '../core/init/config'

const enum Runner {
export const enum Runner {
Node = 'node',
Tsx = 'tsx',
Pm2 = 'pm2'
}

const enum Mode {
export const enum Mode {
Dev = 'dev',
Prod = 'prod'
}

const enum Lang {
export const enum Lang {
Js = 'js',
Ts = 'ts'
}
Expand All @@ -33,6 +33,7 @@ class KarinCli {

constructor () {
process.env.karin_app_start_count = '0'
process.env.karin_app_watch = 'no'
/** 当前文件绝对路径 */
this.filename = fileURLToPath(import.meta.url)
/** karin目录 */
Expand Down Expand Up @@ -99,7 +100,7 @@ class KarinCli {
}

/** 启动 */
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env })
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === Runner.Tsx })
/** 监听退出 */
this.child.once('exit', (code) => process.exit(code))
/** 监听子进程消息 */
Expand Down
54 changes: 46 additions & 8 deletions src/utils/config/yamlEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ export class YamlEditor {
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
* @param value - 要设置的值 允许的类型:`string`, `boolean`, `number`, `object`, `array`
*/
set (path: string, value: YamlValue) {
set (path: string, value: YamlValue): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path
this.document.setIn(_path, value)
return true
} catch (error) {
logger.error(`[YamlEditor] 设置数据时出错:${error}`)
return null
return false
}
}

Expand All @@ -55,13 +56,15 @@ export class YamlEditor {
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
* @param value - 要添加的值
*/
add (path: string, value: YamlValue) {
add (path: string, value: YamlValue): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path
this.document.addIn(_path, value)
logger.debug(`[YamlEditor] 已在 ${path} 添加新的值`)
return true
} catch (error) {
logger.error(`[YamlEditor] 添加数据时出错:${error}`)
return false
}
}

Expand All @@ -70,7 +73,7 @@ export class YamlEditor {
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
* @returns 是否删除成功
*/
del (path: string) {
del (path: string): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path
this.document.deleteIn(_path)
Expand All @@ -87,29 +90,63 @@ export class YamlEditor {
* @param value - 要添加的值
* @param prepend - 如果为 true,则添加到数组的开头,否则添加到末尾
*/
append (path: string, value: string, prepend = false) {
append (path: string, value: string, prepend = false): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path || []
let current = this.document.getIn(_path)
if (!current) {
current = new Yaml.YAMLSeq()
this.document.setIn(_path, current)
} else if (!(current instanceof Yaml.YAMLSeq)) {
throw new Error('[YamlEditor] 指定的路径不是数组')
logger.error('[YamlEditor] 指定的路径不是数组')
return false
} else {
prepend ? current.items.unshift(value) : current.add(value)
}
logger.debug(`[YamlEditor] 已向 ${path} 数组${prepend ? '开头' : '末尾'}添加新元素:${value}`)
return true
} catch (error) {
logger.error(`[YamlEditor] 向数组添加元素时出错:${error}`)
return false
}
}

/**
* 向指定路径的数组删除值
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
* @param value - 要删除的值
*/
remove (path: string, value: YamlValue): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path
const current = this.document.getIn(_path)
if (!current) {
logger.error('[YamlEditor] 指定的路径不存在')
return false
}
if (!(current instanceof Yaml.YAMLSeq)) {
logger.error('[YamlEditor] 指定的路径不是数组')
return false
}
const index = current.items.findIndex(item => lodash.isEqual(item.toJSON(), value))
if (index < 0) {
logger.error('[YamlEditor] 未找到要删除的值')
return false
}
current.items.splice(index, 1)
logger.debug(`[YamlEditor] 已从 ${path} 数组删除元素:${value}`)
return true
} catch (error) {
logger.error(`[YamlEditor] 从数组删除元素时出错:${error}`)
return false
}
}

/**
* 检查指定路径的键是否存在
* @param path - 路径,用点号分隔
*/
has (path: string) {
has (path: string): boolean {
try {
const _path = typeof path === 'string' ? path.split('.') : path
return this.document.hasIn(_path)
Expand Down Expand Up @@ -161,7 +198,7 @@ export class YamlEditor {
* 向根节点新增元素,如果根节点不是数组,则将其转换为数组再新增元素
* @param value - 要新增的元素
*/
pusharr (value: YamlValue) {
pusharr (value: YamlValue): boolean {
try {
if (!(this.document.contents instanceof Yaml.YAMLSeq)) {
// 如果根节点不是数组,则将其转换为数组
Expand All @@ -170,6 +207,7 @@ export class YamlEditor {
}
this.document.contents.add(value)
logger.debug('[YamlEditor] 已向根节点数组新增元素:', value)
return true
} catch (error) {
logger.error(`[YamlEditor] 向根节点数组新增元素时出错:${error}`)
return false
Expand Down
19 changes: 19 additions & 0 deletions src/utils/tools/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ export const exec = (
})
}

/**
* 执行 shell 命令
* @param cmd 命令
* @param log 是否打印日志
* @param options 选项
*/
export const execs = (
cmd: string,
options = { cwd: process.cwd(), encoding: 'utf-8' }
): Promise<string> => {
return new Promise((resolve, reject) => {
execCmd(cmd, options, (error, stdout, stderr) => {
if (error) return reject(error)
if (stderr) console.error(stderr)
resolve(stdout.trim())
})
})
}

export default exec

0 comments on commit e12c05b

Please sign in to comment.