create-plugkey-dsh 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,21 @@
1
- # Temporary Holding Version
1
+ # create-plugkey-dsh
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ DSH + Plugkey 付费插件脚手架,需要 Node.js >=20。生成的项目:`plugin.js` 是插件本体(不含授权代码),`index.js` 用 `definePaid` 声明哪些命令收费。
4
+
5
+ ```sh
6
+ npm create plugkey-dsh@latest my-plugin
7
+ pnpm create plugkey-dsh my-plugin --yes --product prod_abcd1234 --name "我的插件" --package @scope/my-plugin
8
+ ```
9
+
10
+ 省略参数时用中文交互询问;`--yes` 使用目录名作为包名与显示名,产品默认为 `prod_REPLACE_ME`。
11
+ 目录默认 `my-plugin`;拒绝非空目标目录。可用 `--help` 查看参数。
12
+
13
+ 仓库内直接运行:
14
+
15
+ ```sh
16
+ node packages/create-plugkey-dsh/bin/index.js /tmp/my-plugin --yes
17
+ pnpm --filter create-plugkey-dsh typecheck
18
+ pnpm --filter create-plugkey-dsh test
19
+ ```
20
+
21
+ 模板在 `templates/` 中,使用简单占位符替换。生成的插件无需构建,唯一的依赖是授权中心 `@plugkey/dsh-license`(只用到它约 7 KB 的 `define` 子路径);安装、激活和打包步骤见生成的 README,完整说明见 [开发者手册](https://plugkey.dev/docs/developer-guide/)。
package/bin/index.js ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env node
2
+ import { readdir, mkdir, readFile, writeFile } from 'node:fs/promises'
3
+ import { basename, resolve, join } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { createInterface } from 'node:readline/promises'
6
+ import { stdin, stdout } from 'node:process'
7
+
8
+ /** @param {string} value */
9
+ function validPackage(value) {
10
+ if (value.length > 214 || ['node_modules', 'favicon.ico'].includes(value)) return false
11
+ return /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(value)
12
+ }
13
+
14
+ /** @param {string[]} args */
15
+ async function main(args) {
16
+ /** @type {Record<string, string>} */
17
+ const options = {}
18
+ let yes = false
19
+ let directory
20
+ for (let i = 0; i < args.length; i++) {
21
+ const arg = args[i]
22
+ if (arg === '--yes') { yes = true; continue }
23
+ if (arg === '--help' || arg === '-h') {
24
+ console.log('用法:create-plugkey-dsh [目录] [--product prod_xxx] [--name "显示名"] [--package @scope/name] [--yes]')
25
+ return
26
+ }
27
+ if (arg && ['--product', '--name', '--package'].includes(arg)) {
28
+ const value = args[++i]
29
+ if (!value || value.startsWith('--')) throw new Error(`${arg} 缺少参数值。`)
30
+ options[arg.slice(2)] = value
31
+ } else if (arg?.startsWith('-')) throw new Error(`未知选项:${arg}`)
32
+ else if (directory !== undefined) throw new Error('只能指定一个目标目录。')
33
+ else directory = arg
34
+ }
35
+ const rl = yes ? undefined : createInterface({ input: stdin, output: stdout })
36
+ /** @param {string} prompt @param {string} fallback */
37
+ const ask = async (prompt, fallback) => rl ? (await rl.question(`${prompt}(默认 ${fallback}):`)).trim() || fallback : fallback
38
+ let packageName, displayName, product
39
+ try {
40
+ directory ??= await ask('插件目录', 'my-plugin')
41
+ const fallback = basename(resolve(directory))
42
+ packageName = options.package ?? await ask('npm 包名', fallback)
43
+ displayName = options.name ?? await ask('插件显示名', fallback)
44
+ product = options.product ?? await ask('产品 ID', 'prod_REPLACE_ME')
45
+ } finally { rl?.close() }
46
+ const target = resolve(directory)
47
+ try {
48
+ if ((await readdir(target)).length) throw new Error('目标目录非空,拒绝写入。请选择空目录。')
49
+ } catch (error) {
50
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error
51
+ }
52
+ if (!validPackage(packageName)) throw new Error('npm 包名无效:请使用小写字母、数字、点、连字符或下划线,可带 @scope/ 前缀,最长 214 字符。')
53
+ if (product !== 'prod_REPLACE_ME' && !/^prod_[0-9a-z]{4,64}$/.test(product)) throw new Error('产品 ID 无效:应为 prod_ 后跟 4–64 位小写字母或数字,或 prod_REPLACE_ME。')
54
+ if (!displayName.trim()) throw new Error('插件显示名不能为空。')
55
+ const slug = packageName.split('/').at(-1)?.replace(/[^a-z0-9-]/g, '-').replace(/^[^a-z]+/, '').slice(0, 55) || 'my-plugin'
56
+ /** @type {Record<string, string>} */
57
+ const values = { packageJson: JSON.stringify(packageName), nameJson: JSON.stringify(displayName), productJson: JSON.stringify(product), slug, nameMarkdown: displayName.replace(/[\r\n]/g, ' '), descriptionJson: JSON.stringify('免费问候与 Plugkey 授权付费功能。') }
58
+ const source = fileURLToPath(new URL('../templates/', import.meta.url))
59
+ /** @param {string} relative */
60
+ async function copy(relative) {
61
+ for (const entry of await readdir(join(source, relative), { withFileTypes: true })) {
62
+ const path = join(relative, entry.name)
63
+ if (entry.isDirectory()) { await copy(path); continue }
64
+ // npm drops .gitignore from published packages, so the template ships it as _gitignore.
65
+ const output = join(target, path.replace(/(^|[\\/])_gitignore$/, '$1.gitignore'))
66
+ const content = (await readFile(join(source, path), 'utf8')).replace(/\{\{(\w+)\}\}/g, (_, key) => {
67
+ if (values[key] === undefined) throw new Error(`未知模板变量:${key}`)
68
+ return values[key]
69
+ })
70
+ await mkdir(join(output, '..'), { recursive: true })
71
+ await writeFile(output, content, { flag: 'wx' })
72
+ }
73
+ }
74
+ await copy('')
75
+ console.log(`插件已生成:${target}\n下一步:\n1. 在 Plugkey 控制台建产品,将真实 productId 写入 cordis.patch.yml。\n2. 安装授权中心:dsh plugin add @plugkey/dsh-license\n3. 本地安装:dsh plugin add ${JSON.stringify(target)}\n4. 在控制台生成卡密,再使用 /activate <卡密> 激活。\n5. 在插件目录运行 npm run pack,获得可上传的插件包。`)
76
+ }
77
+
78
+ main(process.argv.slice(2)).catch(error => {
79
+ console.error(`创建失败:${error instanceof Error ? error.message : String(error)}`)
80
+ process.exitCode = 1
81
+ })
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "create-plugkey-dsh",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "create-plugkey-dsh": "./bin/index.js"
7
+ },
8
+ "files": [
9
+ "README.md",
10
+ "bin",
11
+ "templates"
12
+ ],
13
+ "engines": {
14
+ "node": ">=20"
15
+ },
16
+ "license": "MIT",
17
+ "description": "Scaffold a paid DeepSeek Harness (DSH) plugin with Plugkey licensing (definePaid).",
18
+ "homepage": "https://plugkey.dev/docs/developer-guide/",
19
+ "keywords": [
20
+ "dsh-plugin",
21
+ "deepseek-harness",
22
+ "plugkey",
23
+ "license",
24
+ "create"
25
+ ],
26
+ "scripts": {
27
+ "test": "vitest run",
28
+ "typecheck": "tsc -p tsconfig.json"
29
+ }
6
30
  }
