Skip to content

22.5 外掛釋出與維護

python
## 22.5.1 插件打包

### 基本打包配置

// package.json { "name": "my-claude-plugin", "version": "1.0.0", "description": "My Claude Code Plugin", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ "dist", "plugin.yaml" ], "scripts": { "build": "tsc", "prepack": "npm run build", "pack": "npm pack", "publish": "npm publish" }, "keywords": [ "claude-code", "plugin" ], "author": "Your Name", "license": "MIT", "devDependencies": { "@claude-code/plugin-sdk": "^1.0.0", "typescript": "^4.9.0" } }

### TypeScript 配置

    bash


    json

    // tsconfig.json
    {
      "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "lib": ["ES2020"],
        "outDir": "./dist",
        "rootDir": "./src",
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "moduleResolution": "node"
      },
      "include": ["src/**/*"],
      "exclude": ["node_modules", "dist", "**/*.test.ts"]
    }

    ### 插件清单

    # plugin.yaml
    name: my-claude-plugin
    version: 1.0.0
    description: My Claude Code Plugin
    author: Your Name
    license: MIT
    homepage: https://github.com/yourname/my-claude-plugin
    repository: https://github.com/yourname/my-claude-plugin
    # 插件入口
    main: dist/index.js
    types: dist/index.d.ts
    # 插件权限
    permissions:
    file:
    - read: "/"
    network:
    - https: ["api.example.com"]
    # 插件依赖
    dependencies:
    claude-code: ">=1.0.0"
    # 插件元数据
    metadata:
    category: development
    tags:
    - code-generation
    - productivity
    keywords:
    - plugin
    - claude-code

### 打包脚本

    bash


    bash

    #!/bin/bash
    # scripts/build.sh

    echo "Building plugin..."

    # 清理构建目录
    rm -rf dist

    # 编译 TypeScript
    npm run build

    # 复制插件清单
    cp plugin.yaml dist/

    # 复制 README
    cp README.md dist/

    # 复制 LICENSE
    cp LICENSE dist/ 2>/dev/null || true

    echo "Build complete!"

    #!/bin/bash
    # scripts/pack.sh
    echo "Packing plugin..."
    # 构建
    ./scripts/build.sh
    # 打包
    cd dist
    npm pack
    # 移动包到项目根目录
    mv *.tgz ../
    echo "Pack complete!"

## 22.5.2 版本管理

