优化
This commit is contained in:
106
apps/cli/.gitignore
vendored
Normal file
106
apps/cli/.gitignore
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
lib
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
2
apps/cli/.npmrc
Normal file
2
apps/cli/.npmrc
Normal file
@@ -0,0 +1,2 @@
|
||||
# https://npmmirror.com/
|
||||
registry = https://registry.npmmirror.com
|
||||
21
apps/cli/LICENSE
Normal file
21
apps/cli/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 MsgByte
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
16
apps/cli/README.md
Normal file
16
apps/cli/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# tailchat-cli
|
||||
A Command line interface of tailchat
|
||||
|
||||
```bash
|
||||
tailchat <command>
|
||||
|
||||
Commands:
|
||||
tailchat create [template] 创建 Tailchat 项目代码
|
||||
tailchat connect 连接到 Tailchat 节点网络
|
||||
tailchat bench 压力测试
|
||||
tailchat declaration <source> Tailchat 插件类型声明
|
||||
|
||||
Options:
|
||||
--version Show version number [boolean]
|
||||
-h, --help Show help [boolean]
|
||||
```
|
||||
3
apps/cli/bin/cli
Executable file
3
apps/cli/bin/cli
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../lib')
|
||||
87
apps/cli/package.json
Normal file
87
apps/cli/package.json
Normal file
@@ -0,0 +1,87 @@
|
||||
{
|
||||
"name": "tailchat-cli",
|
||||
"version": "1.5.14",
|
||||
"description": "A Command line interface of tailchat",
|
||||
"bin": {
|
||||
"tailchat": "./bin/cli"
|
||||
},
|
||||
"main": "./lib/index.js",
|
||||
"types": "./lib/index.d.ts",
|
||||
"files": [
|
||||
"lib",
|
||||
"bin",
|
||||
"templates"
|
||||
],
|
||||
"scripts": {
|
||||
"dev": "cross-env NODE_ENV=development ts-node ./src/index.ts",
|
||||
"build": "rimraf -rf lib && tsc",
|
||||
"prepare": "npm run build",
|
||||
"release": "npm publish --registry https://registry.npmjs.com/",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/msgbyte/tailchat-cli.git"
|
||||
},
|
||||
"keywords": [
|
||||
"tailchat"
|
||||
],
|
||||
"author": "moonrailgun",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/msgbyte/tailchat-cli/issues"
|
||||
},
|
||||
"homepage": "https://github.com/msgbyte/tailchat-cli#readme",
|
||||
"dependencies": {
|
||||
"@types/dockerode": "^3.3.10",
|
||||
"@types/pidusage": "^2.0.2",
|
||||
"as-table": "^1.0.55",
|
||||
"chalk": "4.1.2",
|
||||
"crypto-random-string": "3.3.1",
|
||||
"dockerode": "^3.3.4",
|
||||
"dotenv": "^16.0.0",
|
||||
"filesize": "^8.0.7",
|
||||
"find-process": "^1.4.7",
|
||||
"fs-extra": "^10.1.0",
|
||||
"glob": "^8.1.0",
|
||||
"got": "11.8.5",
|
||||
"ink": "^3.2.0",
|
||||
"ink-tab": "^4.2.0",
|
||||
"ink-text-input": "^4.0.3",
|
||||
"inquirer": "^8.2.2",
|
||||
"lodash": "^4.17.21",
|
||||
"node-plop": "^0.26.3",
|
||||
"nodemailer": "^6.7.2",
|
||||
"ora": "5.4.1",
|
||||
"p-all": "2.1.0",
|
||||
"p-map": "^4.0.0",
|
||||
"p-series": "2.1.0",
|
||||
"pidusage": "^3.0.2",
|
||||
"plop": "^3.0.5",
|
||||
"pretty-ms": "7.0.1",
|
||||
"react": "18.2.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"socket.io-client": "^4.6.2",
|
||||
"socket.io-msgpack-parser": "^3.0.2",
|
||||
"spinnies": "^0.5.1",
|
||||
"tailchat-server-sdk": "^0.0.12",
|
||||
"update-notifier": "5.1.0",
|
||||
"yargs": "^17.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^9.0.13",
|
||||
"@types/glob": "^8.0.0",
|
||||
"@types/inquirer": "^8.2.1",
|
||||
"@types/lodash": "^4.14.170",
|
||||
"@types/node": "^18.13.0",
|
||||
"@types/nodemailer": "^6.4.4",
|
||||
"@types/react": "18.0.20",
|
||||
"@types/spinnies": "^0.5.0",
|
||||
"@types/update-notifier": "^6.0.1",
|
||||
"@types/yargs": "^17.0.10",
|
||||
"cross-env": "^7.0.3",
|
||||
"tailchat-shared": "workspace:*",
|
||||
"ts-node": "^10.7.0",
|
||||
"typescript": "^4.6.3"
|
||||
}
|
||||
}
|
||||
46
apps/cli/src/app/App.tsx
Normal file
46
apps/cli/src/app/App.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import React, { useLayoutEffect, useState } from 'react';
|
||||
import { Box, Text, useStdout } from 'ink';
|
||||
import { Tabs, Tab } from 'ink-tab';
|
||||
import TextInput from 'ink-text-input';
|
||||
import { useScreenSize } from './hooks/useScreenSize';
|
||||
|
||||
export const App: React.FC = React.memo(() => {
|
||||
const [text, setText] = useState('');
|
||||
const { height, width } = useScreenSize();
|
||||
const { stdout } = useStdout();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
stdout?.write('\x1b[?1049h');
|
||||
|
||||
return () => {
|
||||
stdout?.write('\x1b[?1049l');
|
||||
};
|
||||
}, [stdout]);
|
||||
|
||||
return (
|
||||
<Box
|
||||
height={height}
|
||||
width={width}
|
||||
borderStyle="round"
|
||||
borderColor="green"
|
||||
flexDirection="column"
|
||||
>
|
||||
<Box>
|
||||
<TextInput value={text} onChange={setText} />
|
||||
</Box>
|
||||
|
||||
<Box>
|
||||
<Tabs flexDirection="column" onChange={() => {}}>
|
||||
{/* Temporary comments due to react version issues */}
|
||||
{/* <Tab name="tab1">
|
||||
<Text>Foo</Text>
|
||||
</Tab>
|
||||
<Tab name="tab2">
|
||||
<Text>Bar</Text>
|
||||
</Tab> */}
|
||||
</Tabs>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
});
|
||||
App.displayName = 'App';
|
||||
25
apps/cli/src/app/hooks/useScreenSize.ts
Normal file
25
apps/cli/src/app/hooks/useScreenSize.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useStdout } from 'ink';
|
||||
|
||||
export function useScreenSize() {
|
||||
const { stdout } = useStdout();
|
||||
const getSize = useCallback(
|
||||
() => ({
|
||||
height: stdout?.rows ?? 0,
|
||||
width: stdout?.columns ?? 0,
|
||||
}),
|
||||
[stdout]
|
||||
);
|
||||
const [size, setSize] = useState(getSize);
|
||||
|
||||
useEffect(() => {
|
||||
const onResize = () => setSize(getSize());
|
||||
stdout?.on('resize', onResize);
|
||||
|
||||
return () => {
|
||||
stdout?.off('resize', onResize);
|
||||
};
|
||||
}, [stdout, getSize]);
|
||||
|
||||
return size;
|
||||
}
|
||||
9
apps/cli/src/app/index.tsx
Normal file
9
apps/cli/src/app/index.tsx
Normal file
@@ -0,0 +1,9 @@
|
||||
import React from 'react';
|
||||
import { App } from './App';
|
||||
import { render } from 'ink';
|
||||
|
||||
export function run() {
|
||||
const ins = render(<App />);
|
||||
|
||||
return ins;
|
||||
}
|
||||
12
apps/cli/src/commands/app.ts
Normal file
12
apps/cli/src/commands/app.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { run } from '../app';
|
||||
import { isDev } from '../utils';
|
||||
|
||||
export const appCommand: CommandModule = {
|
||||
command: 'app',
|
||||
describe: isDev() ? false : 'Tailchat cli(WIP)',
|
||||
builder: undefined,
|
||||
async handler() {
|
||||
await run();
|
||||
},
|
||||
};
|
||||
173
apps/cli/src/commands/benchmark/connections.ts
Normal file
173
apps/cli/src/commands/benchmark/connections.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import msgpackParser from 'socket.io-msgpack-parser';
|
||||
import fs from 'fs-extra';
|
||||
import ora from 'ora';
|
||||
import randomString from 'crypto-random-string';
|
||||
import pMap from 'p-map';
|
||||
|
||||
const CLIENT_CREATION_INTERVAL_IN_MS = 5;
|
||||
|
||||
export const benchmarkConnectionsCommand: CommandModule = {
|
||||
command: 'connections <url>',
|
||||
describe: 'Test Tailchat Connections',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.demandOption('url', 'Backend Url')
|
||||
.option('file', {
|
||||
describe: 'Account Token Path',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
default: './accounts',
|
||||
})
|
||||
.option('concurrency', {
|
||||
describe: 'Concurrency when create connection',
|
||||
type: 'number',
|
||||
default: 1,
|
||||
})
|
||||
.option('groupId', {
|
||||
describe: 'Group Id which send Message',
|
||||
type: 'string',
|
||||
})
|
||||
.option('converseId', {
|
||||
describe: 'Converse Id which send Message',
|
||||
type: 'string',
|
||||
})
|
||||
.option('messageNum', {
|
||||
describe: 'Times which send Message',
|
||||
type: 'number',
|
||||
default: 1,
|
||||
}),
|
||||
async handler(args) {
|
||||
const url = args.url as string;
|
||||
const file = args.file as string;
|
||||
const groupId = args.groupId as string;
|
||||
const converseId = args.converseId as string;
|
||||
const messageNum = args.messageNum as number;
|
||||
const concurrency = args.concurrency as number;
|
||||
|
||||
console.log('Reading account tokens from', file);
|
||||
const account = await fs.readFile(file as string, {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
const sockets = await createClients(
|
||||
url as string,
|
||||
account.split('\n').map((s) => s.trim()),
|
||||
concurrency
|
||||
);
|
||||
|
||||
if (groupId && converseId) {
|
||||
// send message test
|
||||
for (let i = 0; i < messageNum; i++) {
|
||||
console.log('Start send message test:', i + 1);
|
||||
await sendMessage(sockets, groupId, converseId);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
async function createClients(
|
||||
url: string,
|
||||
accountTokens: string[],
|
||||
concurrency: number
|
||||
): Promise<Socket[]> {
|
||||
const maxCount = accountTokens.length;
|
||||
const spinner = ora().info(`Create Client Connection to ${url}`).start();
|
||||
|
||||
let i = 0;
|
||||
const sockets: Socket[] = [];
|
||||
await pMap(
|
||||
accountTokens,
|
||||
async (token) => {
|
||||
await sleep(CLIENT_CREATION_INTERVAL_IN_MS);
|
||||
const socket = await createClient(url, token);
|
||||
spinner.text = `Progress: ${++i}/${maxCount}`;
|
||||
sockets.push(socket);
|
||||
},
|
||||
{
|
||||
concurrency,
|
||||
}
|
||||
);
|
||||
|
||||
spinner.succeed(`${maxCount} clients has been create.`);
|
||||
|
||||
return sockets;
|
||||
}
|
||||
|
||||
function createClient(url: string, token: string): Promise<Socket> {
|
||||
return new Promise<Socket>((resolve, reject) => {
|
||||
const socket = io(url, {
|
||||
transports: ['websocket'],
|
||||
auth: {
|
||||
token,
|
||||
},
|
||||
forceNew: true,
|
||||
parser: msgpackParser,
|
||||
});
|
||||
socket.once('connect', () => {
|
||||
// 连接成功
|
||||
resolve(socket);
|
||||
});
|
||||
socket.once('error', () => {
|
||||
reject();
|
||||
});
|
||||
|
||||
socket.on('disconnect', (reason) => {
|
||||
console.log(`disconnect due to ${reason}`);
|
||||
});
|
||||
}).then(async (socket) => {
|
||||
await socket.emitWithAck('chat.converse.findAndJoinRoom', {});
|
||||
|
||||
return socket;
|
||||
});
|
||||
}
|
||||
|
||||
async function sendMessage(
|
||||
sockets: Socket[],
|
||||
groupId: string,
|
||||
converseId: string
|
||||
) {
|
||||
return new Promise<void>((resolve) => {
|
||||
const randomMessage = randomString({ length: 16 });
|
||||
const spinner = ora()
|
||||
.info(`Start message receive test, message: ${randomMessage}`)
|
||||
.start();
|
||||
const start = Date.now();
|
||||
let receiveCount = 0;
|
||||
const len = sockets.length;
|
||||
|
||||
function receivedCallback() {
|
||||
receiveCount += 1;
|
||||
spinner.text = `Receive: ${receiveCount}/${len}`;
|
||||
|
||||
if (receiveCount === len) {
|
||||
spinner.succeed(`All client received, usage: ${Date.now() - start}ms`);
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
|
||||
sockets.forEach((socket) => {
|
||||
socket.on('notify:chat.message.add', (message) => {
|
||||
const content = message.content;
|
||||
|
||||
if (message.converseId === converseId && randomMessage === content) {
|
||||
socket.off('notify:chat.message.add');
|
||||
|
||||
receivedCallback();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
sockets[0].emit('chat.message.sendMessage', {
|
||||
groupId,
|
||||
converseId,
|
||||
content: randomMessage,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function sleep(milliseconds: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, milliseconds);
|
||||
});
|
||||
}
|
||||
18
apps/cli/src/commands/benchmark/index.ts
Normal file
18
apps/cli/src/commands/benchmark/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { benchmarkConnectionsCommand } from './connections';
|
||||
import { benchmarkMessageCommand } from './message';
|
||||
import { benchmarkRegisterCommand } from './register';
|
||||
|
||||
// https://docs.docker.com/engine/api/v1.41/
|
||||
|
||||
export const benchmarkCommand: CommandModule = {
|
||||
command: 'benchmark',
|
||||
describe: 'Tailchat Benchmark Test',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.command(benchmarkMessageCommand)
|
||||
.command(benchmarkConnectionsCommand)
|
||||
.command(benchmarkRegisterCommand)
|
||||
.demandCommand(),
|
||||
handler(args) {},
|
||||
};
|
||||
178
apps/cli/src/commands/benchmark/message.ts
Normal file
178
apps/cli/src/commands/benchmark/message.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { TcBroker } from 'tailchat-server-sdk';
|
||||
import defaultBrokerConfig from 'tailchat-server-sdk/dist/runner/moleculer.config';
|
||||
import { config } from 'dotenv';
|
||||
import _ from 'lodash';
|
||||
import os from 'os';
|
||||
import pAll from 'p-all';
|
||||
import pSeries from 'p-series';
|
||||
import ora from 'ora';
|
||||
import prettyMs from 'pretty-ms';
|
||||
import filesize from 'filesize';
|
||||
|
||||
export const benchmarkMessageCommand: CommandModule = {
|
||||
command: 'message',
|
||||
describe:
|
||||
'Stress testing through Tailchat network requests (suitable for pure business testing)',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.option('groupId', {
|
||||
describe: 'Group ID',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
})
|
||||
.option('converseId', {
|
||||
describe: 'Converse ID',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
})
|
||||
.option('userId', {
|
||||
describe: 'User ID',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
})
|
||||
.option('num', {
|
||||
describe: 'Test Num',
|
||||
type: 'number',
|
||||
default: 100,
|
||||
})
|
||||
.option('parallel', {
|
||||
describe: 'Is Parallel',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
})
|
||||
.option('parallelLimit', {
|
||||
describe: 'Parallel Limit',
|
||||
type: 'number',
|
||||
default: Infinity,
|
||||
}),
|
||||
async handler(args) {
|
||||
config(); // 加载环境变量
|
||||
|
||||
const broker = new TcBroker({
|
||||
...defaultBrokerConfig,
|
||||
transporter: process.env.TRANSPORTER,
|
||||
logger: false,
|
||||
});
|
||||
await broker.start();
|
||||
|
||||
printSystemInfo();
|
||||
|
||||
console.log('===============');
|
||||
|
||||
await startBenchmark<number>({
|
||||
parallel: args.parallel as boolean,
|
||||
parallelLimit: args.parallelLimit as number,
|
||||
number: args.num as number,
|
||||
task: async (i) => {
|
||||
const start = process.hrtime();
|
||||
await broker.call(
|
||||
'chat.message.sendMessage',
|
||||
{
|
||||
converseId: args.converseId,
|
||||
groupId: args.groupId,
|
||||
content: `benchmessage ${i + 1}`,
|
||||
},
|
||||
{
|
||||
meta: {
|
||||
userId: args.userId,
|
||||
},
|
||||
}
|
||||
);
|
||||
const usage = calcUsage(start);
|
||||
|
||||
return usage;
|
||||
},
|
||||
onCompleted: (res) => {
|
||||
console.log(`Test Num: \t${res.length}`);
|
||||
console.log(`Max Usage: \t${prettyMs(Math.max(...res, 0))}`);
|
||||
console.log(`Min Usage: \t${prettyMs(Math.min(...res, 0))}`);
|
||||
console.log(`Average time: \t${prettyMs(_.mean(res))}`);
|
||||
},
|
||||
});
|
||||
|
||||
await broker.stop();
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* 打印系统信息
|
||||
*/
|
||||
function printSystemInfo() {
|
||||
console.log(`Host: \t${os.hostname()}`);
|
||||
console.log(`System: \t${os.type()} - ${os.release()}`);
|
||||
console.log(`Architecture: \t${os.arch()} - ${os.version()}`);
|
||||
console.log(`CPU: \t${os.cpus().length}`);
|
||||
console.log(`Memory: \t${filesize(os.totalmem(), { base: 2 })}`);
|
||||
}
|
||||
|
||||
function calcUsage(startTime: [number, number]) {
|
||||
const diff = process.hrtime(startTime);
|
||||
const usage = (diff[0] + diff[1] / 1e9) * 1000;
|
||||
|
||||
return usage;
|
||||
}
|
||||
|
||||
interface BenchmarkOptions<T> {
|
||||
parallel: boolean; // 是否并发
|
||||
parallelLimit?: number; // 并发上限, 默认不限制(Infinity)
|
||||
task: (index: number) => Promise<T>;
|
||||
number?: number;
|
||||
onCompleted: (res: T[]) => void;
|
||||
}
|
||||
/**
|
||||
* 开始一次基准测试
|
||||
*/
|
||||
async function startBenchmark<T>(options: BenchmarkOptions<T>) {
|
||||
const {
|
||||
parallel,
|
||||
parallelLimit = Infinity,
|
||||
task,
|
||||
number = 100,
|
||||
onCompleted,
|
||||
} = options;
|
||||
|
||||
const spinner = ora();
|
||||
|
||||
spinner.info(
|
||||
`Test mode: ${
|
||||
parallel ? `parallel, parallel limit ${parallelLimit}` : `serial`
|
||||
}`
|
||||
);
|
||||
spinner.info(`Number of tasks to execute: ${number}`);
|
||||
spinner.start('Benchmark in progress...');
|
||||
try {
|
||||
const startTime = process.hrtime();
|
||||
let res: (T | false)[] = [];
|
||||
if (parallel) {
|
||||
res = await pAll<T | false>(
|
||||
[
|
||||
...Array.from({ length: number }).map(
|
||||
(_, i) => () => task(i).catch(() => false as const)
|
||||
),
|
||||
],
|
||||
{
|
||||
concurrency: parallelLimit,
|
||||
}
|
||||
);
|
||||
} else {
|
||||
res = await pSeries<T | false>([
|
||||
...Array.from({ length: number }).map(
|
||||
(_, i) => () => task(i).catch(() => false as const)
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
const allUsage = calcUsage(startTime);
|
||||
const succeed = res.filter((i): i is T => Boolean(i));
|
||||
const failed = res.filter((i) => !Boolean(i));
|
||||
spinner.succeed(`Benchmarking is complete, usage: ${prettyMs(allUsage)}`);
|
||||
console.log(`Success/Failed: ${succeed.length}/${failed.length}`);
|
||||
console.log(`TPS: ${res.length / (allUsage / 1000)}`);
|
||||
|
||||
onCompleted(succeed);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
spinner.fail(`A problem with benchmarking`).stop();
|
||||
}
|
||||
}
|
||||
116
apps/cli/src/commands/benchmark/register.ts
Normal file
116
apps/cli/src/commands/benchmark/register.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import fs from 'fs-extra';
|
||||
import got from 'got';
|
||||
import ora from 'ora';
|
||||
import pMap from 'p-map';
|
||||
|
||||
export const benchmarkRegisterCommand: CommandModule = {
|
||||
command: 'register <url>',
|
||||
describe: 'Create Tailchat temporary account and output token',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.example(
|
||||
'$0 benchmark register http://127.0.0.1:11000',
|
||||
'Register account in local server'
|
||||
)
|
||||
.demandOption('url', 'Backend Url')
|
||||
.option('file', {
|
||||
describe: 'Account Token Path',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
default: './accounts',
|
||||
})
|
||||
.option('count', {
|
||||
describe: 'Register Count',
|
||||
demandOption: true,
|
||||
type: 'number',
|
||||
default: 100,
|
||||
})
|
||||
.option('concurrency', {
|
||||
describe: 'Concurrency when send request',
|
||||
type: 'number',
|
||||
default: 1,
|
||||
})
|
||||
.option('invite', {
|
||||
describe: 'Invite Code',
|
||||
type: 'string',
|
||||
})
|
||||
.option('append', {
|
||||
describe: 'Append mode',
|
||||
type: 'boolean',
|
||||
}),
|
||||
async handler(args) {
|
||||
const url = args.url as string;
|
||||
const file = args.file as string;
|
||||
const count = args.count as number;
|
||||
const concurrency = args.concurrency as number;
|
||||
const invite = args.invite as string | undefined;
|
||||
const append = (args.append ?? false) as boolean;
|
||||
const tokens: string[] = [];
|
||||
const start = Date.now();
|
||||
|
||||
const spinner = ora().info(`Register temporary account`).start();
|
||||
|
||||
let i = 0;
|
||||
spinner.text = `Progress: ${i}/${count}`;
|
||||
await pMap(
|
||||
Array.from({ length: count }),
|
||||
async () => {
|
||||
const token = await registerTemporaryAccount(url, `benchUser-${i}`);
|
||||
if (invite) {
|
||||
// Apply group invite
|
||||
await applyGroupInviteCode(url, token, invite);
|
||||
}
|
||||
if (append) {
|
||||
await fs.appendFile(file, `\n${token}`);
|
||||
}
|
||||
|
||||
spinner.text = `Progress: ${++i}/${count}`;
|
||||
tokens.push(token);
|
||||
},
|
||||
{
|
||||
concurrency,
|
||||
}
|
||||
);
|
||||
|
||||
if (!append) {
|
||||
spinner.info(`Writing tokens into path: ${file}`);
|
||||
|
||||
await fs.writeFile(file, tokens.join('\n'));
|
||||
}
|
||||
|
||||
spinner.succeed(`Register completed! Usage: ${Date.now() - start}ms`);
|
||||
},
|
||||
};
|
||||
|
||||
async function registerTemporaryAccount(
|
||||
url: string,
|
||||
nickname: string
|
||||
): Promise<string> {
|
||||
const res = await got
|
||||
.post(`${url}/api/user/createTemporaryUser`, {
|
||||
json: {
|
||||
nickname,
|
||||
},
|
||||
retry: 5,
|
||||
})
|
||||
.json<{ data: { token: string } }>();
|
||||
|
||||
return res.data.token;
|
||||
}
|
||||
|
||||
async function applyGroupInviteCode(
|
||||
url: string,
|
||||
token: string,
|
||||
inviteCode: string
|
||||
) {
|
||||
await got.post(`${url}/api/group/invite/applyInvite`, {
|
||||
json: {
|
||||
code: inviteCode,
|
||||
},
|
||||
retry: 5,
|
||||
headers: {
|
||||
'X-Token': token,
|
||||
},
|
||||
});
|
||||
}
|
||||
20
apps/cli/src/commands/connect.ts
Normal file
20
apps/cli/src/commands/connect.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { TcBroker } from 'tailchat-server-sdk';
|
||||
import defaultBrokerConfig from 'tailchat-server-sdk/dist/runner/moleculer.config';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
export const connectCommand: CommandModule = {
|
||||
command: 'connect',
|
||||
describe: 'Connect to Tailchat network',
|
||||
builder: undefined,
|
||||
async handler(args) {
|
||||
config();
|
||||
|
||||
const broker = new TcBroker({
|
||||
...defaultBrokerConfig,
|
||||
transporter: process.env.TRANSPORTER,
|
||||
});
|
||||
await broker.start();
|
||||
broker.repl();
|
||||
},
|
||||
};
|
||||
51
apps/cli/src/commands/create.ts
Normal file
51
apps/cli/src/commands/create.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import nodePlop from 'node-plop';
|
||||
import path from 'path';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
const plop = nodePlop(path.resolve(__dirname, '../../templates/plopfile.js'));
|
||||
|
||||
export const createCommand: CommandModule = {
|
||||
command: 'create [template]',
|
||||
describe: 'Create Tailchat repo code',
|
||||
builder: (yargs) =>
|
||||
yargs.positional('template', {
|
||||
demandOption: true,
|
||||
description: 'Template Name',
|
||||
type: 'string',
|
||||
choices: plop.getGeneratorList().map((v) => v.name),
|
||||
}),
|
||||
async handler(args) {
|
||||
let template: string;
|
||||
|
||||
if (!args.template) {
|
||||
const res = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'template',
|
||||
message: 'Choose Template',
|
||||
choices: plop.getGeneratorList().map((v) => ({
|
||||
name: `${v.name} (${v.description})`,
|
||||
value: v.name,
|
||||
})),
|
||||
},
|
||||
]);
|
||||
template = String(res.template);
|
||||
} else {
|
||||
template = String(args.template);
|
||||
}
|
||||
|
||||
const basic = plop.getGenerator(template);
|
||||
|
||||
const answers = await basic.runPrompts();
|
||||
const results = await basic.runActions(answers);
|
||||
|
||||
console.log('Changes:');
|
||||
console.log(results.changes.map((change) => change.path).join('\n'));
|
||||
|
||||
if (results.failures.length > 0) {
|
||||
console.log('Operation failed:');
|
||||
console.log(results.failures);
|
||||
}
|
||||
},
|
||||
};
|
||||
67
apps/cli/src/commands/declaration.ts
Normal file
67
apps/cli/src/commands/declaration.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import inquirer from 'inquirer';
|
||||
import { CommandModule } from 'yargs';
|
||||
import fs, { mkdirp } from 'fs-extra';
|
||||
import path from 'path';
|
||||
import ora from 'ora';
|
||||
import got from 'got';
|
||||
|
||||
const onlineDeclarationUrl =
|
||||
'https://raw.githubusercontent.com/msgbyte/tailchat/master/client/web/tailchat.d.ts';
|
||||
export const declarationCommand: CommandModule = {
|
||||
command: 'declaration <source>',
|
||||
describe: 'Tailchat plugin type declaration',
|
||||
builder: (yargs) =>
|
||||
yargs.positional('source', {
|
||||
demandOption: true,
|
||||
description: 'Declaration Type Source',
|
||||
type: 'string',
|
||||
choices: ['empty', 'github'],
|
||||
}),
|
||||
async handler(args) {
|
||||
let source = String(args.source);
|
||||
|
||||
if (!source) {
|
||||
const res = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'source',
|
||||
message: 'Select type source',
|
||||
choices: [
|
||||
{
|
||||
name: 'Empty',
|
||||
value: 'empty',
|
||||
},
|
||||
{
|
||||
name: 'Download the full statement from Github',
|
||||
value: 'github',
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
source = String(res.source);
|
||||
}
|
||||
|
||||
let content = '';
|
||||
if (source === 'empty') {
|
||||
content =
|
||||
"declare module '@capital/common';\ndeclare module '@capital/component';\n";
|
||||
} else if (source === 'github') {
|
||||
const url = onlineDeclarationUrl;
|
||||
|
||||
const spinner = ora(
|
||||
`Downloading plugin type declarations from Github: ${url}`
|
||||
).start();
|
||||
|
||||
content = await got.get(url).then((res) => res.body);
|
||||
|
||||
spinner.succeed('The declaration file has been downloaded');
|
||||
}
|
||||
|
||||
if (content !== '') {
|
||||
const target = path.resolve(process.cwd(), './types/tailchat.d.ts');
|
||||
await mkdirp(path.dirname(target));
|
||||
await fs.writeFile(target, content);
|
||||
console.log('Writing type file complete:', target);
|
||||
}
|
||||
},
|
||||
};
|
||||
32
apps/cli/src/commands/docker/doctor.ts
Normal file
32
apps/cli/src/commands/docker/doctor.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import Docker from 'dockerode';
|
||||
import asTable from 'as-table';
|
||||
import filesize from 'filesize';
|
||||
|
||||
export const dockerDoctorCommand: CommandModule = {
|
||||
command: 'doctor',
|
||||
describe: 'docker environment check',
|
||||
builder: undefined,
|
||||
async handler(args) {
|
||||
const docker = new Docker();
|
||||
|
||||
const images = await docker.listImages();
|
||||
const tailchatImages = images.filter((image) =>
|
||||
(image.RepoTags ?? []).some((tag) => tag.includes('tailchat'))
|
||||
);
|
||||
|
||||
console.log('Tailchat image list:');
|
||||
console.log(
|
||||
asTable.configure({ delimiter: ' | ' })(
|
||||
tailchatImages.map((image) => ({
|
||||
tag: (image.RepoTags ?? []).join(','),
|
||||
id: image.Id.substring(0, 12),
|
||||
size: filesize(image.Size),
|
||||
}))
|
||||
)
|
||||
);
|
||||
|
||||
const info = await docker.info();
|
||||
console.log('info', info);
|
||||
},
|
||||
};
|
||||
18
apps/cli/src/commands/docker/index.ts
Normal file
18
apps/cli/src/commands/docker/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { dockerInitCommand } from './init';
|
||||
import { dockerUpdateCommand } from './update';
|
||||
import { dockerDoctorCommand } from './doctor';
|
||||
|
||||
// https://docs.docker.com/engine/api/v1.41/
|
||||
|
||||
export const dockerCommand: CommandModule = {
|
||||
command: 'docker',
|
||||
describe: 'Tailchat image management',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.command(dockerInitCommand)
|
||||
.command(dockerDoctorCommand)
|
||||
.command(dockerUpdateCommand)
|
||||
.demandCommand(),
|
||||
handler(args) {},
|
||||
};
|
||||
185
apps/cli/src/commands/docker/init.ts
Normal file
185
apps/cli/src/commands/docker/init.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import ora from 'ora';
|
||||
import fs from 'fs-extra';
|
||||
import got from 'got';
|
||||
import path from 'path';
|
||||
import chalk from 'chalk';
|
||||
import inquirer from 'inquirer';
|
||||
import randomString from 'crypto-random-string';
|
||||
import { withGhProxy } from '../../utils';
|
||||
|
||||
// https://docs.docker.com/engine/api/v1.41/
|
||||
|
||||
const initWelcome = `================
|
||||
Initializing Tailchat configuration and environment variables for you
|
||||
A complete list of environment variables can be accessed at: ${chalk.underline(
|
||||
'https://tailchat.msgbyte.com/docs/deployment/environment'
|
||||
)} to learn more
|
||||
================`;
|
||||
|
||||
const initCompleted = (dir: string) =>
|
||||
chalk.green(`================
|
||||
Congratulations, you have successfully completed the configuration initialization, your configuration file is ready, and you are one step away from a successful deployment!
|
||||
|
||||
Your tailchat configuration files are stored in: ${chalk.underline(
|
||||
path.resolve(process.cwd(), dir)
|
||||
)}
|
||||
|
||||
Run the following command to complete the image download and start:
|
||||
- ${chalk.bold(`cd ${dir}`)} ${chalk.gray(
|
||||
'# Move to the installation directory'
|
||||
)}
|
||||
- ${chalk.bold('tailchat docker update')} ${chalk.gray(
|
||||
'# Download/update the official mirror'
|
||||
)}
|
||||
- ${chalk.bold('docker compose up -d')} ${chalk.gray('# Start service')}
|
||||
================`);
|
||||
|
||||
const envUrl =
|
||||
'https://raw.githubusercontent.com/msgbyte/tailchat/master/docker-compose.env';
|
||||
const configUrl =
|
||||
'https://raw.githubusercontent.com/msgbyte/tailchat/master/docker-compose.yml';
|
||||
|
||||
export const dockerInitCommand: CommandModule = {
|
||||
command: 'init',
|
||||
describe: 'Initialize Tailchat with docker configuration',
|
||||
builder: undefined,
|
||||
async handler(args) {
|
||||
const spinner = ora();
|
||||
try {
|
||||
console.log(initWelcome);
|
||||
const { dir, secret, apiUrl, fileLimit } = await inquirer.prompt([
|
||||
{
|
||||
name: 'dir',
|
||||
type: 'input',
|
||||
default: './tailchat',
|
||||
message: 'Configurate storage directory',
|
||||
},
|
||||
{
|
||||
name: 'secret',
|
||||
type: 'input',
|
||||
default: randomString({ length: 16 }),
|
||||
message:
|
||||
'(SECRET)Please enter any string, which will be used as the secret key for Tailchat to sign the user identity. Leaking this string will cause the risk of identity forgery. By default, a 16-digit string is randomly generated',
|
||||
},
|
||||
{
|
||||
name: 'apiUrl',
|
||||
type: 'input',
|
||||
message:
|
||||
'(API_URL)Please configure an address that can be accessed by the external network, which will affect the storage path and access address of the file, example: https://tailchat.example.com',
|
||||
},
|
||||
{
|
||||
name: 'fileLimit',
|
||||
type: 'number',
|
||||
default: 1048576,
|
||||
message:
|
||||
'(FILE_LIMIT)File upload volume limit, the default is 1048576(1m)',
|
||||
},
|
||||
]);
|
||||
|
||||
spinner.start('Start downloading the latest configuration file');
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [rawEnv, rawConfig] = await Promise.all([
|
||||
got(envUrl).then((res) => res.body),
|
||||
got(configUrl).then((res) => res.body),
|
||||
]);
|
||||
spinner.info('The configuration file is downloaded');
|
||||
|
||||
if (secret) {
|
||||
rawEnv = setEnvValue(rawEnv, 'SECRET', secret);
|
||||
}
|
||||
|
||||
if (apiUrl) {
|
||||
rawEnv = setEnvValue(rawEnv, 'API_URL', apiUrl);
|
||||
}
|
||||
|
||||
if (fileLimit) {
|
||||
rawEnv = setEnvValue(rawEnv, 'FILE_LIMIT', fileLimit);
|
||||
}
|
||||
|
||||
if (
|
||||
await promptConfirm(
|
||||
'Do you need to configure the email service? The email service can be used for functions such as password retrieval and email notification.'
|
||||
)
|
||||
) {
|
||||
const { stmpURI, stmpSender } = await inquirer.prompt([
|
||||
{
|
||||
name: 'stmpURI',
|
||||
type: 'input',
|
||||
message:
|
||||
'(SMTP_URI) Please configure the SMTP connection address of the mail service, for example: smtps://username:password@example.mailserver.com/?pool=true',
|
||||
},
|
||||
{
|
||||
name: 'stmpSender',
|
||||
type: 'input',
|
||||
message:
|
||||
'(SMTP_SENDER) Email sender, example: "Tailchat" tailchat@example.mailserver.com',
|
||||
},
|
||||
]);
|
||||
|
||||
if (stmpURI) {
|
||||
rawEnv = setEnvValue(rawEnv, 'SMTP_URI', stmpURI);
|
||||
}
|
||||
if (stmpSender) {
|
||||
rawEnv = setEnvValue(rawEnv, 'SMTP_SENDER', stmpSender);
|
||||
}
|
||||
|
||||
if (stmpURI && stmpSender) {
|
||||
if (
|
||||
await promptConfirm(
|
||||
'Do you need to enable email verification? After it is enabled, when the user registers, it is necessary to verify the email address and pass it before continuing to register'
|
||||
)
|
||||
) {
|
||||
rawEnv = setEnvValue(rawEnv, 'EMAIL_VERIFY', 'true');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spinner.info(`Creating directory ${dir} ...`);
|
||||
await fs.mkdirp(dir);
|
||||
|
||||
spinner.info('Writing configuration file ...');
|
||||
|
||||
await Promise.all([
|
||||
fs.writeFile(path.join(dir, 'docker-compose.env'), rawEnv),
|
||||
fs.writeFile(path.join(dir, 'docker-compose.yml'), rawConfig),
|
||||
]);
|
||||
spinner.succeed('The configuration is initialized');
|
||||
|
||||
console.log(initCompleted(dir));
|
||||
} catch (err) {
|
||||
spinner.fail('Unexpected initialization of Tailchat with docker');
|
||||
console.error(err);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置环境变量值
|
||||
*/
|
||||
function setEnvValue(text: string, key: string, value: string): string {
|
||||
const re = new RegExp(`${key}=(.*?)\n`);
|
||||
if (re.test(text)) {
|
||||
// 配置文件已经有了
|
||||
return text.replace(re, `${key}=${value}\n`);
|
||||
}
|
||||
|
||||
// 配置文件还没有
|
||||
return text + `\n${key}=${value}\n`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置更多配置的确认项
|
||||
*/
|
||||
async function promptConfirm(message: string): Promise<boolean> {
|
||||
const { res } = await inquirer.prompt([
|
||||
{
|
||||
name: 'res',
|
||||
type: 'confirm',
|
||||
message,
|
||||
default: false,
|
||||
},
|
||||
]);
|
||||
|
||||
return res;
|
||||
}
|
||||
108
apps/cli/src/commands/docker/update.ts
Normal file
108
apps/cli/src/commands/docker/update.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import ora from 'ora';
|
||||
import Docker from 'dockerode';
|
||||
import Spinnies from 'spinnies';
|
||||
|
||||
const remoteImageName = 'moonrailgun/tailchat:latest';
|
||||
const targetImage = {
|
||||
repo: 'tailchat',
|
||||
tag: 'latest',
|
||||
};
|
||||
const targetImageName = targetImage.repo + targetImage.tag;
|
||||
|
||||
export const dockerUpdateCommand: CommandModule = {
|
||||
command: 'update',
|
||||
describe: 'Update Tailchat image version',
|
||||
builder: undefined,
|
||||
async handler(args) {
|
||||
const docker = new Docker();
|
||||
|
||||
const spinner = ora().start('Start updating the image');
|
||||
|
||||
try {
|
||||
const pullSpinnies = new Spinnies();
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const taskMap = new Map<string, Spinnies.SpinnerOptions>();
|
||||
|
||||
// 这里有个类型问题,不会返回任何值
|
||||
docker.pull(remoteImageName, {}, (err, stream) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
spinner.info('The remote image has been found, start downloading');
|
||||
|
||||
docker.modem.followProgress(
|
||||
stream,
|
||||
(err, output) => {
|
||||
// onFinish
|
||||
// console.log('finish', err, output);
|
||||
|
||||
if (err) {
|
||||
pullSpinnies.stopAll('stopped');
|
||||
reject(err);
|
||||
} else {
|
||||
pullSpinnies.stopAll('succeed');
|
||||
|
||||
spinner.succeed(output[1]?.status);
|
||||
spinner.succeed(output[2]?.status);
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
(event) => {
|
||||
if (!event.id) {
|
||||
console.log(event.status); // 可能是完成后的信息打印,直接输出
|
||||
return;
|
||||
}
|
||||
const text = `[${event.id}] ${event.status}${
|
||||
event.progress ? ':' : ''
|
||||
} ${event.progress ?? ''}`;
|
||||
|
||||
// onProcess
|
||||
if (taskMap.has(event.id)) {
|
||||
pullSpinnies.update(event.id, {
|
||||
text,
|
||||
});
|
||||
|
||||
if (event.status === 'Pull complete') {
|
||||
pullSpinnies.succeed(event.id);
|
||||
}
|
||||
} else {
|
||||
taskMap.set(
|
||||
event.id,
|
||||
pullSpinnies.add(event.id, {
|
||||
text,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
spinner.succeed('Download image complete');
|
||||
|
||||
const image = docker.getImage(remoteImageName);
|
||||
|
||||
if (!image) {
|
||||
spinner.fail(
|
||||
'An exception occurred, the downloaded image was not found'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
spinner.info('Updating image tags');
|
||||
|
||||
await image.tag(targetImage);
|
||||
|
||||
spinner.succeed('The image tag has been updated');
|
||||
} catch (err) {
|
||||
spinner.fail(
|
||||
'An update error occurred, please check the network configuration'
|
||||
);
|
||||
console.error(err);
|
||||
}
|
||||
},
|
||||
};
|
||||
62
apps/cli/src/commands/registry/config.ts
Normal file
62
apps/cli/src/commands/registry/config.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import path from 'path';
|
||||
import glob from 'glob';
|
||||
import inquirer from 'inquirer';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
const feRegistryPath = path.join(process.cwd(), './client/web/registry.json');
|
||||
|
||||
export const registryConfigCommand: CommandModule = {
|
||||
command: 'config',
|
||||
describe:
|
||||
'config tailchat registry which can display in Tailchat, run it in tailchat root path',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.option('fe', {
|
||||
describe: 'Config FE Plugin List',
|
||||
type: 'boolean',
|
||||
})
|
||||
.option('verbose', {
|
||||
describe: 'Show plugin manifest path list',
|
||||
type: 'boolean',
|
||||
}),
|
||||
async handler(args) {
|
||||
const feplugins = glob.sync(
|
||||
path.join(process.cwd(), './client/web/plugins/*/manifest.json')
|
||||
);
|
||||
const beplugins = glob.sync(
|
||||
path.join(process.cwd(), './server/plugins/*/web/plugins/*/manifest.json')
|
||||
);
|
||||
|
||||
if (args.verbose) {
|
||||
console.log('feplugins', feplugins);
|
||||
console.log('beplugins', beplugins);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Scan plugins: fe(count: ${feplugins.length}) be(count: ${beplugins.length})`
|
||||
);
|
||||
|
||||
if (args.fe) {
|
||||
const alreadySelected = await fs.readJSON(feRegistryPath);
|
||||
const feInfos = await Promise.all(feplugins.map((p) => fs.readJSON(p)));
|
||||
const { selected: selectedInfo } = await inquirer.prompt([
|
||||
{
|
||||
name: 'selected',
|
||||
type: 'checkbox',
|
||||
default: alreadySelected
|
||||
.map((item: any) => feInfos.find((info) => info.name === item.name))
|
||||
.filter(Boolean),
|
||||
choices: feInfos.map((info) => ({
|
||||
name: `${info.name}(${info.version})`,
|
||||
value: info,
|
||||
})),
|
||||
},
|
||||
]);
|
||||
|
||||
console.log(`Selected ${selectedInfo.length} plugin.`);
|
||||
|
||||
await fs.writeJSON(feRegistryPath, selectedInfo, { spaces: 2 });
|
||||
}
|
||||
},
|
||||
};
|
||||
11
apps/cli/src/commands/registry/index.ts
Normal file
11
apps/cli/src/commands/registry/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { registryConfigCommand } from './config';
|
||||
|
||||
// https://docs.docker.com/engine/api/v1.41/
|
||||
|
||||
export const registryCommand: CommandModule = {
|
||||
command: 'registry',
|
||||
describe: 'Tailchat registry config',
|
||||
builder: (yargs) => yargs.command(registryConfigCommand).demandCommand(),
|
||||
handler(args) {},
|
||||
};
|
||||
103
apps/cli/src/commands/smtp.ts
Normal file
103
apps/cli/src/commands/smtp.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import { config } from 'dotenv';
|
||||
import inquirer from 'inquirer';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { parseConnectionUrl } from 'nodemailer/lib/shared';
|
||||
|
||||
export const smtpCommand: CommandModule = {
|
||||
command: 'smtp',
|
||||
describe: 'SMTP Service',
|
||||
builder: (yargs) =>
|
||||
yargs
|
||||
.command(
|
||||
'verify',
|
||||
'Verify smtp sender service',
|
||||
(yargs) => {},
|
||||
async (args) => {
|
||||
config(); // 加载环境变量
|
||||
|
||||
console.log(
|
||||
'This command will verify SMTP URI which use in tailchat, please put your URI which same like in tailchat env'
|
||||
);
|
||||
const { uri } = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'uri',
|
||||
message: 'SMTP_URI',
|
||||
default: process.env.SMTP_URI,
|
||||
validate: isValidStr,
|
||||
},
|
||||
]);
|
||||
|
||||
const transporter = nodemailer.createTransport(
|
||||
parseConnectionUrl(uri)
|
||||
);
|
||||
|
||||
try {
|
||||
const verify = await transporter.verify();
|
||||
console.log('Verify Result:', verify);
|
||||
} catch (err) {
|
||||
console.log('Verify Failed:', String(err));
|
||||
}
|
||||
}
|
||||
)
|
||||
.command(
|
||||
'test',
|
||||
'Send test email with smtp service',
|
||||
(yargs) => {},
|
||||
async (args) => {
|
||||
config(); // 加载环境变量
|
||||
|
||||
console.log(
|
||||
'This command will send test email to your own email, please put your info which same like in tailchat env'
|
||||
);
|
||||
|
||||
const { sender, uri, target } = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'sender',
|
||||
message: 'SMTP_SENDER',
|
||||
default: process.env.SMTP_SENDER,
|
||||
validate: isValidStr,
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'uri',
|
||||
message: 'SMTP_URI',
|
||||
default: process.env.SMTP_URI,
|
||||
validate: isValidStr,
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'target',
|
||||
message: 'Email address which wanna send',
|
||||
validate: isValidStr,
|
||||
},
|
||||
]);
|
||||
|
||||
const transporter = nodemailer.createTransport(
|
||||
parseConnectionUrl(uri)
|
||||
);
|
||||
|
||||
try {
|
||||
const res = await transporter.sendMail({
|
||||
from: sender,
|
||||
to: target,
|
||||
subject: `Test email send in ${new Date().toLocaleDateString()}`,
|
||||
text: `This is a test email send by tailchat-cli at ${new Date().toLocaleString()}`,
|
||||
});
|
||||
console.log('Send Result:', res);
|
||||
} catch (err) {
|
||||
console.log('Send Failed:', String(err));
|
||||
} finally {
|
||||
transporter.close();
|
||||
}
|
||||
}
|
||||
)
|
||||
.demandCommand(),
|
||||
handler() {},
|
||||
};
|
||||
|
||||
function isValidStr(input: any): boolean {
|
||||
return typeof input === 'string' && input.length > 0;
|
||||
}
|
||||
52
apps/cli/src/commands/usage.ts
Normal file
52
apps/cli/src/commands/usage.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { CommandModule } from 'yargs';
|
||||
import inquirer from 'inquirer';
|
||||
import find from 'find-process';
|
||||
import pidusage from 'pidusage';
|
||||
|
||||
export const usageCommand: CommandModule = {
|
||||
command: 'usage [pid]',
|
||||
describe: 'View Tailchat process usage',
|
||||
builder: (yargs) =>
|
||||
yargs.positional('pid', {
|
||||
demandOption: false,
|
||||
description: 'process id',
|
||||
type: 'number',
|
||||
}),
|
||||
async handler(args) {
|
||||
let pidList: number[] = [];
|
||||
|
||||
if (!args.pid) {
|
||||
const list = await find('name', 'tailchat');
|
||||
|
||||
const processList = list.filter((item) => item.pid !== process.pid);
|
||||
|
||||
const res = await inquirer.prompt([
|
||||
{
|
||||
type: 'checkbox',
|
||||
name: 'process',
|
||||
message: 'Select the process to view',
|
||||
choices: processList.map((item) => ({
|
||||
name: `(${item.pid})${item.cmd}`,
|
||||
value: item.pid,
|
||||
})),
|
||||
},
|
||||
]);
|
||||
|
||||
pidList = res.process;
|
||||
} else {
|
||||
if (Array.isArray(args.pid)) {
|
||||
pidList = args.pid;
|
||||
} else {
|
||||
pidList = [args.pid as number];
|
||||
}
|
||||
}
|
||||
|
||||
const stats = await pidusage(pidList);
|
||||
const res = Object.entries(stats).map(([pid, info]) => ({
|
||||
pid,
|
||||
cpu: info.cpu,
|
||||
memory: `${info.memory / 1024 / 1024} MB`,
|
||||
}));
|
||||
console.table(res);
|
||||
},
|
||||
};
|
||||
26
apps/cli/src/index.ts
Normal file
26
apps/cli/src/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import './update';
|
||||
import yargs from 'yargs';
|
||||
import { createCommand } from './commands/create';
|
||||
import { connectCommand } from './commands/connect';
|
||||
import { appCommand } from './commands/app';
|
||||
import { declarationCommand } from './commands/declaration';
|
||||
import { benchmarkCommand } from './commands/benchmark';
|
||||
import { dockerCommand } from './commands/docker';
|
||||
import { usageCommand } from './commands/usage';
|
||||
import { registryCommand } from './commands/registry';
|
||||
import { smtpCommand } from './commands/smtp';
|
||||
|
||||
yargs
|
||||
.demandCommand()
|
||||
.command(createCommand)
|
||||
.command(connectCommand)
|
||||
.command(appCommand)
|
||||
.command(benchmarkCommand)
|
||||
.command(declarationCommand)
|
||||
.command(dockerCommand)
|
||||
.command(registryCommand)
|
||||
.command(smtpCommand)
|
||||
.command(usageCommand)
|
||||
.alias('h', 'help')
|
||||
.scriptName('tailchat')
|
||||
.parse();
|
||||
7
apps/cli/src/update.ts
Normal file
7
apps/cli/src/update.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import updateNotifier from 'update-notifier';
|
||||
const packageJson = require('../package.json');
|
||||
|
||||
updateNotifier({
|
||||
pkg: packageJson,
|
||||
shouldNotifyInNpmScript: true,
|
||||
}).notify();
|
||||
15
apps/cli/src/utils.ts
Normal file
15
apps/cli/src/utils.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Determine whether it is a development environment
|
||||
*/
|
||||
export function isDev(): boolean {
|
||||
return process.env.NODE_ENV === 'development';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add github resource proxy to optimize chinese access speed
|
||||
*
|
||||
* @deprecated this website is down
|
||||
*/
|
||||
export function withGhProxy(url: string): string {
|
||||
return `https://ghproxy.com/${url}`;
|
||||
}
|
||||
9
apps/cli/templates/client-plugin/{{id}}/manifest.json
Normal file
9
apps/cli/templates/client-plugin/{{id}}/manifest.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"label": "{{name}}",
|
||||
"name": "{{id}}",
|
||||
"url": "/plugins/{{id}}/index.js",
|
||||
"version": "0.0.0",
|
||||
"author": "{{author}}",
|
||||
"description": "{{desc}}",
|
||||
"requireRestart": true
|
||||
}
|
||||
16
apps/cli/templates/client-plugin/{{id}}/package.json
Normal file
16
apps/cli/templates/client-plugin/{{id}}/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "@plugins/{{id}}",
|
||||
"main": "src/index.tsx",
|
||||
"version": "0.0.0",
|
||||
"description": "{{desc}}",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"sync:declaration": "tailchat declaration github"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/styled-components": "^5.1.26",
|
||||
"react": "18.2.0",
|
||||
"styled-components": "^5.3.6"
|
||||
}
|
||||
}
|
||||
4
apps/cli/templates/client-plugin/{{id}}/src/index.tsx
Normal file
4
apps/cli/templates/client-plugin/{{id}}/src/index.tsx
Normal file
@@ -0,0 +1,4 @@
|
||||
const PLUGIN_ID = '{{id}}';
|
||||
const PLUGIN_NAME = '{{name}}';
|
||||
|
||||
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);
|
||||
8
apps/cli/templates/client-plugin/{{id}}/src/translate.ts
Normal file
8
apps/cli/templates/client-plugin/{{id}}/src/translate.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { localTrans } from '@capital/common';
|
||||
|
||||
export const Translate = {
|
||||
name: localTrans({
|
||||
'zh-CN': '{{name}}',
|
||||
'en-US': '{{name}}',
|
||||
}),
|
||||
};
|
||||
7
apps/cli/templates/client-plugin/{{id}}/tsconfig.json
Normal file
7
apps/cli/templates/client-plugin/{{id}}/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react",
|
||||
"importsNotUsedAsValues": "error"
|
||||
}
|
||||
}
|
||||
2
apps/cli/templates/client-plugin/{{id}}/types/tailchat.d.ts
vendored
Normal file
2
apps/cli/templates/client-plugin/{{id}}/types/tailchat.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
declare module '@capital/common';
|
||||
declare module '@capital/component';
|
||||
132
apps/cli/templates/plopfile.js
Normal file
132
apps/cli/templates/plopfile.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const path = require('path');
|
||||
const _ = require('lodash')
|
||||
|
||||
function pickPluginName(text) {
|
||||
const [_1, _2, ...others] = text.split('.');
|
||||
return others.join('-');
|
||||
}
|
||||
function upperFirst(text) {
|
||||
return _.upperFirst(_.camelCase(text));
|
||||
}
|
||||
|
||||
module.exports = function (
|
||||
/** @type {import('plop').NodePlopAPI} */
|
||||
plop
|
||||
) {
|
||||
plop.setHelper('pickPluginName', pickPluginName);
|
||||
plop.setHelper('pickPluginNameUp', (text) => {
|
||||
return upperFirst(pickPluginName(text));
|
||||
});
|
||||
|
||||
const namePrompts = [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'name',
|
||||
require: true,
|
||||
message: 'Plugin Name',
|
||||
}
|
||||
]
|
||||
|
||||
const serverPrompts = [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'id',
|
||||
require: true,
|
||||
default: 'com.msgbyte.example',
|
||||
message: 'Plugin unique id, a unique string in reverse domain name format',
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'author',
|
||||
message: 'Plugin Author',
|
||||
default: 'anonymous',
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'desc',
|
||||
message: 'Plugin description',
|
||||
default: '',
|
||||
},
|
||||
];
|
||||
|
||||
// 服务端插件的前端模板代码
|
||||
plop.setGenerator('client-plugin', {
|
||||
description: 'Pure frontend plugin template',
|
||||
prompts: [
|
||||
...namePrompts,
|
||||
...serverPrompts,
|
||||
],
|
||||
actions: [
|
||||
{
|
||||
type: 'addMany',
|
||||
destination: path.resolve(process.cwd(), './plugins'),
|
||||
base: './client-plugin',
|
||||
templateFiles: [
|
||||
'./client-plugin/**/*',
|
||||
],
|
||||
skipIfExists: true,
|
||||
globOptions: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// 服务端插件的前端模板代码
|
||||
plop.setGenerator('server-plugin', {
|
||||
description: 'Pure backtend plugin template',
|
||||
prompts: serverPrompts,
|
||||
actions: [
|
||||
{
|
||||
type: 'addMany',
|
||||
destination: path.resolve(process.cwd(), './plugins'),
|
||||
base: './server-plugin',
|
||||
templateFiles: ['./server-plugin/**/*'],
|
||||
skipIfExists: true,
|
||||
globOptions: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// 服务端插件的前端模板代码
|
||||
plop.setGenerator('server-plugin-web', {
|
||||
description: 'web plugin in backtend plugin template',
|
||||
prompts: [
|
||||
...namePrompts,
|
||||
...serverPrompts,
|
||||
],
|
||||
actions: [
|
||||
{
|
||||
type: 'addMany',
|
||||
destination: path.resolve(process.cwd(), './plugins'),
|
||||
base: './server-plugin-web',
|
||||
templateFiles: [
|
||||
'./server-plugin-web/**/*',
|
||||
'./server-plugin-web/*/.ministarrc.js',
|
||||
],
|
||||
skipIfExists: true,
|
||||
globOptions: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// 服务端插件的前端模板代码
|
||||
plop.setGenerator('server-plugin-full', {
|
||||
description: 'Full backend plugin template',
|
||||
prompts: [
|
||||
...namePrompts,
|
||||
...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: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
};
|
||||
17
apps/cli/templates/server-plugin-full/{{id}}/.ministarrc.js
Normal file
17
apps/cli/templates/server-plugin-full/{{id}}/.ministarrc.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const path = require('path');
|
||||
|
||||
const pluginRoot = path.resolve(__dirname, './web');
|
||||
const outDir = path.resolve(__dirname, '../../public');
|
||||
|
||||
module.exports = {
|
||||
externalDeps: [
|
||||
'react',
|
||||
'react-router',
|
||||
'axios',
|
||||
'styled-components',
|
||||
'zustand',
|
||||
'zustand/middleware/immer'
|
||||
],
|
||||
pluginRoot,
|
||||
outDir,
|
||||
};
|
||||
@@ -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;
|
||||
20
apps/cli/templates/server-plugin-full/{{id}}/package.json
Normal file
20
apps/cli/templates/server-plugin-full/{{id}}/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "tailchat-plugin-{{pickPluginName id}}",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "{{author}}",
|
||||
"description": "{{desc}}",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build:web": "ministar buildPlugin all",
|
||||
"build:web:watch": "ministar watchPlugin all"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "18.0.20",
|
||||
"mini-star": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"tailchat-server-sdk": "*"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { TcService, TcDbService } from 'tailchat-server-sdk';
|
||||
import type { {{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model } from '../models/{{pickPluginName id}}';
|
||||
|
||||
/**
|
||||
* {{name}}
|
||||
*
|
||||
* {{desc}}
|
||||
*/
|
||||
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;
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"label": "{{name}}",
|
||||
"name": "{{id}}",
|
||||
"url": "{BACKEND}/plugins/{{id}}/index.js",
|
||||
"version": "0.0.0",
|
||||
"author": "{{author}}",
|
||||
"description": "{{desc}}",
|
||||
"requireRestart": true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "@plugins/{{id}}",
|
||||
"main": "src/index.tsx",
|
||||
"version": "0.0.0",
|
||||
"description": "{{desc}}",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"sync:declaration": "tailchat declaration github"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/styled-components": "^5.1.26",
|
||||
"react": "18.2.0",
|
||||
"styled-components": "^5.3.6"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
const PLUGIN_ID = '{{id}}';
|
||||
const PLUGIN_NAME = '{{name}}';
|
||||
|
||||
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react",
|
||||
"importsNotUsedAsValues": "error"
|
||||
}
|
||||
}
|
||||
2
apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/types/tailchat.d.ts
vendored
Normal file
2
apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/types/tailchat.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
declare module '@capital/common';
|
||||
declare module '@capital/component';
|
||||
17
apps/cli/templates/server-plugin-web/{{id}}/.ministarrc.js
Normal file
17
apps/cli/templates/server-plugin-web/{{id}}/.ministarrc.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const path = require('path');
|
||||
|
||||
const pluginRoot = path.resolve(__dirname, './web');
|
||||
const outDir = path.resolve(__dirname, '../../public');
|
||||
|
||||
module.exports = {
|
||||
externalDeps: [
|
||||
'react',
|
||||
'react-router',
|
||||
'axios',
|
||||
'styled-components',
|
||||
'zustand',
|
||||
'zustand/middleware/immer'
|
||||
],
|
||||
pluginRoot,
|
||||
outDir,
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"label": "{{name}}",
|
||||
"name": "{{id}}",
|
||||
"url": "{BACKEND}/plugins/{{id}}/index.js",
|
||||
"version": "0.0.0",
|
||||
"author": "{{author}}",
|
||||
"description": "{{desc}}",
|
||||
"requireRestart": true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "@plugins/{{id}}",
|
||||
"main": "src/index.tsx",
|
||||
"version": "0.0.0",
|
||||
"description": "{{desc}}",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"sync:declaration": "tailchat declaration github"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/styled-components": "^5.1.26",
|
||||
"react": "18.2.0",
|
||||
"styled-components": "^5.3.6"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
const PLUGIN_ID = '{{id}}';
|
||||
const PLUGIN_NAME = '{{name}}';
|
||||
|
||||
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);
|
||||
@@ -0,0 +1,8 @@
|
||||
import { localTrans } from '@capital/common';
|
||||
|
||||
export const Translate = {
|
||||
name: localTrans({
|
||||
'zh-CN': '{{name}}',
|
||||
'en-US': '{{name}}',
|
||||
}),
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react",
|
||||
"importsNotUsedAsValues": "error"
|
||||
}
|
||||
}
|
||||
2
apps/cli/templates/server-plugin-web/{{id}}/web/plugins/{{id}}/types/tailchat.d.ts
vendored
Normal file
2
apps/cli/templates/server-plugin-web/{{id}}/web/plugins/{{id}}/types/tailchat.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
declare module '@capital/common';
|
||||
declare module '@capital/component';
|
||||
@@ -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;
|
||||
14
apps/cli/templates/server-plugin/{{id}}/package.json
Normal file
14
apps/cli/templates/server-plugin/{{id}}/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "tailchat-plugin-{{pickPluginName id}}",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "{{author}}",
|
||||
"description": "{{desc}}",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {},
|
||||
"devDependencies": {},
|
||||
"dependencies": {
|
||||
"tailchat-server-sdk": "*"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { TcService, TcDbService } from 'tailchat-server-sdk';
|
||||
import type { {{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model } from '../models/{{pickPluginName id}}';
|
||||
|
||||
/**
|
||||
* {{desc}}
|
||||
*/
|
||||
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;
|
||||
16
apps/cli/tsconfig.json
Normal file
16
apps/cli/tsconfig.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES6",
|
||||
"module": "commonjs",
|
||||
"rootDir": "src",
|
||||
"outDir": "lib",
|
||||
"jsx": "react",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
"skipLibCheck": true,
|
||||
"noEmit": false
|
||||
},
|
||||
"exclude": ["./templates"]
|
||||
}
|
||||
Reference in New Issue
Block a user