@wooksjs/event-cli 0.1.0
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/README.md +42 -0
- package/dist/index.cjs +105 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.mjs +98 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @wooksjs/event-cli
|
|
2
|
+
|
|
3
|
+
**!!! This is work-in-progress library, breaking changes are expected !!!**
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="../../logo.png" width="128px"><br>
|
|
7
|
+
<a href="https://github.com/prostojs/wooks/blob/main/LICENSE">
|
|
8
|
+
<img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
|
|
9
|
+
</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
As a part of `wooks` event processing framework, `@wooksjs/event-cli` implements CLI events and provides composables that let you:
|
|
13
|
+
|
|
14
|
+
- access flags (- or --)
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
`npm install wooks @wooksjs/event-cli`
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { useRouteParams, Wooks } from 'wooks'
|
|
24
|
+
import { WooksCli, cliShortcuts, useFlags } from '@wooksjs/event-cli'
|
|
25
|
+
|
|
26
|
+
const app = new Wooks().shortcuts(cliShortcuts)
|
|
27
|
+
|
|
28
|
+
app.cli('test', () => {
|
|
29
|
+
console.log('flags:')
|
|
30
|
+
return useFlags()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
app.cli(':arg', () => {
|
|
34
|
+
console.log('run argument:', useRouteParams().params)
|
|
35
|
+
return 'done'
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
app.subscribe(new WooksCli())
|
|
39
|
+
|
|
40
|
+
// node ./index.js test
|
|
41
|
+
// node ./index.js random
|
|
42
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var eventCore = require('@wooksjs/event-core');
|
|
4
|
+
var minimist = require('minimist');
|
|
5
|
+
|
|
6
|
+
function createCliContext(data) {
|
|
7
|
+
return eventCore.createEventContext({
|
|
8
|
+
event: Object.assign(Object.assign({}, data), { type: 'CLI' }),
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function useCliContext() {
|
|
12
|
+
return eventCore.useEventContext('CLI');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/******************************************************************************
|
|
16
|
+
Copyright (c) Microsoft Corporation.
|
|
17
|
+
|
|
18
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
19
|
+
purpose with or without fee is hereby granted.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
22
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
23
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
24
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
25
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
26
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
27
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
28
|
+
***************************************************************************** */
|
|
29
|
+
|
|
30
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
31
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
32
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
33
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
34
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
35
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
36
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const banner = () => `[${"@wooksjs/event-cli"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
41
|
+
|
|
42
|
+
/* istanbul ignore file */
|
|
43
|
+
function logError(error) {
|
|
44
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const cliShortcuts = {
|
|
48
|
+
cli: 'CLI',
|
|
49
|
+
};
|
|
50
|
+
class WooksCli {
|
|
51
|
+
subscribe(lookup) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const argv = process.argv.slice(2);
|
|
54
|
+
const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
|
|
55
|
+
const routing = { method: 'CLI', url: '/' + (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv).map(v => encodeURIComponent(v)).join('/') };
|
|
56
|
+
const { restoreCtx, clearCtx } = createCliContext({ argv });
|
|
57
|
+
const handlers = lookup(routing);
|
|
58
|
+
if (handlers) {
|
|
59
|
+
try {
|
|
60
|
+
for (const handler of handlers) {
|
|
61
|
+
restoreCtx();
|
|
62
|
+
const response = yield handler();
|
|
63
|
+
if (typeof response === 'string') {
|
|
64
|
+
console.log(response);
|
|
65
|
+
}
|
|
66
|
+
else if (Array.isArray(response)) {
|
|
67
|
+
response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
console.log(JSON.stringify(response, null, ' '));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
logError(e.message);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
clearCtx();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
logError('Unknown command parameters');
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function useFlags() {
|
|
89
|
+
const { store } = useCliContext();
|
|
90
|
+
const flags = store('flags');
|
|
91
|
+
if (!flags.value) {
|
|
92
|
+
flags.value = minimist(store('event').value.argv);
|
|
93
|
+
}
|
|
94
|
+
return flags.value;
|
|
95
|
+
}
|
|
96
|
+
function useFlag(name) {
|
|
97
|
+
return useFlags()[name];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
exports.WooksCli = WooksCli;
|
|
101
|
+
exports.cliShortcuts = cliShortcuts;
|
|
102
|
+
exports.createCliContext = createCliContext;
|
|
103
|
+
exports.useCliContext = useCliContext;
|
|
104
|
+
exports.useFlag = useFlag;
|
|
105
|
+
exports.useFlags = useFlags;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { TGenericContextStore } from '@wooksjs/event-core';
|
|
2
|
+
import { TGenericEvent } from '@wooksjs/event-core';
|
|
3
|
+
import { TWooksLookupArgs } from 'wooks';
|
|
4
|
+
import { TWooksLookupHandlers } from 'wooks';
|
|
5
|
+
import { TWooksSubscribeAdapter } from 'wooks';
|
|
6
|
+
|
|
7
|
+
export declare const cliShortcuts: {
|
|
8
|
+
cli: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export declare function createCliContext(data: TCliEventData): {
|
|
12
|
+
getCtx: () => TCliContextStore;
|
|
13
|
+
restoreCtx: () => TCliContextStore;
|
|
14
|
+
clearCtx: () => null;
|
|
15
|
+
store: <K extends keyof TCliContextStore>(key: K) => {
|
|
16
|
+
value: TCliContextStore[K];
|
|
17
|
+
hook: <K2 extends keyof Required<TCliContextStore>[K]>(key2: K2) => {
|
|
18
|
+
value: Required<TCliContextStore>[K][K2];
|
|
19
|
+
isDefined: boolean;
|
|
20
|
+
};
|
|
21
|
+
init: <K2_1 extends keyof Required<TCliContextStore>[K]>(key2: K2_1, getter: () => Required<Required<TCliContextStore>[K]>[K2_1]) => Required<Required<TCliContextStore>[K]>[K2_1];
|
|
22
|
+
set: <K2_2 extends keyof Required<TCliContextStore>[K]>(key2: K2_2, v: Required<TCliContextStore[K]>[K2_2]) => Required<TCliContextStore[K]>[K2_2];
|
|
23
|
+
get: <K2_3 extends keyof Required<TCliContextStore>[K]>(key2: K2_3) => Required<TCliContextStore>[K][K2_3];
|
|
24
|
+
has: <K2_4 extends keyof Required<TCliContextStore>[K]>(key2: K2_4) => boolean;
|
|
25
|
+
del: <K2_5 extends keyof Required<TCliContextStore>[K]>(key2: K2_5) => void;
|
|
26
|
+
entries: () => [string, unknown][];
|
|
27
|
+
clear: () => void;
|
|
28
|
+
};
|
|
29
|
+
getStore: <K_1 extends keyof TCliContextStore>(key: K_1) => TCliContextStore[K_1];
|
|
30
|
+
setStore: <K_2 extends keyof TCliContextStore>(key: K_2, v: TCliContextStore[K_2]) => void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare interface TCliContextStore extends TGenericContextStore<TCliEvent> {
|
|
34
|
+
flags?: {
|
|
35
|
+
[name: string]: boolean | string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare interface TCliEvent extends TGenericEvent, TCliEventData {
|
|
40
|
+
type: 'CLI';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare interface TCliEventData {
|
|
44
|
+
argv: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export declare function useCliContext<T extends object>(): {
|
|
48
|
+
getCtx: () => TCliContextStore & T;
|
|
49
|
+
restoreCtx: () => TCliContextStore & T;
|
|
50
|
+
clearCtx: () => null;
|
|
51
|
+
store: <K extends keyof TCliContextStore | keyof T>(key: K) => {
|
|
52
|
+
value: (TCliContextStore & T)[K];
|
|
53
|
+
hook: <K2 extends keyof Required<TCliContextStore & T>[K]>(key2: K2) => {
|
|
54
|
+
value: Required<TCliContextStore & T>[K][K2];
|
|
55
|
+
isDefined: boolean;
|
|
56
|
+
};
|
|
57
|
+
init: <K2_1 extends keyof Required<TCliContextStore & T>[K]>(key2: K2_1, getter: () => Required<Required<TCliContextStore & T>[K]>[K2_1]) => Required<Required<TCliContextStore & T>[K]>[K2_1];
|
|
58
|
+
set: <K2_2 extends keyof Required<TCliContextStore & T>[K]>(key2: K2_2, v: Required<(TCliContextStore & T)[K]>[K2_2]) => Required<(TCliContextStore & T)[K]>[K2_2];
|
|
59
|
+
get: <K2_3 extends keyof Required<TCliContextStore & T>[K]>(key2: K2_3) => Required<TCliContextStore & T>[K][K2_3];
|
|
60
|
+
has: <K2_4 extends keyof Required<TCliContextStore & T>[K]>(key2: K2_4) => boolean;
|
|
61
|
+
del: <K2_5 extends keyof Required<TCliContextStore & T>[K]>(key2: K2_5) => void;
|
|
62
|
+
entries: () => [string, unknown][];
|
|
63
|
+
clear: () => void;
|
|
64
|
+
};
|
|
65
|
+
getStore: <K_1 extends keyof TCliContextStore | keyof T>(key: K_1) => (TCliContextStore & T)[K_1];
|
|
66
|
+
setStore: <K_2 extends keyof TCliContextStore | keyof T>(key: K_2, v: (TCliContextStore & T)[K_2]) => void;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export declare function useFlag(name: string): string | boolean;
|
|
70
|
+
|
|
71
|
+
export declare function useFlags(): {
|
|
72
|
+
[name: string]: string | boolean;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export declare class WooksCli implements TWooksSubscribeAdapter {
|
|
76
|
+
subscribe(lookup: (route: TWooksLookupArgs) => TWooksLookupHandlers | null): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createEventContext, useEventContext } from '@wooksjs/event-core';
|
|
2
|
+
import minimist from 'minimist';
|
|
3
|
+
|
|
4
|
+
function createCliContext(data) {
|
|
5
|
+
return createEventContext({
|
|
6
|
+
event: Object.assign(Object.assign({}, data), { type: 'CLI' }),
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
function useCliContext() {
|
|
10
|
+
return useEventContext('CLI');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/******************************************************************************
|
|
14
|
+
Copyright (c) Microsoft Corporation.
|
|
15
|
+
|
|
16
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
17
|
+
purpose with or without fee is hereby granted.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
20
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
21
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
22
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
23
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
24
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
25
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
26
|
+
***************************************************************************** */
|
|
27
|
+
|
|
28
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const banner = () => `[${"@wooksjs/event-cli"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
39
|
+
|
|
40
|
+
/* istanbul ignore file */
|
|
41
|
+
function logError(error) {
|
|
42
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const cliShortcuts = {
|
|
46
|
+
cli: 'CLI',
|
|
47
|
+
};
|
|
48
|
+
class WooksCli {
|
|
49
|
+
subscribe(lookup) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const argv = process.argv.slice(2);
|
|
52
|
+
const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
|
|
53
|
+
const routing = { method: 'CLI', url: '/' + (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv).map(v => encodeURIComponent(v)).join('/') };
|
|
54
|
+
const { restoreCtx, clearCtx } = createCliContext({ argv });
|
|
55
|
+
const handlers = lookup(routing);
|
|
56
|
+
if (handlers) {
|
|
57
|
+
try {
|
|
58
|
+
for (const handler of handlers) {
|
|
59
|
+
restoreCtx();
|
|
60
|
+
const response = yield handler();
|
|
61
|
+
if (typeof response === 'string') {
|
|
62
|
+
console.log(response);
|
|
63
|
+
}
|
|
64
|
+
else if (Array.isArray(response)) {
|
|
65
|
+
response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
console.log(JSON.stringify(response, null, ' '));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
logError(e.message);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
clearCtx();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
logError('Unknown command parameters');
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function useFlags() {
|
|
87
|
+
const { store } = useCliContext();
|
|
88
|
+
const flags = store('flags');
|
|
89
|
+
if (!flags.value) {
|
|
90
|
+
flags.value = minimist(store('event').value.argv);
|
|
91
|
+
}
|
|
92
|
+
return flags.value;
|
|
93
|
+
}
|
|
94
|
+
function useFlag(name) {
|
|
95
|
+
return useFlags()[name];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { WooksCli, cliShortcuts, createCliContext, useCliContext, useFlag, useFlags };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wooksjs/event-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@wooksjs/event-cli",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/wooksjs/wooksjs.git",
|
|
14
|
+
"directory": "packages/event-cli"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"http",
|
|
18
|
+
"wooks",
|
|
19
|
+
"composables",
|
|
20
|
+
"web",
|
|
21
|
+
"framework",
|
|
22
|
+
"app",
|
|
23
|
+
"api",
|
|
24
|
+
"rest",
|
|
25
|
+
"restful",
|
|
26
|
+
"prostojs"
|
|
27
|
+
],
|
|
28
|
+
"author": "Artem Maltsev",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-cli#readme",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"minimist": "^1.2.6",
|
|
36
|
+
"@wooksjs/event-core": "0.1.0",
|
|
37
|
+
"wooks": "0.1.0"
|
|
38
|
+
}
|
|
39
|
+
}
|