Skip to content

Commit

Permalink
feat: YamlEditor root
Browse files Browse the repository at this point in the history
  • Loading branch information
Lain. committed May 30, 2024
1 parent 4fe44da commit a46b036
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/utils/YamlEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,47 @@ export default class YamlEditor {
}
}

/**
* 向根节点新增元素,如果根节点不是数组,则将其转换为数组再新增元素
* @param {any} value - 要新增的元素
*/
pusharr (value) {
try {
if (!(this.document.contents instanceof Yaml.YAMLSeq)) {
// 如果根节点不是数组,则将其转换为数组
this.document.contents = new Yaml.YAMLSeq()
logger.debug('[YamlEditor] 根节点已转换为数组')
}
this.document.contents.add(value)
logger.debug('[YamlEditor] 已向根节点数组新增元素:', value)
} catch (error) {
logger.error(`[YamlEditor] 向根节点数组新增元素时出错:${error}`)
return false
}
}

/**
* 根据索引从根节点数组删除元素
* @param {number} index - 要删除元素的索引
* @returns {boolean} 是否删除成功
*/
delarr (index) {
try {
if (!(this.document.contents instanceof Yaml.YAMLSeq)) {
throw new Error('[YamlEditor] 根节点不是数组')
}
if (index < 0 || index >= this.document.contents.items.length) {
throw new Error('[YamlEditor] 索引超出范围')
}
this.document.contents.items.splice(index, 1)
logger.debug('[YamlEditor] 已根据索引从根节点数组删除元素,索引:', index)
return true
} catch (error) {
logger.error(`[YamlEditor] 根据索引删除根节点数组元素时出错:${error}`)
return false
}
}

/**
* 保存文件
*/
Expand Down

0 comments on commit a46b036

Please sign in to comment.