@midwayjs/commander 4.0.0-beta.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,3 @@
1
+ export declare class CommanderConfiguration {
2
+ }
3
+ //# sourceMappingURL=configuration.d.ts.map
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.CommanderConfiguration = void 0;
10
+ const core_1 = require("@midwayjs/core");
11
+ let CommanderConfiguration = class CommanderConfiguration {
12
+ };
13
+ exports.CommanderConfiguration = CommanderConfiguration;
14
+ exports.CommanderConfiguration = CommanderConfiguration = __decorate([
15
+ (0, core_1.Configuration)({
16
+ namespace: 'commander',
17
+ importConfigs: [
18
+ {
19
+ default: {
20
+ midwayLogger: {
21
+ clients: {
22
+ commanderLogger: {
23
+ fileLogName: 'midway-commander.log',
24
+ },
25
+ },
26
+ },
27
+ },
28
+ },
29
+ ],
30
+ })
31
+ ], CommanderConfiguration);
32
+ //# sourceMappingURL=configuration.js.map
@@ -0,0 +1,19 @@
1
+ export declare const CLI_COMMAND_KEY = "commander:command";
2
+ export declare const CLI_OPTION_KEY = "commander:option";
3
+ export interface CliCommandOptions {
4
+ name: string;
5
+ arguments?: string;
6
+ description?: string;
7
+ argsDescription?: Record<string, string>;
8
+ aliases?: string[];
9
+ }
10
+ export interface CliOptionOptions {
11
+ flags: string;
12
+ description?: string;
13
+ defaultValue?: any;
14
+ required?: boolean;
15
+ name?: string;
16
+ }
17
+ export declare function Command(options: CliCommandOptions): ClassDecorator;
18
+ export declare function Option(options: CliOptionOptions): MethodDecorator;
19
+ //# sourceMappingURL=decorator.d.ts.map
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLI_OPTION_KEY = exports.CLI_COMMAND_KEY = void 0;
4
+ exports.Command = Command;
5
+ exports.Option = Option;
6
+ const core_1 = require("@midwayjs/core");
7
+ exports.CLI_COMMAND_KEY = 'commander:command';
8
+ exports.CLI_OPTION_KEY = 'commander:option';
9
+ function Command(options) {
10
+ return (target) => {
11
+ core_1.DecoratorManager.saveModule(exports.CLI_COMMAND_KEY, target);
12
+ core_1.MetadataManager.defineMetadata(exports.CLI_COMMAND_KEY, options, target);
13
+ (0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
14
+ };
15
+ }
16
+ function Option(options) {
17
+ return (target, propertyKey, _descriptor) => {
18
+ void _descriptor;
19
+ core_1.MetadataManager.attachMetadata(exports.CLI_OPTION_KEY, {
20
+ propertyKey,
21
+ options,
22
+ }, target.constructor);
23
+ };
24
+ }
25
+ //# sourceMappingURL=decorator.js.map
@@ -0,0 +1,25 @@
1
+ import { BaseFramework, IMidwayBootstrapOptions } from '@midwayjs/core';
2
+ import { ICommanderConfigurationOptions, IMidwayCommanderApplication, IMidwayContext } from './interface';
3
+ import { Command } from 'commander';
4
+ export declare class MidwayCommanderFramework extends BaseFramework<IMidwayCommanderApplication, IMidwayContext, ICommanderConfigurationOptions> {
5
+ app: IMidwayCommanderApplication;
6
+ protected frameworkLoggerName: string;
7
+ private program;
8
+ private isCommandsLoaded;
9
+ configure(_options: ICommanderConfigurationOptions): ICommanderConfigurationOptions;
10
+ applicationInitialize(_options: IMidwayBootstrapOptions): Promise<void>;
11
+ run(): Promise<void>;
12
+ /**
13
+ * 适用于测试或编程式触发命令的场景。
14
+ *
15
+ * 与 run() 不同,这里不会读取 process.argv,而是解析传入的 args,
16
+ * 从而避免测试环境(Jest/Node)对命令行参数的污染。
17
+ */
18
+ runCommand(...args: string[]): Promise<Command>;
19
+ private outputResult;
20
+ private outputChunk;
21
+ private isReadableLike;
22
+ private isAsyncIterableLike;
23
+ private loadCommands;
24
+ }
25
+ //# sourceMappingURL=framework.d.ts.map
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.MidwayCommanderFramework = void 0;
10
+ const core_1 = require("@midwayjs/core");
11
+ const decorator_1 = require("./decorator");
12
+ const commander_1 = require("commander");
13
+ const stream_1 = require("stream");
14
+ let MidwayCommanderFramework = class MidwayCommanderFramework extends core_1.BaseFramework {
15
+ frameworkLoggerName = 'commanderLogger';
16
+ program;
17
+ isCommandsLoaded = false;
18
+ configure(_options) {
19
+ void _options;
20
+ return this.configService.getConfiguration('commander');
21
+ }
22
+ async applicationInitialize(_options) {
23
+ void _options;
24
+ this.app = {};
25
+ this.program = new commander_1.Command();
26
+ this.program.exitOverride();
27
+ }
28
+ async run() {
29
+ this.loadCommands();
30
+ try {
31
+ await this.program.parseAsync(process.argv);
32
+ }
33
+ catch (error) {
34
+ void error;
35
+ }
36
+ }
37
+ /**
38
+ * 适用于测试或编程式触发命令的场景。
39
+ *
40
+ * 与 run() 不同,这里不会读取 process.argv,而是解析传入的 args,
41
+ * 从而避免测试环境(Jest/Node)对命令行参数的污染。
42
+ */
43
+ async runCommand(...args) {
44
+ this.loadCommands();
45
+ return this.program.parseAsync(args, { from: 'user' });
46
+ }
47
+ async outputResult(result) {
48
+ if (result == null) {
49
+ return;
50
+ }
51
+ if (Buffer.isBuffer(result)) {
52
+ process.stdout.write(result);
53
+ return;
54
+ }
55
+ if (typeof result === 'string') {
56
+ process.stdout.write(result);
57
+ return;
58
+ }
59
+ if (this.isReadableLike(result)) {
60
+ await new Promise((resolve, reject) => {
61
+ result.on('error', reject);
62
+ result.on('end', resolve);
63
+ result.pipe(process.stdout, { end: false });
64
+ });
65
+ return;
66
+ }
67
+ if (this.isAsyncIterableLike(result)) {
68
+ for await (const chunk of result) {
69
+ this.outputChunk(chunk);
70
+ }
71
+ return;
72
+ }
73
+ this.outputChunk(result);
74
+ }
75
+ outputChunk(chunk) {
76
+ if (chunk == null) {
77
+ return;
78
+ }
79
+ if (Buffer.isBuffer(chunk) || typeof chunk === 'string') {
80
+ process.stdout.write(chunk);
81
+ return;
82
+ }
83
+ process.stdout.write(JSON.stringify(chunk));
84
+ }
85
+ isReadableLike(value) {
86
+ return (value instanceof stream_1.Readable ||
87
+ (!!value &&
88
+ typeof value.pipe === 'function' &&
89
+ typeof value.on === 'function'));
90
+ }
91
+ isAsyncIterableLike(value) {
92
+ return (!!value && typeof value[Symbol.asyncIterator] === 'function');
93
+ }
94
+ loadCommands() {
95
+ if (this.isCommandsLoaded) {
96
+ return;
97
+ }
98
+ const modules = core_1.DecoratorManager.listModule(decorator_1.CLI_COMMAND_KEY);
99
+ for (const module of modules) {
100
+ const metadata = core_1.MetadataManager.getMetadata(decorator_1.CLI_COMMAND_KEY, module);
101
+ const cmd = this.program.command(metadata.name);
102
+ if (metadata.arguments) {
103
+ cmd.arguments(metadata.arguments);
104
+ }
105
+ if (metadata.description) {
106
+ cmd.description(metadata.description, metadata.argsDescription);
107
+ }
108
+ if (metadata.aliases) {
109
+ metadata.aliases.forEach(alias => cmd.alias(alias));
110
+ }
111
+ const optionMetadataList = core_1.MetadataManager.getMetadata(decorator_1.CLI_OPTION_KEY, module) || [];
112
+ for (const optMeta of optionMetadataList) {
113
+ const opt = optMeta.options;
114
+ let parser;
115
+ if (optMeta.propertyKey) {
116
+ parser = module.prototype[optMeta.propertyKey];
117
+ }
118
+ if (opt.required) {
119
+ if (parser) {
120
+ cmd.requiredOption(opt.flags, opt.description, parser, opt.defaultValue);
121
+ }
122
+ else {
123
+ cmd.requiredOption(opt.flags, opt.description, opt.defaultValue);
124
+ }
125
+ }
126
+ else {
127
+ if (parser) {
128
+ cmd.option(opt.flags, opt.description, parser, opt.defaultValue);
129
+ }
130
+ else {
131
+ cmd.option(opt.flags, opt.description, opt.defaultValue);
132
+ }
133
+ }
134
+ }
135
+ cmd.action(async (...args) => {
136
+ const commandInstance = (await this.applicationContext.getAsync(module));
137
+ const commandObj = args[args.length - 1];
138
+ const actualArgs = args.slice(0, -2);
139
+ const actualOptions = commandObj.opts();
140
+ if (commandInstance.run) {
141
+ const result = await commandInstance.run(actualArgs, actualOptions);
142
+ await this.outputResult(result);
143
+ }
144
+ });
145
+ }
146
+ this.isCommandsLoaded = true;
147
+ }
148
+ };
149
+ exports.MidwayCommanderFramework = MidwayCommanderFramework;
150
+ exports.MidwayCommanderFramework = MidwayCommanderFramework = __decorate([
151
+ (0, core_1.Framework)()
152
+ ], MidwayCommanderFramework);
153
+ //# sourceMappingURL=framework.js.map
@@ -0,0 +1,6 @@
1
+ export * from './interface';
2
+ export * from './decorator';
3
+ export * from './response';
4
+ export { MidwayCommanderFramework as Framework } from './framework';
5
+ export { CommanderConfiguration as Configuration } from './configuration';
6
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
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.Configuration = exports.Framework = void 0;
18
+ __exportStar(require("./interface"), exports);
19
+ __exportStar(require("./decorator"), exports);
20
+ __exportStar(require("./response"), exports);
21
+ var framework_1 = require("./framework");
22
+ Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.MidwayCommanderFramework; } });
23
+ var configuration_1 = require("./configuration");
24
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.CommanderConfiguration; } });
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,11 @@
1
+ import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
2
+ export { IMidwayContext } from '@midwayjs/core';
3
+ export interface ICommanderConfigurationOptions extends IConfigurationOptions {
4
+ }
5
+ export type IMidwayCommanderApplication = IMidwayApplication<IMidwayContext>;
6
+ export type Application = IMidwayCommanderApplication;
7
+ export type Context = IMidwayContext;
8
+ export interface CommandRunner {
9
+ run(passedParams: string[], options?: Record<string, any>): unknown | Promise<unknown>;
10
+ }
11
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1,15 @@
1
+ import { IMidwayContext, ServerResponse, ServerStreamOptions } from '@midwayjs/core';
2
+ import { Transform } from 'stream';
3
+ export declare class CliStreamResponse<CTX extends IMidwayContext = IMidwayContext> extends Transform {
4
+ private readonly ctx;
5
+ private readonly options;
6
+ constructor(ctx: CTX, options?: ServerStreamOptions<CTX>);
7
+ _transform(chunk: any, encoding: any, callback: any): void;
8
+ send(data: unknown): void;
9
+ sendError(error: Error): void;
10
+ }
11
+ export declare class CliServerResponse<CTX extends IMidwayContext = IMidwayContext> extends ServerResponse<CTX> {
12
+ static STREAM_TPL: <CTX_1 extends IMidwayContext>(data: unknown, _ctx: CTX_1) => unknown;
13
+ stream(options?: ServerStreamOptions<CTX>): CliStreamResponse<any>;
14
+ }
15
+ //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CliServerResponse = exports.CliStreamResponse = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ const stream_1 = require("stream");
6
+ class CliStreamResponse extends stream_1.Transform {
7
+ ctx;
8
+ options;
9
+ constructor(ctx, options = {}) {
10
+ super({
11
+ objectMode: true,
12
+ ...options,
13
+ });
14
+ this.ctx = ctx;
15
+ this.options = options;
16
+ }
17
+ _transform(chunk, encoding, callback) {
18
+ try {
19
+ if (Buffer.isBuffer(chunk) || typeof chunk === 'string') {
20
+ this.push(chunk);
21
+ }
22
+ else {
23
+ this.push(JSON.stringify(chunk));
24
+ }
25
+ callback();
26
+ }
27
+ catch (err) {
28
+ this.ctx?.logger?.error?.(err);
29
+ this.end();
30
+ callback(err);
31
+ }
32
+ }
33
+ send(data) {
34
+ if (!this.writable) {
35
+ return;
36
+ }
37
+ const tpl = this.options.tpl ?? ((v) => v);
38
+ this.write(tpl(data, this.ctx));
39
+ }
40
+ sendError(error) {
41
+ this.ctx?.logger?.error?.(error);
42
+ this.end();
43
+ }
44
+ }
45
+ exports.CliStreamResponse = CliStreamResponse;
46
+ class CliServerResponse extends core_1.ServerResponse {
47
+ static STREAM_TPL = (data, _ctx) => {
48
+ void _ctx;
49
+ return data;
50
+ };
51
+ stream(options = {}) {
52
+ return new CliStreamResponse(this.ctx, {
53
+ tpl: Object.getPrototypeOf(this).constructor.STREAM_TPL,
54
+ ...options,
55
+ });
56
+ }
57
+ }
58
+ exports.CliServerResponse = CliServerResponse;
59
+ //# sourceMappingURL=response.js.map
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { CommanderConfigOptions } from './dist/index';
2
+
3
+ export * from './dist/index';
4
+
5
+ declare module '@midwayjs/core/dist/interface' {
6
+ interface MidwayConfig {
7
+ commander?: Partial<CommanderConfigOptions>;
8
+ }
9
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@midwayjs/commander",
3
+ "version": "4.0.0-beta.1",
4
+ "description": "Midway Commander Framework",
5
+ "main": "dist/index.js",
6
+ "typings": "index.d.ts",
7
+ "keywords": [
8
+ "midway",
9
+ "component",
10
+ "commander"
11
+ ],
12
+ "author": "czy88840616 <czy88840616@gmail.com>",
13
+ "files": [
14
+ "dist/**/*.js",
15
+ "dist/**/*.d.ts",
16
+ "index.d.ts"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
21
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
22
+ "ci": "npm run test",
23
+ "lint": "mwts check"
24
+ },
25
+ "dependencies": {
26
+ "@midwayjs/core": "workspace:^",
27
+ "commander": "14.0.2"
28
+ },
29
+ "devDependencies": {
30
+ "@midwayjs/mock": "workspace:^"
31
+ },
32
+ "engines": {
33
+ "node": ">=20"
34
+ },
35
+ "license": "MIT"
36
+ }