Skip to content

🧹 RemoveScript ​

从 composer.json 移除指定名称的脚本及其描述。

何时使用 ​

需要清理不再需要的 Composer 生命周期脚本或自定义脚本时使用。本方法会同时清理 scripts 段和 scripts-descriptions 段中的对应条目。

签名 ​

go
func (c *Composer) RemoveScript(name string) error

参数 ​

参数类型说明
namestring要移除的脚本名称

返回值 ​

  • error:读取或写入 composer.json 失败时返回错误。

示例 ​

go
package main

import (
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatal(err)
	}

	if err := comp.RemoveScript("post-install-cmd"); err != nil {
		log.Fatalf("移除脚本失败: %v", err)
	}
}

进阶 ​

  • 🔄 内部流程:ReadComposerJSON → 从 Scripts 和 ScriptsDescriptions 各 delete 一次 → WriteComposerJSON。
  • ⚠️ 当脚本不存在或映射为 nil 时,方法是安全无操作的,不会报错。
  • ➕ 对应的添加方法见 AddScript。
  • 📋 查询现有脚本可使用 GetScripts(返回 map[string]interface{})。

基于 MIT 许可证发布