### 语义化版本

    bash


    typescript

    // src/version.ts

    /**
     * 语义化版本
     */
    export class SemanticVersion {
      major: number;
      minor: number;
      patch: number;
      prerelease?: string;
      build?: string;

      constructor(version: string) {
        const parsed = this.parse(version);
        this.major = parsed.major;
        this.minor = parsed.minor;
        this.patch = parsed.patch;
        this.prerelease = parsed.prerelease;
        this.build = parsed.build;
      }

      /**
       * 解析版本字符串
       */
      private parse(version: string): {
        major: number;
        minor: number;
        patch: number;
        prerelease?: string;
        build?: string;
      } {
        const regex = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+))?(?:\+([0-9A-Za-z-]+))?$/;
        const match = version.match(regex);

        if (!match) {
          throw new Error(`Invalid version: ${version}`);
        }

        return {
          major: parseInt(match[1]),
          minor: parseInt(match[2]),
          patch: parseInt(match[3]),
          prerelease: match[4],
          build: match[5]
        };
      }

      /**
       * 转换为字符串
       */
      toString(): string {
        let version = `${this.major}.${this.minor}.${this.patch}`;

        if (this.prerelease) {
          version += `-${this.prerelease}`;
        }

        if (this.build) {
          version += `+${this.build}`;
        }

        return version;
      }

      /**
       * 主版本升级
       */
      bumpMajor(): SemanticVersion {
        return new SemanticVersion(
          `${this.major + 1}.0.0`
        );
      }

      /**
       * 次版本升级
       */
      bumpMinor(): SemanticVersion {
        return new SemanticVersion(
          `${this.major}.${this.minor + 1}.0`
        );
      }

      /**
       * 补丁版本升级
       */
      bumpPatch(): SemanticVersion {
        return new SemanticVersion(
          `${this.major}.${this.minor}.${this.patch + 1}`
        );
      }

      /**
       * 比较版本
       */
      compare(other: SemanticVersion): number {
        if (this.major !== other.major) {
          return this.major - other.major;
        }

        if (this.minor !== other.minor) {
          return this.minor - other.minor;
        }

        if (this.patch !== other.patch) {
          return this.patch - other.patch;
        }

        return 0;
      }

      /**
       * 检查是否大于
       */
      greaterThan(other: SemanticVersion): boolean {
        return this.compare(other) > 0;
      }

      /**
       * 检查是否小于
       */
      lessThan(other: SemanticVersion): boolean {
        return this.compare(other) < 0;
      }

      /**
       * 检查是否等于
       */
      equals(other: SemanticVersion): boolean {
        return this.compare(other) === 0;
      }
    }

    // 使用示例
    const version = new SemanticVersion('1.2.3');
    console.log(version.toString()); // 1.2.3

    const nextMajor = version.bumpMajor();
    console.log(nextMajor.toString()); // 2.0.0

    const nextMinor = version.bumpMinor();
    console.log(nextMinor.toString()); // 1.3.0

    const nextPatch = version.bumpPatch();
    console.log(nextPatch.toString()); // 1.2.4

    const v1 = new SemanticVersion('1.2.3');
    const v2 = new SemanticVersion('1.2.4');

    console.log(v1.lessThan(v2)); // true
    console.log(v2.greaterThan(v1)); // true

    ### 版本发布脚本

    #!/bin/bash
    # scripts/release.sh
    VERSION=$1
    if [ -z "$VERSION" ]; then
    echo "Usage: ./scripts/release.sh <version>"
    exit 1
    fi
    echo "Releasing version $VERSION..."
    # 更新 package.json
    npm version $VERSION --no-git-tag-version
    # 更新 plugin.yaml
    sed -i.bak "s/^version: .*/version: $VERSION/" plugin.yaml
    rm plugin.yaml.bak
    # 构建和打包
    ./scripts/pack.sh
    # 提交更改
    git add package.json plugin.yaml
    git commit -m "Release version $VERSION"
    # 创建标签
    git tag -a "v$VERSION" -m "Release version $VERSION"
    # 推送到远程
    git push origin main
    git push origin "v$VERSION"
    # 发布到 npm
    npm publish
    echo "Release $VERSION complete!"

## 22.5.3 文档生成

### API 文档生成

    bash


    typescript

    // scripts/generate-docs.ts

    import { Project, TSConfigReader, TypeDocReader } from 'typedoc';
    import { MarkdownRenderer } from 'typedoc-plugin-markdown';

    /**
     * 生成 API 文档
     */
    async function generateDocs() {
      const project = new Project({
        tsconfig: 'tsconfig.json',
        entryPoints: ['src/index.ts'],
        readme: 'README.md',
        out: 'docs/api',
        plugin: [TypeDocReader, MarkdownRenderer],
        exclude: ['**/*.test.ts', '**/node_modules/**'],
        theme: 'markdown',
        gitRevision: 'main'
      });

      await project.generate();
    }

    generateDocs().catch(console.error);

    ### README 模板

    # My Claude Code Plugin
    [![NPM Version](https://img.shields.io/npm/v/my-claude-plugin.svg)](https://www.npmjs.com/package/my-claude-plugin)
    [![License](https://img.shields.io/npm/l/my-claude-plugin.svg)](LICENSE)
    My Claude Code Plugin is a powerful plugin for Claude Code that provides [brief description].
    ## Features
    - Feature 1
    - Feature 2
    - Feature 3
    ## Installation

    ````bash

    `bash

```python
    claude plugin install my-claude-plugin

    ```> Or install from npm:

bash

npm install -g my-claude-plugin

```python
## Usage

### Basic Usage

    typescript


    ````typescript

    ````bash

    # Clone the repository
    git clone https://github.com/yourname/my-claude-plugin.git
    cd my-claude-plugin

    # Install dependencies
    npm install

    # Build the project
    npm run build

    ```### Testing

    ```
    bash

    # Run tests
    npm test

    # Run tests with coverage
    npm run test:coverage

    ### Building

    ````bash

    Contributions are welcome! Please feel free to submit a Pull Request.

    ## License

    MIT © [Your Name]

    ## Support

    For support, please open an issue on GitHub or contact [your-email@example.com].

    ### 发布工作流

    ### 性能监控

    ### 用户调查

基于 MIT 许可发布 | 永久导航