clerc 0.0.0 → 0.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.
- package/dist/index.cjs +118 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.mjs +106 -0
- package/package.json +6 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var liteEmit = require('lite-emit');
|
|
6
|
+
var minimist = require('minimist');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var minimist__default = /*#__PURE__*/_interopDefaultLegacy(minimist);
|
|
11
|
+
|
|
12
|
+
function resolveFlagAlias(command) {
|
|
13
|
+
return Object.entries((command == null ? void 0 : command.flags) || {}).reduce((acc, [name, { alias }]) => {
|
|
14
|
+
if (alias) {
|
|
15
|
+
acc[name] = alias;
|
|
16
|
+
}
|
|
17
|
+
return acc;
|
|
18
|
+
}, {});
|
|
19
|
+
}
|
|
20
|
+
function resolveCommand(commands, name) {
|
|
21
|
+
const possibleCommands = Object.values(commands).filter((c) => {
|
|
22
|
+
var _a;
|
|
23
|
+
return c.name === name || ((_a = c.alias) == null ? void 0 : _a.includes(name));
|
|
24
|
+
});
|
|
25
|
+
if (possibleCommands.length > 1) {
|
|
26
|
+
throw new Error(`Multiple commands found with name "${name}"`);
|
|
27
|
+
}
|
|
28
|
+
return possibleCommands[0];
|
|
29
|
+
}
|
|
30
|
+
function compose(invokers) {
|
|
31
|
+
return function fn(ctx) {
|
|
32
|
+
return dispatch(0);
|
|
33
|
+
function dispatch(i) {
|
|
34
|
+
const invoker = invokers[i];
|
|
35
|
+
return invoker(ctx, dispatch.bind(null, i + 1));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Clerc {
|
|
41
|
+
constructor() {
|
|
42
|
+
this._name = "";
|
|
43
|
+
this._description = "";
|
|
44
|
+
this._version = "";
|
|
45
|
+
this._invokers = [];
|
|
46
|
+
this._commands = {};
|
|
47
|
+
this.__command_emitter = new liteEmit.LiteEmit();
|
|
48
|
+
}
|
|
49
|
+
static create() {
|
|
50
|
+
return new Clerc();
|
|
51
|
+
}
|
|
52
|
+
name(name) {
|
|
53
|
+
this._name = name;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
description(description) {
|
|
57
|
+
this._description = description;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
version(version) {
|
|
61
|
+
this._version = version;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
command(name, description, options = {}) {
|
|
65
|
+
const { alias = [], flags = {} } = options;
|
|
66
|
+
this._commands[name] = { name, description, alias, flags };
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
on(name, cb) {
|
|
70
|
+
this.__command_emitter.on(name, cb);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
use(plugin) {
|
|
74
|
+
return plugin.setup(this);
|
|
75
|
+
}
|
|
76
|
+
registerInvoker(invoker) {
|
|
77
|
+
this._invokers.push(invoker);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
parse() {
|
|
81
|
+
const argv = process.argv.slice(2);
|
|
82
|
+
let parsed = minimist__default["default"](argv);
|
|
83
|
+
const name = parsed._[0];
|
|
84
|
+
const command = resolveCommand(this._commands, name || "_");
|
|
85
|
+
if (!command) {
|
|
86
|
+
throw new Error(`No such command: ${name}`);
|
|
87
|
+
}
|
|
88
|
+
const commandName = command.name;
|
|
89
|
+
parsed = minimist__default["default"](argv, {
|
|
90
|
+
alias: resolveFlagAlias(this._commands[commandName])
|
|
91
|
+
});
|
|
92
|
+
if (!command) {
|
|
93
|
+
throw new Error(`Command "${name}" not found`);
|
|
94
|
+
}
|
|
95
|
+
const invokerContext = {
|
|
96
|
+
name,
|
|
97
|
+
flags: parsed,
|
|
98
|
+
cli: this
|
|
99
|
+
};
|
|
100
|
+
const handlerContext = invokerContext;
|
|
101
|
+
const emitHandler = () => {
|
|
102
|
+
this.__command_emitter.emit(commandName, handlerContext);
|
|
103
|
+
};
|
|
104
|
+
const invokers = [...this._invokers, emitHandler];
|
|
105
|
+
const invoker = compose(invokers);
|
|
106
|
+
invoker(invokerContext);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function definePlugin(p) {
|
|
111
|
+
return p;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
exports.Clerc = Clerc;
|
|
115
|
+
exports.compose = compose;
|
|
116
|
+
exports.definePlugin = definePlugin;
|
|
117
|
+
exports.resolveCommand = resolveCommand;
|
|
118
|
+
exports.resolveFlagAlias = resolveFlagAlias;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { LiteEmit } from 'lite-emit';
|
|
2
|
+
|
|
3
|
+
interface Plugin<U, T extends Clerc = Clerc> {
|
|
4
|
+
setup: (cli: T) => U;
|
|
5
|
+
}
|
|
6
|
+
declare function definePlugin<T extends Clerc, U extends Clerc>(p: Plugin<T, U>): Plugin<T, U>;
|
|
7
|
+
|
|
8
|
+
declare type Dict<T> = Record<string, T>;
|
|
9
|
+
declare type MustArray<T> = T extends any[] ? T : [T];
|
|
10
|
+
declare type MaybeArray<T> = T | T[];
|
|
11
|
+
declare type GetLength<T extends any[]> = T extends {
|
|
12
|
+
length: infer L extends number;
|
|
13
|
+
} ? L : never;
|
|
14
|
+
declare type GetTail<T extends any[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
|
|
15
|
+
declare type EnhanceSingle<T, E extends Dict<any>> = T & E;
|
|
16
|
+
declare type Enhance<T, E extends Dict<any> | Dict<any>[]> = GetLength<MustArray<E>> extends 0 ? T : Enhance<EnhanceSingle<T, MustArray<E>[0]>, GetTail<MustArray<E>>>;
|
|
17
|
+
interface FlagOptions {
|
|
18
|
+
alias?: MaybeArray<string>;
|
|
19
|
+
description: string;
|
|
20
|
+
}
|
|
21
|
+
interface Flag extends FlagOptions {
|
|
22
|
+
name: string;
|
|
23
|
+
}
|
|
24
|
+
interface CommandOptions {
|
|
25
|
+
alias?: MaybeArray<string>;
|
|
26
|
+
flags?: Dict<FlagOptions>;
|
|
27
|
+
}
|
|
28
|
+
interface Command<N extends string = string, D extends string = string> extends CommandOptions {
|
|
29
|
+
name: N;
|
|
30
|
+
description: D;
|
|
31
|
+
}
|
|
32
|
+
declare type CommandRecord = Dict<Command>;
|
|
33
|
+
declare type MakeEventMap<T extends CommandRecord> = {
|
|
34
|
+
[K in keyof T]: [InvokerContext];
|
|
35
|
+
};
|
|
36
|
+
declare type PossibleFlagKind = string | number | boolean | Dict<any>;
|
|
37
|
+
interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
|
|
38
|
+
name: N;
|
|
39
|
+
flags: Dict<PossibleFlagKind | PossibleFlagKind[]>;
|
|
40
|
+
cli: Clerc<C>;
|
|
41
|
+
}
|
|
42
|
+
declare type Handler = (ctx: HandlerContext) => void;
|
|
43
|
+
interface InvokerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> extends HandlerContext<C, N> {
|
|
44
|
+
}
|
|
45
|
+
declare type Invoker = (ctx: InvokerContext<any>, next: Invoker) => void;
|
|
46
|
+
|
|
47
|
+
declare class Clerc<C extends CommandRecord = {}> {
|
|
48
|
+
_name: string;
|
|
49
|
+
_description: string;
|
|
50
|
+
_version: string;
|
|
51
|
+
_invokers: Invoker[];
|
|
52
|
+
_commands: C;
|
|
53
|
+
__command_emitter: LiteEmit<MakeEventMap<C>>;
|
|
54
|
+
private constructor();
|
|
55
|
+
static create(): Clerc<{}>;
|
|
56
|
+
name(name: string): this;
|
|
57
|
+
description(description: string): this;
|
|
58
|
+
version(version: string): this;
|
|
59
|
+
command<N extends string, D extends string>(name: N, description: D, options?: CommandOptions): this & Clerc<C & Record<N, Command<N, D>>>;
|
|
60
|
+
on<K extends keyof C>(name: K, cb: Handler): this;
|
|
61
|
+
use<T extends Clerc, U>(plugin: Plugin<U, T>): U;
|
|
62
|
+
registerInvoker(invoker: Invoker): this;
|
|
63
|
+
parse(): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
declare function resolveFlagAlias(command: Command): Dict<MaybeArray<string>>;
|
|
67
|
+
declare function resolveCommand(commands: CommandRecord, name: string): Command<string, string>;
|
|
68
|
+
declare function compose(invokers: Invoker[]): (ctx: HandlerContext) => void;
|
|
69
|
+
|
|
70
|
+
export { Clerc, Command, CommandOptions, CommandRecord, Dict, Enhance, Flag, FlagOptions, Handler, HandlerContext, Invoker, InvokerContext, MakeEventMap, MaybeArray, Plugin, PossibleFlagKind, compose, definePlugin, resolveCommand, resolveFlagAlias };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { LiteEmit } from 'lite-emit';
|
|
2
|
+
import minimist from 'minimist';
|
|
3
|
+
|
|
4
|
+
function resolveFlagAlias(command) {
|
|
5
|
+
return Object.entries((command == null ? void 0 : command.flags) || {}).reduce((acc, [name, { alias }]) => {
|
|
6
|
+
if (alias) {
|
|
7
|
+
acc[name] = alias;
|
|
8
|
+
}
|
|
9
|
+
return acc;
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
12
|
+
function resolveCommand(commands, name) {
|
|
13
|
+
const possibleCommands = Object.values(commands).filter((c) => {
|
|
14
|
+
var _a;
|
|
15
|
+
return c.name === name || ((_a = c.alias) == null ? void 0 : _a.includes(name));
|
|
16
|
+
});
|
|
17
|
+
if (possibleCommands.length > 1) {
|
|
18
|
+
throw new Error(`Multiple commands found with name "${name}"`);
|
|
19
|
+
}
|
|
20
|
+
return possibleCommands[0];
|
|
21
|
+
}
|
|
22
|
+
function compose(invokers) {
|
|
23
|
+
return function fn(ctx) {
|
|
24
|
+
return dispatch(0);
|
|
25
|
+
function dispatch(i) {
|
|
26
|
+
const invoker = invokers[i];
|
|
27
|
+
return invoker(ctx, dispatch.bind(null, i + 1));
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class Clerc {
|
|
33
|
+
constructor() {
|
|
34
|
+
this._name = "";
|
|
35
|
+
this._description = "";
|
|
36
|
+
this._version = "";
|
|
37
|
+
this._invokers = [];
|
|
38
|
+
this._commands = {};
|
|
39
|
+
this.__command_emitter = new LiteEmit();
|
|
40
|
+
}
|
|
41
|
+
static create() {
|
|
42
|
+
return new Clerc();
|
|
43
|
+
}
|
|
44
|
+
name(name) {
|
|
45
|
+
this._name = name;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
description(description) {
|
|
49
|
+
this._description = description;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
version(version) {
|
|
53
|
+
this._version = version;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
command(name, description, options = {}) {
|
|
57
|
+
const { alias = [], flags = {} } = options;
|
|
58
|
+
this._commands[name] = { name, description, alias, flags };
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
on(name, cb) {
|
|
62
|
+
this.__command_emitter.on(name, cb);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
use(plugin) {
|
|
66
|
+
return plugin.setup(this);
|
|
67
|
+
}
|
|
68
|
+
registerInvoker(invoker) {
|
|
69
|
+
this._invokers.push(invoker);
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
parse() {
|
|
73
|
+
const argv = process.argv.slice(2);
|
|
74
|
+
let parsed = minimist(argv);
|
|
75
|
+
const name = parsed._[0];
|
|
76
|
+
const command = resolveCommand(this._commands, name || "_");
|
|
77
|
+
if (!command) {
|
|
78
|
+
throw new Error(`No such command: ${name}`);
|
|
79
|
+
}
|
|
80
|
+
const commandName = command.name;
|
|
81
|
+
parsed = minimist(argv, {
|
|
82
|
+
alias: resolveFlagAlias(this._commands[commandName])
|
|
83
|
+
});
|
|
84
|
+
if (!command) {
|
|
85
|
+
throw new Error(`Command "${name}" not found`);
|
|
86
|
+
}
|
|
87
|
+
const invokerContext = {
|
|
88
|
+
name,
|
|
89
|
+
flags: parsed,
|
|
90
|
+
cli: this
|
|
91
|
+
};
|
|
92
|
+
const handlerContext = invokerContext;
|
|
93
|
+
const emitHandler = () => {
|
|
94
|
+
this.__command_emitter.emit(commandName, handlerContext);
|
|
95
|
+
};
|
|
96
|
+
const invokers = [...this._invokers, emitHandler];
|
|
97
|
+
const invoker = compose(invokers);
|
|
98
|
+
invoker(invokerContext);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function definePlugin(p) {
|
|
103
|
+
return p;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { Clerc, compose, definePlugin, resolveCommand, resolveFlagAlias };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clerc",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc",
|
|
6
6
|
"keywords": [],
|
|
@@ -14,9 +14,6 @@
|
|
|
14
14
|
"url": "https://github.com/so1ve/clerc/issues"
|
|
15
15
|
},
|
|
16
16
|
"license": "MIT",
|
|
17
|
-
"publishConfig": {
|
|
18
|
-
"access": "public"
|
|
19
|
-
},
|
|
20
17
|
"sideEffects": false,
|
|
21
18
|
"exports": {
|
|
22
19
|
".": {
|
|
@@ -31,16 +28,18 @@
|
|
|
31
28
|
"files": [
|
|
32
29
|
"dist"
|
|
33
30
|
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"lite-emit": "^1.4.0",
|
|
36
|
-
"minimist": "^1.2.
|
|
37
|
-
"type-gym": "^0.5.0"
|
|
36
|
+
"minimist": "^1.2.7"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
40
39
|
"@types/minimist": "^1.2.2"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
42
|
"build": "puild",
|
|
44
|
-
"
|
|
43
|
+
"watch": "puild --watch"
|
|
45
44
|
}
|
|
46
45
|
}
|