Skip to content

Commit

Permalink
feat: 默认分支 插件主页
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Jan 25, 2025
1 parent a234ba0 commit 9686017
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 30 deletions.
72 changes: 45 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@
| description | string | 插件描述(限制50字符) | "karin plugin basic" |
| time | string | 发布时间 (YYYY-MM-DD HH:mm:ss) | "2025-01-19 10:00:00" |
| license | object | 开源协议信息 | 见下方示例 |
| home | string | 插件主页 | https://github.com/sj817 |

#### 许可证信息 (license)

| 字段名 | 类型 | 描述 | 示例 |
| ------ | ------ | ---------- | ----------------------------------------------------------------- |
| name | string | 许可证名称 | "MIT" |
| url | string | 许可证地址 | "https://github.com/karinjs/karin-plugin-basic/blob/main/LICENSE" |
| url | string | 许可证地址 | https://github.com/karinjs/karin-plugin-basic/blob/main/LICENSE |

#### 作者信息 (author)

作者信息是一个数组,支持多个作者:

| 字段名 | 类型 | 描述 | 示例 |
| ------ | ------ | -------- | -------------------------- |
| name | string | 作者名称 | "shijin" |
| home | string | 作者主页 | "https://github.com/sj817" |
| name | string | 作者名称 | shijin |
| home | string | 作者主页 | https://github.com/sj817 |

#### 仓库信息 (repo)

Expand All @@ -64,7 +65,34 @@
| 字段名 | 类型 | 描述 | 示例 |
| ------ | ------ | ---------------------------------------------------------------- | ----------------------------------------------- |
| type | string | 仓库类型 ("github" \| "gitee" \| "gitcode" \| "gitlab" \| "npm") | "github" |
| url | string | 仓库地址 | "https://github.com/karinjs/karin-plugin-basic" |
| url | string | 仓库地址 | https://github.com/karinjs/karin-plugin-basic |
| branch | string | 默认分支(npm类型为空字符串) | "main" |

#### 主页信息 (home)

| 字段名 | 类型 | 描述 | 示例 |
| ------ | ------ | -------- | -------------------------- |
| home | string | 主页地址 | https://github.com/sj817 |

### 插件类型说明

1. **NPM 插件** (`type: "npm"`)
- 通过 npm 包方式发布的插件

2. **Git 插件** (`type: "git"`)
- 通过 git 仓库方式发布的插件

3. **App 插件** (`type: "app"`)
- 单应用插件,需要提供文件直链地址
- 需要在配置中添加 `files` 字段,包含应用文件的直链地址

## 注意事项

- 请确保将新插件添加到 `plugins` 数组中
- name 必须是唯一的
- description 长度限制为 50 字符
- 对于 App 类型插件,建议提供多个下载源以提高可用性
- 所有仓库源都必须提供`package.json` 并且包含`name``version` 字段

### 示例

