@hlw-midway/plugin-cli 1.0.0 → 1.0.1

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.
@@ -0,0 +1,52 @@
1
+ /**
2
+ * 返回码
3
+ */
4
+ export declare enum RESCODE {
5
+ SUCCESS = 1000,
6
+ COMMFAIL = 1001,
7
+ VALIDATEFAIL = 1002,
8
+ COREFAIL = 1003
9
+ }
10
+ /**
11
+ * 返回信息
12
+ */
13
+ export declare enum RESMESSAGE {
14
+ SUCCESS = "success",
15
+ COMMFAIL = "comm fail",
16
+ VALIDATEFAIL = "validate fail",
17
+ COREFAIL = "core fail"
18
+ }
19
+ /**
20
+ * 错误提示
21
+ */
22
+ export declare enum ERRINFO {
23
+ NOENTITY = "\u672A\u8BBE\u7F6E\u64CD\u4F5C\u5B9E\u4F53",
24
+ NOID = "\u67E5\u8BE2\u53C2\u6570[id]\u4E0D\u5B58\u5728",
25
+ SORTFIELD = "\u6392\u5E8F\u53C2\u6570\u4E0D\u6B63\u786E"
26
+ }
27
+ /**
28
+ * 事件
29
+ */
30
+ export declare enum EVENT {
31
+ SOFT_DELETE = "onSoftDelete",
32
+ SERVER_READY = "onServerReady",
33
+ READY = "onReady",
34
+ ES_DATA_CHANGE = "esDataChange"
35
+ }
36
+ export declare class GlobalConfig {
37
+ private static instance;
38
+ RESCODE: {
39
+ SUCCESS: number;
40
+ COMMFAIL: number;
41
+ VALIDATEFAIL: number;
42
+ COREFAIL: number;
43
+ };
44
+ RESMESSAGE: {
45
+ SUCCESS: string;
46
+ COMMFAIL: string;
47
+ VALIDATEFAIL: string;
48
+ COREFAIL: string;
49
+ };
50
+ private constructor();
51
+ static getInstance(): GlobalConfig;
52
+ }
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GlobalConfig = exports.EVENT = exports.ERRINFO = exports.RESMESSAGE = exports.RESCODE = void 0;
4
+ /**
5
+ * 返回码
6
+ */
7
+ var RESCODE;
8
+ (function (RESCODE) {
9
+ // 成功
10
+ RESCODE[RESCODE["SUCCESS"] = 1000] = "SUCCESS";
11
+ // 失败
12
+ RESCODE[RESCODE["COMMFAIL"] = 1001] = "COMMFAIL";
13
+ // 参数验证失败
14
+ RESCODE[RESCODE["VALIDATEFAIL"] = 1002] = "VALIDATEFAIL";
15
+ // 参数验证失败
16
+ RESCODE[RESCODE["COREFAIL"] = 1003] = "COREFAIL";
17
+ })(RESCODE || (exports.RESCODE = RESCODE = {}));
18
+ /**
19
+ * 返回信息
20
+ */
21
+ var RESMESSAGE;
22
+ (function (RESMESSAGE) {
23
+ // 成功
24
+ RESMESSAGE["SUCCESS"] = "success";
25
+ // 失败
26
+ RESMESSAGE["COMMFAIL"] = "comm fail";
27
+ // 参数验证失败
28
+ RESMESSAGE["VALIDATEFAIL"] = "validate fail";
29
+ // 核心异常
30
+ RESMESSAGE["COREFAIL"] = "core fail";
31
+ })(RESMESSAGE || (exports.RESMESSAGE = RESMESSAGE = {}));
32
+ /**
33
+ * 错误提示
34
+ */
35
+ var ERRINFO;
36
+ (function (ERRINFO) {
37
+ ERRINFO["NOENTITY"] = "\u672A\u8BBE\u7F6E\u64CD\u4F5C\u5B9E\u4F53";
38
+ ERRINFO["NOID"] = "\u67E5\u8BE2\u53C2\u6570[id]\u4E0D\u5B58\u5728";
39
+ ERRINFO["SORTFIELD"] = "\u6392\u5E8F\u53C2\u6570\u4E0D\u6B63\u786E";
40
+ })(ERRINFO || (exports.ERRINFO = ERRINFO = {}));
41
+ /**
42
+ * 事件
43
+ */
44
+ var EVENT;
45
+ (function (EVENT) {
46
+ // 软删除
47
+ EVENT["SOFT_DELETE"] = "onSoftDelete";
48
+ // 服务成功启动
49
+ EVENT["SERVER_READY"] = "onServerReady";
50
+ // 服务就绪
51
+ EVENT["READY"] = "onReady";
52
+ // ES 数据改变
53
+ EVENT["ES_DATA_CHANGE"] = "esDataChange";
54
+ })(EVENT || (exports.EVENT = EVENT = {}));
55
+ class GlobalConfig {
56
+ // ... 其他的配置 ...
57
+ constructor() {
58
+ this.RESCODE = {
59
+ SUCCESS: 1000,
60
+ COMMFAIL: 1001,
61
+ VALIDATEFAIL: 1002,
62
+ COREFAIL: 1003,
63
+ };
64
+ this.RESMESSAGE = {
65
+ SUCCESS: "success",
66
+ COMMFAIL: "comm fail",
67
+ VALIDATEFAIL: "validate fail",
68
+ COREFAIL: "core fail",
69
+ };
70
+ }
71
+ static getInstance() {
72
+ if (!GlobalConfig.instance) {
73
+ GlobalConfig.instance = new GlobalConfig();
74
+ }
75
+ return GlobalConfig.instance;
76
+ }
77
+ }
78
+ exports.GlobalConfig = GlobalConfig;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 异常基类
3
+ */
4
+ export declare class BaseException extends Error {
5
+ status: number;
6
+ constructor(name: string, code: number, message: string);
7
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseException = void 0;
4
+ /**
5
+ * 异常基类
6
+ */
7
+ class BaseException extends Error {
8
+ constructor(name, code, message) {
9
+ super(message);
10
+ this.name = name;
11
+ this.status = code;
12
+ }
13
+ }
14
+ exports.BaseException = BaseException;
@@ -0,0 +1,7 @@
1
+ import { BaseException } from './base';
2
+ /**
3
+ * 通用异常
4
+ */
5
+ export declare class HlwCommException extends BaseException {
6
+ constructor(message: string);
7
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HlwCommException = void 0;
4
+ const global_1 = require("../constant/global");
5
+ const base_1 = require("./base");
6
+ /**
7
+ * 通用异常
8
+ */
9
+ class HlwCommException extends base_1.BaseException {
10
+ constructor(message) {
11
+ const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
12
+ super('HlwCommException', RESCODE.COMMFAIL, message ? message : RESMESSAGE.COMMFAIL);
13
+ }
14
+ }
15
+ exports.HlwCommException = HlwCommException;
@@ -0,0 +1,7 @@
1
+ import { BaseException } from './base';
2
+ /**
3
+ * 核心异常
4
+ */
5
+ export declare class HlwCoreException extends BaseException {
6
+ constructor(message: string);
7
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HlwCoreException = void 0;
4
+ const global_1 = require("../constant/global");
5
+ const base_1 = require("./base");
6
+ /**
7
+ * 核心异常
8
+ */
9
+ class HlwCoreException extends base_1.BaseException {
10
+ constructor(message) {
11
+ const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
12
+ super('HlwCoreException', RESCODE.COREFAIL, message ? message : RESMESSAGE.COREFAIL);
13
+ }
14
+ }
15
+ exports.HlwCoreException = HlwCoreException;
@@ -0,0 +1,7 @@
1
+ import { BaseException } from "./base";
2
+ /**
3
+ * 校验异常
4
+ */
5
+ export declare class HlwValidateException extends BaseException {
6
+ constructor(message: string);
7
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HlwValidateException = void 0;
4
+ const global_1 = require("../constant/global");
5
+ const base_1 = require("./base");
6
+ /**
7
+ * 校验异常
8
+ */
9
+ class HlwValidateException extends base_1.BaseException {
10
+ constructor(message) {
11
+ const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
12
+ super("HlwValidateException", RESCODE.VALIDATEFAIL, message ? message : RESMESSAGE.VALIDATEFAIL);
13
+ }
14
+ }
15
+ exports.HlwValidateException = HlwValidateException;
@@ -0,0 +1,51 @@
1
+ export declare enum MODETYPE {
2
+ LOCAL = "local",
3
+ CLOUD = "cloud",
4
+ OTHER = "other"
5
+ }
6
+ export declare enum CLOUDTYPE {
7
+ OSS = "oss",
8
+ COS = "cos",
9
+ QINIU = "qiniu",
10
+ /** AWS S3 */
11
+ AWS = "aws"
12
+ }
13
+ /**
14
+ * 上传模式
15
+ */
16
+ export interface Mode {
17
+ mode: MODETYPE;
18
+ type: string;
19
+ }
20
+ /**
21
+ * 文件上传
22
+ */
23
+ export interface BaseUpload {
24
+ /**
25
+ * 获得上传模式
26
+ */
27
+ getMode(): Promise<Mode>;
28
+ /**
29
+ * 获得原始操作对象
30
+ * @returns
31
+ */
32
+ getMetaFileObj(): any;
33
+ /**
34
+ * 下载并上传
35
+ * @param url
36
+ * @param fileName 文件名
37
+ */
38
+ downAndUpload(url: string, fileName?: string): Promise<string>;
39
+ /**
40
+ * 指定Key(路径)上传,本地文件上传到存储服务
41
+ * @param filePath 文件路径
42
+ * @param key 路径一致会覆盖源文件
43
+ */
44
+ uploadWithKey(filePath: any, key: any): Promise<string>;
45
+ /**
46
+ * 上传文件
47
+ * @param ctx
48
+ * @param key 文件路径
49
+ */
50
+ upload(ctx: any): Promise<any>;
51
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLOUDTYPE = exports.MODETYPE = void 0;
4
+ // 模式
5
+ var MODETYPE;
6
+ (function (MODETYPE) {
7
+ // 本地
8
+ MODETYPE["LOCAL"] = "local";
9
+ // 云存储
10
+ MODETYPE["CLOUD"] = "cloud";
11
+ // 其他
12
+ MODETYPE["OTHER"] = "other";
13
+ })(MODETYPE || (exports.MODETYPE = MODETYPE = {}));
14
+ var CLOUDTYPE;
15
+ (function (CLOUDTYPE) {
16
+ // 阿里云存储
17
+ CLOUDTYPE["OSS"] = "oss";
18
+ // 腾讯云存储
19
+ CLOUDTYPE["COS"] = "cos";
20
+ // 七牛云存储
21
+ CLOUDTYPE["QINIU"] = "qiniu";
22
+ /** AWS S3 */
23
+ CLOUDTYPE["AWS"] = "aws";
24
+ })(CLOUDTYPE || (exports.CLOUDTYPE = CLOUDTYPE = {}));
@@ -0,0 +1,104 @@
1
+ import { MidwayCache } from "@midwayjs/cache-manager";
2
+ import { IMidwayContext, IMidwayApplication } from "@midwayjs/core";
3
+ import { HlwCacheStore } from "@hlw-midway/core";
4
+ type FsCacheStore = ReturnType<typeof HlwCacheStore>;
5
+ export * from "./hook/upload";
6
+ export * from "./exception/base";
7
+ export * from "./exception/comm";
8
+ export * from "./exception/core";
9
+ export * from "./exception/validate";
10
+ export * from "./constant/global";
11
+ /**
12
+ * 插件信息
13
+ */
14
+ export interface PluginInfo {
15
+ /** 名称 */
16
+ name: string;
17
+ /** 唯一标识 */
18
+ key: string;
19
+ /** 钩子 */
20
+ hook: string;
21
+ /** 版本 */
22
+ version: string;
23
+ /** 描述 */
24
+ description: string;
25
+ /** 作者 */
26
+ author: string;
27
+ /** logo */
28
+ logo: string;
29
+ /** README 使用说明 */
30
+ readme: string;
31
+ /** 配置 */
32
+ config: any;
33
+ }
34
+ /**
35
+ * 插件基类,不建议修改
36
+ */
37
+ export declare abstract class BasePlugin {
38
+ /** 插件信息 */
39
+ pluginInfo: PluginInfo;
40
+ /** 请求上下文,用到此项无法本地调试,需安装到hlw-admin中才能调试 */
41
+ ctx: IMidwayContext;
42
+ /** 应用实例,用到此项无法本地调试,需安装到hlw-admin中才能调试 */
43
+ app: IMidwayApplication;
44
+ /** 缓存 */
45
+ cache: FsCacheStore | MidwayCache;
46
+ /** 插件 */
47
+ pluginService: PluginService;
48
+ /** 基础目录位置 */
49
+ baseDir: string;
50
+ setCtx(ctx: IMidwayContext): void;
51
+ setApp(app: IMidwayApplication): void;
52
+ constructor();
53
+ /**
54
+ * 获得App级别的实例
55
+ * @param name
56
+ * @returns
57
+ */
58
+ getAppInstance(name: string): Promise<unknown>;
59
+ /**
60
+ * 获得请求级别的实例
61
+ * @param name
62
+ * @returns
63
+ */
64
+ getCtxInstance(name: string): Promise<unknown>;
65
+ /**
66
+ * 初始化插件
67
+ * @param pluginInfo
68
+ * @param ctx
69
+ * @param app
70
+ */
71
+ init(pluginInfo: PluginInfo, ctx?: IMidwayContext, app?: IMidwayApplication, other?: any): Promise<void>;
72
+ /**
73
+ * 处理配置目录
74
+ */
75
+ private configDir;
76
+ /**
77
+ * 插件就绪
78
+ */
79
+ ready(): Promise<void>;
80
+ }
81
+ /**
82
+ * 插件服务
83
+ */
84
+ export declare class PluginService {
85
+ /**
86
+ * 调用插件
87
+ * @param key 插件key
88
+ * @param method 方法
89
+ * @param params 参数
90
+ * @returns
91
+ */
92
+ invoke(key: string, method: string, ...params: any[]): Promise<any>;
93
+ /**
94
+ * 获得插件实例
95
+ * @param key
96
+ * @returns
97
+ */
98
+ getInstance(key: string): Promise<any>;
99
+ /**
100
+ * 检查状态
101
+ * @param key
102
+ */
103
+ checkStatus(key: string): Promise<void>;
104
+ }
package/dist/index.js ADDED
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.BasePlugin = void 0;
18
+ const core_1 = require("@hlw-midway/core");
19
+ // 文件上传
20
+ __exportStar(require("./hook/upload"), exports);
21
+ // 异常处理
22
+ __exportStar(require("./exception/base"), exports);
23
+ __exportStar(require("./exception/comm"), exports);
24
+ __exportStar(require("./exception/core"), exports);
25
+ __exportStar(require("./exception/validate"), exports);
26
+ // 全局参数
27
+ __exportStar(require("./constant/global"), exports);
28
+ /**
29
+ * 插件基类,不建议修改
30
+ */
31
+ class BasePlugin {
32
+ setCtx(ctx) {
33
+ this.ctx = ctx;
34
+ }
35
+ setApp(app) {
36
+ this.app = app;
37
+ this.baseDir = app.getBaseDir();
38
+ this.configDir();
39
+ }
40
+ constructor() { }
41
+ /**
42
+ * 获得App级别的实例
43
+ * @param name
44
+ * @returns
45
+ */
46
+ async getAppInstance(name) {
47
+ return await this.app.getApplicationContext().getAsync(name);
48
+ }
49
+ /**
50
+ * 获得请求级别的实例
51
+ * @param name
52
+ * @returns
53
+ */
54
+ async getCtxInstance(name) {
55
+ return await this.ctx.requestContext.getAsync(name);
56
+ }
57
+ /**
58
+ * 初始化插件
59
+ * @param pluginInfo
60
+ * @param ctx
61
+ * @param app
62
+ */
63
+ async init(pluginInfo, ctx, app, other) {
64
+ var _a;
65
+ this.pluginInfo = pluginInfo;
66
+ this.ctx = ctx;
67
+ this.app = app;
68
+ this.baseDir = process.cwd();
69
+ if (this.app) {
70
+ this.baseDir = (_a = this.app) === null || _a === void 0 ? void 0 : _a.getBaseDir();
71
+ }
72
+ this.configDir();
73
+ this.cache = (0, core_1.HlwCacheStore)({});
74
+ if (other) {
75
+ this.cache = other.cache;
76
+ this.pluginService = other.pluginService;
77
+ }
78
+ await this.ready();
79
+ }
80
+ /**
81
+ * 处理配置目录
82
+ */
83
+ configDir() {
84
+ // 替换\为/
85
+ this.baseDir = this.baseDir.replace(/\\/g, "/");
86
+ let config = this.pluginInfo.config || {};
87
+ config = JSON.stringify(config);
88
+ this.pluginInfo.config = JSON.parse(config.replace(/@baseDir/g, this.baseDir));
89
+ }
90
+ /**
91
+ * 插件就绪
92
+ */
93
+ async ready() { }
94
+ }
95
+ exports.BasePlugin = BasePlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-midway/plugin-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "hlw-admin midway plugin",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {