From efa3f3634cbb17d5b7da55acc009c43d3a312e53 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 26 Sep 2022 20:27:30 +0800 Subject: [PATCH] feat(cli): add server-plugin-full and update declaration path --- cli/package.json | 4 +-- cli/src/commands/create.ts | 9 ++++++- cli/src/commands/declaration.ts | 2 +- cli/templates/plopfile.js | 27 +++++++++++++++++++ .../server-plugin-full/{{id}}/.ministarrc.js | 7 +++++ .../{{id}}/models/{{pickPluginName id}}.ts | 20 ++++++++++++++ .../server-plugin-full/{{id}}/package.json | 12 +++++++++ .../services/{{pickPluginName id}}.service.ts | 20 ++++++++++++++ pnpm-lock.yaml | 2 +- 9 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 cli/templates/server-plugin-full/{{id}}/.ministarrc.js create mode 100644 cli/templates/server-plugin-full/{{id}}/models/{{pickPluginName id}}.ts create mode 100644 cli/templates/server-plugin-full/{{id}}/package.json create mode 100644 cli/templates/server-plugin-full/{{id}}/services/{{pickPluginName id}}.service.ts diff --git a/cli/package.json b/cli/package.json index ea0ef780..f9c4a593 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "tailchat-cli", - "version": "1.4.0", + "version": "1.4.3", "description": "A Command line interface of tailchat", "bin": { "tailchat": "./bin/cli" @@ -49,7 +49,6 @@ "pretty-ms": "7.0.1", "react": "18.2.0", "tailchat-server-sdk": "^0.0.12", - "tailchat-shared": "workspace:*", "yargs": "^17.4.0" }, "devDependencies": { @@ -59,6 +58,7 @@ "@types/node": "16.11.7", "@types/react": "18.0.20", "@types/yargs": "^17.0.10", + "tailchat-shared": "workspace:*", "ts-node": "^10.7.0", "typescript": "^4.6.3" } diff --git a/cli/src/commands/create.ts b/cli/src/commands/create.ts index 5ff6a878..4a35deac 100644 --- a/cli/src/commands/create.ts +++ b/cli/src/commands/create.ts @@ -39,6 +39,13 @@ export const createCommand: CommandModule = { const answers = await basic.runPrompts(); const results = await basic.runActions(answers); - console.log('results', results); + + console.log('操作变更:'); + console.log(results.changes.map((change) => change.path).join('\n')); + + if (results.failures.length > 0) { + console.log('操作失败:'); + console.log(results.failures); + } }, }; diff --git a/cli/src/commands/declaration.ts b/cli/src/commands/declaration.ts index 8313ccc3..e2bdbda7 100644 --- a/cli/src/commands/declaration.ts +++ b/cli/src/commands/declaration.ts @@ -48,7 +48,7 @@ export const declarationCommand: CommandModule = { content = await got .get( - 'https://raw.githubusercontent.com/msgbyte/tailchat/master/web/tailchat.d.ts' + 'https://raw.githubusercontent.com/msgbyte/tailchat/master/client/web/tailchat.d.ts' ) .then((res) => res.body); diff --git a/cli/templates/plopfile.js b/cli/templates/plopfile.js index 38be9d13..07d6001a 100644 --- a/cli/templates/plopfile.js +++ b/cli/templates/plopfile.js @@ -82,4 +82,31 @@ module.exports = function ( }, ], }); + + // 服务端插件的前端模板代码 + plop.setGenerator('server-plugin-full', { + description: '服务端插件的完整模板代码', + prompts: [ + { + type: 'input', + name: 'name', + require: true, + message: '插件名称', + }, + ...serverPrompts, + ], + actions: [ + { + type: 'addMany', + destination: path.resolve(process.cwd(), './plugins'), + base: './server-plugin-full', + templateFiles: [ + './server-plugin-full/**/*', + './server-plugin-full/*/.ministarrc.js', + ], + skipIfExists: true, + globOptions: {}, + }, + ], + }); }; diff --git a/cli/templates/server-plugin-full/{{id}}/.ministarrc.js b/cli/templates/server-plugin-full/{{id}}/.ministarrc.js new file mode 100644 index 00000000..ab39d260 --- /dev/null +++ b/cli/templates/server-plugin-full/{{id}}/.ministarrc.js @@ -0,0 +1,7 @@ +const path = require('path'); + +module.exports = { + externalDeps: ['react'], + pluginRoot: path.resolve(__dirname, './web'), + outDir: path.resolve(__dirname, '../../public'), +}; diff --git a/cli/templates/server-plugin-full/{{id}}/models/{{pickPluginName id}}.ts b/cli/templates/server-plugin-full/{{id}}/models/{{pickPluginName id}}.ts new file mode 100644 index 00000000..75159f78 --- /dev/null +++ b/cli/templates/server-plugin-full/{{id}}/models/{{pickPluginName id}}.ts @@ -0,0 +1,20 @@ +import { db } from 'tailchat-server-sdk'; +const { getModelForClass, prop, modelOptions, TimeStamps } = db; + +@modelOptions({ + options: { + customName: 'p_{{pickPluginName id}}', + }, +}) +export class {{pickPluginNameUp id}} extends TimeStamps implements db.Base { + _id: db.Types.ObjectId; + id: string; +} + +export type {{pickPluginNameUp id}}Document = db.DocumentType<{{pickPluginNameUp id}}>; + +const model = getModelForClass({{pickPluginNameUp id}}); + +export type {{pickPluginNameUp id}}Model = typeof model; + +export default model; diff --git a/cli/templates/server-plugin-full/{{id}}/package.json b/cli/templates/server-plugin-full/{{id}}/package.json new file mode 100644 index 00000000..a73dbd51 --- /dev/null +++ b/cli/templates/server-plugin-full/{{id}}/package.json @@ -0,0 +1,12 @@ +{ + "name": "tailchat-plugin-{{pickPluginName id}}", + "version": "1.0.0", + "main": "index.js", + "author": "{{author}}", + "description": "{{desc}}", + "license": "MIT", + "private": true, + "scripts": {}, + "devDependencies": {}, + "dependencies": {} +} diff --git a/cli/templates/server-plugin-full/{{id}}/services/{{pickPluginName id}}.service.ts b/cli/templates/server-plugin-full/{{id}}/services/{{pickPluginName id}}.service.ts new file mode 100644 index 00000000..d31f3c97 --- /dev/null +++ b/cli/templates/server-plugin-full/{{id}}/services/{{pickPluginName id}}.service.ts @@ -0,0 +1,20 @@ +import { TcService, TcDbService } from 'tailchat-server-sdk'; +import type { {{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model } from '../models/{{pickPluginName id}}'; + +/** + * 任务管理服务 + */ +interface {{pickPluginNameUp id}}Service + extends TcService, + TcDbService<{{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model> {} +class {{pickPluginNameUp id}}Service extends TcService { + get serviceName() { + return 'plugin:{{id}}'; + } + + onInit() { + this.registerLocalDb(require('../models/{{pickPluginName id}}').default); + } +} + +export default {{pickPluginNameUp id}}Service; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6f9c71bd..ddd25429 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -101,7 +101,6 @@ importers: pretty-ms: 7.0.1 react: 18.2.0 tailchat-server-sdk: link:../server/packages/sdk - tailchat-shared: link:../client/shared yargs: 17.5.1 devDependencies: '@types/fs-extra': 9.0.13 @@ -110,6 +109,7 @@ importers: '@types/node': 16.11.7 '@types/react': 18.0.20 '@types/yargs': 17.0.12 + tailchat-shared: link:../client/shared ts-node: 10.9.1_rk33jgomelnuriwr3foeinccb4 typescript: 4.8.2