Expand All @@ -91,7 +119,13 @@
"repo": [
{
"type": "github",
"url": "https://github.com/karinjs/karin-plugin-basic"
"url": "https://github.com/karinjs/karin-plugin-basic",
"branch": "main"
},
{
"type": "npm",
"url": "https://www.npmjs.com/package/karin-plugin-basic",
"branch": ""
}
]
},
Expand All @@ -113,11 +147,13 @@
"repo": [
{
"type": "github",
"url": "https://github.com/username/karin-plugin-git-example"
"url": "https://github.com/username/karin-plugin-git-example",
"branch": "main"
},
{
"type": "gitee",
"url": "https://gitee.com/username/karin-plugin-git-example"
"url": "https://gitee.com/username/karin-plugin-git-example",
"branch": "master"
}
]
},
Expand All @@ -139,7 +175,8 @@
"repo": [
{
"type": "github",
"url": "https://github.com/username/karin-plugin-app-example"
"url": "https://github.com/username/karin-plugin-app-example",
"branch": "main"
}
],
"files": [
Expand All @@ -151,25 +188,6 @@
}
```

### 插件类型说明

1. **NPM 插件** (`type: "npm"`)
- 通过 npm 包方式发布的插件

2. **Git 插件** (`type: "git"`)
- 通过 git 仓库方式发布的插件

3. **App 插件** (`type: "app"`)
- 单应用插件,需要提供文件直链地址
- 需要在配置中添加 `files` 字段,包含应用文件的直链地址

## 注意事项

- 请确保将新插件添加到 `plugins` 数组中
- name 必须是唯一的
- description 长度限制为 50 字符
- 对于 App 类型插件,建议提供多个下载源以提高可用性

## 许可证

本仓库采用 MIT 许可证
Expand Down
8 changes: 6 additions & 2 deletions plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "karin-plugin-basic",
"type": "npm",
"description": "karin plugin basic",
"home": "https://github.com/karinjs/karin-plugin-basic",
"license": {
"name": "MIT",
"url": "https://github.com/karinjs/karin-plugin-basic/blob/main/LICENSE"
Expand All @@ -18,14 +19,16 @@
"repo": [
{
"type": "github",
"url": "https://github.com/karinjs/karin-plugin-basic"
"url": "https://github.com/karinjs/karin-plugin-basic",
"branch": "main"
}
]
},
{
"name": "karin-plugin-kkk",
"type": "npm",
"description": "咕咕咕 ~",
"home": "https://github.com/ikenxuan/karin-plugin-kkk",
"license": {
"name": "GPL-3.0",
"url": "https://github.com/ikenxuan/karin-plugin-kkk/blob/main/LICENSE"
Expand All @@ -40,7 +43,8 @@
"repo": [
{
"type": "github",
"url": "https://github.com/ikenxuan/karin-plugin-kkk"
"url": "https://github.com/ikenxuan/karin-plugin-kkk",
"branch": "main"
}
]
}
Expand Down
76 changes: 75 additions & 1 deletion scripts/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const isValidUrl = (url) => {
* @param {object} plugin
*/
const validateBaseFields = (plugin) => {
const requiredFields = ['name', 'type', 'description', 'license', 'time', 'author', 'repo']
const requiredFields = ['name', 'type', 'description', 'license', 'time', 'author', 'repo', 'home']

for (const field of requiredFields) {
if (!plugin[field]) {
Expand Down Expand Up @@ -68,6 +68,10 @@ const validateBaseFields = (plugin) => {
if (!type.includes(plugin.type)) {
throw new Error(`插件 ${plugin.name} 的类型 ${plugin.type} 无效`)
}

if (!isValidUrl(plugin.home)) {
throw new Error(`插件 ${plugin.name} 的主页 URL 无效`)
}
}

/**
Expand All @@ -85,6 +89,71 @@ const validateAuthor = (plugin) => {
}
}

/**
* 获取仓库原始文件的URL
* @param {object} repo - 仓库信息
* @param {string} filePath - 文件路径
* @returns {string} 原始文件URL
*/
const getRawFileUrl = (repo) => {
const { type, url, branch } = repo
const filePath = 'package.json'

switch (type) {
case 'github':
return url.replace('github.com', 'raw.githubusercontent.com') + `/${branch}/${filePath}`
case 'gitee':
return `${url}/raw/${branch}/${filePath}`
case 'gitlab':
return `${url}/-/raw/${branch}/${filePath}`
case 'gitcode':
return `${url}/-/raw/${branch}/${filePath}`
case 'npm':
return `https://registry.npmjs.org/${url}/latest`
default:
throw new Error(`不支持的仓库类型: ${type}`)
}
}

/**
* 验证仓库的 package.json
* @param {object} plugin - 插件信息
* @param {object} repo - 仓库信息
*/
const validatePackageJson = async (plugin, repo) => {
try {
if (repo.type === 'npm') {
if (repo.branch !== '') {
throw new Error(`插件 ${plugin.name} 的 npm 仓库不应该设置 branch 字段`)
}
return // npm 类型不需要验证 package.json
}

if (!repo.branch) {
throw new Error(`插件 ${plugin.name}${repo.type} 仓库缺少 branch 字段`)
}

const rawUrl = getRawFileUrl(repo)
const response = await fetch(rawUrl)

if (!response.ok) {
throw new Error(`无法访问 package.json: ${response.statusText}`)
}

const packageJson = await response.json()

if (!packageJson.name) {
throw new Error('package.json 缺少 name 字段')
}

if (!packageJson.version) {
throw new Error('package.json 缺少 version 字段')
}
} catch (error) {
throw new Error(`插件 ${plugin.name} 的仓库 ${repo.url} 验证失败: ${error.message}`)
}
}

/**
* 验证仓库信息
* @param {object} plugin
Expand Down Expand Up @@ -144,6 +213,11 @@ const main = async () => {
validateAuthor(plugin)
validateRepo(plugin)

// 验证每个仓库的 package.json
for (const repo of plugin.repo) {
await validatePackageJson(plugin, repo)
}

if (plugin.type === 'app') {
validateAppPlugin(plugin)
} else if (plugin.type === 'git' || plugin.type === 'npm') {
Expand Down
4 changes: 4 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface Base {
description: string
/** 插件提交到仓库时间 */
time: string
/** 插件主页 */
home: string
/** 插件许可证 */
license: {
/** 许可证名称 */
Expand All @@ -35,6 +37,8 @@ export interface Base {
type: 'github' | 'gitee' | 'gitcode' | 'gitlab' | 'npm'
/** 仓库地址 */
url: string
/** 默认分支 npm类型为空字符串 */
branch: string
}[]
}

Expand Down

0 comments on commit 9686017

Please sign in to comment.