@@ -0,0 +1,27 @@
1
+ # {{nameMarkdown}}
2
+
3
+ 无需构建的 DSH 插件(ESM)。需要 DSH >=0.2.1-rc.1 <0.3.0。唯一的依赖是授权中心提供的 `definePaid`(`@plugkey/dsh-license/define`,无其他依赖)。
4
+
5
+ - `plugin.js`:插件本体,普通的 DSH 插件,没有授权代码。
6
+ - `index.js`:一行声明哪些命令收费(`definePaid`)。
7
+
8
+ ## 安装和授权
9
+
10
+ 1. 安装授权中心:`dsh plugin add @plugkey/dsh-license`。
11
+ 2. 在 Plugkey 控制台新建产品,取得真实 productId;默认「标准版」即可买了就用。
12
+ 3. 修改 `cordis.patch.yml` 的 `productId`(当前为 {{productJson}}),可选填写 `purchaseUrl`。已安装时也要同步 DSH 插件行配置并重新加载。
13
+ 4. 本地安装:`dsh plugin add <path>`,将 `<path>` 替换为本插件的绝对路径。
14
+ 5. 在控制台的产品「卡密」页选择方案并生成卡密。
15
+ 6. 输入 `/activate <卡密>` 激活,再执行 `/{{slug}}-pro`。
16
+
17
+ `/{{slug}}-hello` 始终免费;`/{{slug}}-pro` 在 `index.js` 里声明为收费命令,由授权中心在每次调用时校验,并在斜杠菜单里显示「(需授权)」。
18
+ 想让整个插件付费,把 `paid` 改成 `'*'`:未激活时插件不加载,授权失效(到期、吊销、离线超过宽限期)时自动卸载。
19
+ 没有安装授权中心时,免费命令依然可用;付费命令显示安装提示。
20
+ `/license` 查看授权,`/license refresh` 同步状态。
21
+
22
+ ## 打包分发
23
+
24
+ 在插件目录运行 `npm run pack`。最简单的方式也可以不改代码:在控制台「插件包」上传生成的 `.tgz`,选择部分命令收费并勾选付费命令,下载授权包分发给用户。
25
+ 上传受保护插件包时,收费命令以控制台的勾选为准:把 `plugin.js` 作为入口(`package.json` 的 `exports["."]` 改为 `./plugin.js`)再打包上传即可。
26
+
27
+ 授权校验运行在用户电脑上,无法完全防止破解。
@@ -0,0 +1,3 @@
1
+ node_modules/
2
+ *.tgz
3
+ .DS_Store
@@ -0,0 +1,6 @@
1
+ - insert:
2
+ - id: {{slug}}
3
+ name: {{packageJson}}
4
+ config:
5
+ productId: {{productJson}}
6
+ purchaseUrl: ''
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect x="4" y="4" width="56" height="56" rx="14" fill="#17334b"/><path d="M32 12 50 32 32 52 14 32Z" fill="#56d6bb"/><circle cx="32" cy="28" r="6" fill="#17334b"/><path d="M29 32h6v11h-6z" fill="#17334b"/></svg>
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 插件入口:只声明「卖什么」。插件本体在 plugin.js,里面没有任何授权代码。
3
+ *
4
+ * - paid: { commands: { '命令名': true } } 这些命令需要授权,其余免费
5
+ * - paid: { commands: { '命令名': 'pro' } } 需要方案里有功能点 pro(分级收费)
6
+ * - paid: '*' 整个插件付费,未激活时不加载
7
+ *
8
+ * 激活、续签、离线宽限、吊销、提示文案都由授权中心 @plugkey/dsh-license 统一处理。
9
+ * cordis.patch.yml 里的 productId / purchaseUrl 会覆盖这里的同名设置。
10
+ */
11
+ import { definePaid } from '@plugkey/dsh-license/define'
12
+ import * as plugin from './plugin.js'
13
+
14
+ const paid = definePaid({
15
+ product: {{productJson}},
16
+ name: {{nameJson}},
17
+ packageName: {{packageJson}},
18
+ paid: { commands: { '{{slug}}-pro': true } },
19
+ }, plugin)
20
+
21
+ export const name = paid.name
22
+ export const apply = paid.apply
23
+ export default paid
@@ -0,0 +1 @@
1
+ { "meta": { "title": {{nameJson}}, "description": "Free greeting and paid features with Plugkey licensing." } }
@@ -0,0 +1 @@
1
+ { "meta": { "title": {{nameJson}}, "description": {{descriptionJson}} } }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": {{packageJson}},
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": { ".": "./index.js", "./locale/*.json": "./locale/*.json", "./icon": "./icon.svg" },
6
+ "files": ["index.js", "plugin.js", "cordis.patch.yml", "locale/*.json", "icon.svg", "README.md"],
7
+ "dsh": { "bundle": { "patch": "./cordis.patch.yml" } },
8
+ "engines": { "dsh": ">=0.2.1-rc.1 <0.3.0" },
9
+ "dependencies": { "@plugkey/dsh-license": "^0.5.0" },
10
+ "scripts": { "pack": "npm pack" }
11
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 插件本体:普通的 DSH(Cordis)插件,不需要关心授权。
3
+ * 需要在代码里做更细的判断时(例如某个参数是付费选项),可以用:
4
+ * if (!ctx.entitlement.allows('pro')) return { kind: 'error', text: ctx.entitlement.prompt('pro') }
5
+ */
6
+ export const name = '{{slug}}'
7
+ export const inject = ['commands']
8
+
9
+ export function apply(ctx) {
10
+ ctx.commands.register({
11
+ name: '{{slug}}-hello',
12
+ description: '免费问候',
13
+ handler: async () => ({ kind: 'success', text: '你好!免费功能随时可用。' }),
14
+ })
15
+ ctx.commands.register({
16
+ name: '{{slug}}-pro',
17
+ description: '体验付费功能',
18
+ handler: async () => ({ kind: 'success', text: '已授权,付费功能执行成功!' }),
19
+ })
20
+ }