@initx-plugin/core 0.0.23 → 0.0.24

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.d.mts CHANGED
@@ -1,3 +1,47 @@
1
+ type MaybeArray<T> = T | T[];
2
+ type MaybePromise<T> = T | Promise<T>;
3
+
4
+ interface MatcherCommon {
5
+ /**
6
+ * Description of the handler
7
+ *
8
+ * If multiple handlers are matched, this description will be displayed
9
+ */
10
+ description: string;
11
+ }
12
+ interface MatcherSetup {
13
+ /**
14
+ * Matching string or RegExp
15
+ *
16
+ * The key that was used to match the handler
17
+ */
18
+ matching: MaybeArray<string | RegExp>;
19
+ }
20
+ type Matcher<TMatcher> = TMatcher & MatcherCommon;
21
+ type ResultFunction<TResult, TMatcher> = (matcher: Matcher<TMatcher>, ...others: string[]) => TResult & TMatcher;
22
+ type BaseMatchers<TMatcher> = Matcher<TMatcher> & MatcherSetup;
23
+ type TypeMatchers<TMatcher> = Record<string, BaseMatchers<TMatcher>>;
24
+ type MatcherOthersDefault = Record<any, any>;
25
+ type MatcherOthers<T extends MatcherOthersDefault = MatcherOthersDefault> = T;
26
+ type Matchers<TMatcher extends MatcherOthers = MatcherOthers> = MaybeArray<BaseMatchers<TMatcher>> | TypeMatchers<TMatcher>;
27
+ declare class InitxMatcher<TResult, TMatcher extends Matcher<MatcherOthers>> {
28
+ private resultFunction;
29
+ constructor(fn: ResultFunction<TResult, TMatcher>);
30
+ match(matchers: Matchers, key: string, ...others: string[]): (TResult & TMatcher)[];
31
+ private matchBaseMatchers;
32
+ private matchArrayBaseMatchers;
33
+ private matchTypeMatchers;
34
+ private isBaseMatchers;
35
+ private isArrayBaseMatchers;
36
+ private alwaysArray;
37
+ private isObject;
38
+ private isPassed;
39
+ private omit;
40
+ private buildResultMatcher;
41
+ private buildResultFunction;
42
+ }
43
+ declare function useInitxMatcher<TResult, TMatcher extends Matcher<MatcherOthers> = Matcher<MatcherOthers>>(fn: ResultFunction<TResult, TMatcher>): InitxMatcher<TResult, TMatcher>;
44
+
1
45
  interface PackageInfo {
2
46
  root: string;
3
47
  name: string;
@@ -27,19 +71,6 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
71
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
72
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
29
73
 
30
- type MaybeArray<T> = T | T[];
31
- type MaybePromise<T> = T | Promise<T>;
32
- interface BaseMatchers {
33
- matching: MaybeArray<string | RegExp>;
34
- /**
35
- * Description of the handler
36
- *
37
- * If multiple handlers are matched, this description will be displayed
38
- */
39
- description: string;
40
- }
41
- type TypeMatchers = Record<string, BaseMatchers>;
42
- type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
43
74
  type PluginStore = Record<string, any>;
44
75
  interface HandlerInfo {
45
76
  handler: () => MaybePromise<void>;
@@ -72,30 +103,29 @@ interface InitxRunContext extends InitxBaseContext {
72
103
  */
73
104
  packageInfo: PackageInfo;
74
105
  }
75
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
106
+ interface InitxContext<TStore extends PluginStore = PluginStore, TMatcher extends MatcherOthers<MatcherOthersDefault> = MatcherOthers<MatcherOthersDefault>> extends InitxRunContext {
76
107
  /**
77
108
  * Store
78
109
  *
79
110
  * Store data in memory, and write to disk when the program exits
80
111
  */
81
112
  store: TStore;
113
+ /**
114
+ * Matcher
115
+ *
116
+ * Matched matcher object, you can get custom fields, excluded `matching`
117
+ */
118
+ matcher: Matcher<TMatcher>;
82
119
  }
83
- declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
120
+ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore, TMatcher extends MatcherOthers = MatcherOthers> {
84
121
  abstract matchers: Matchers;
85
- abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
122
+ abstract handle(context: InitxContext<TStore, TMatcher>, ...others: string[]): MaybePromise<void>;
86
123
  defaultStore?: TStore;
87
124
  run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
88
- private matchBaseMatchers;
89
- private matchArrayBaseMatchers;
90
- private matchTypeMatchers;
91
- private isBaseMatchers;
92
- private isArrayBaseMatchers;
93
- private isObject;
94
- private isPassed;
95
125
  private executeHandle;
96
126
  }
97
127
 
98
- declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
99
- declare function writeStore({ name }: PackageInfo): void;
128
+ declare function createStore(name: string, defaultStore?: Record<string, any>): any;
129
+ declare function writeStore(name: string): void;
100
130
 
101
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
131
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type Matcher, type MatcherOthers, type MatcherOthersDefault, type Matchers, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,47 @@
1
+ type MaybeArray<T> = T | T[];
2
+ type MaybePromise<T> = T | Promise<T>;
3
+
4
+ interface MatcherCommon {
5
+ /**
6
+ * Description of the handler
7
+ *
8
+ * If multiple handlers are matched, this description will be displayed
9
+ */
10
+ description: string;
11
+ }
12
+ interface MatcherSetup {
13
+ /**
14
+ * Matching string or RegExp
15
+ *
16
+ * The key that was used to match the handler
17
+ */
18
+ matching: MaybeArray<string | RegExp>;
19
+ }
20
+ type Matcher<TMatcher> = TMatcher & MatcherCommon;
21
+ type ResultFunction<TResult, TMatcher> = (matcher: Matcher<TMatcher>, ...others: string[]) => TResult & TMatcher;
22
+ type BaseMatchers<TMatcher> = Matcher<TMatcher> & MatcherSetup;
23
+ type TypeMatchers<TMatcher> = Record<string, BaseMatchers<TMatcher>>;
24
+ type MatcherOthersDefault = Record<any, any>;
25
+ type MatcherOthers<T extends MatcherOthersDefault = MatcherOthersDefault> = T;
26
+ type Matchers<TMatcher extends MatcherOthers = MatcherOthers> = MaybeArray<BaseMatchers<TMatcher>> | TypeMatchers<TMatcher>;
27
+ declare class InitxMatcher<TResult, TMatcher extends Matcher<MatcherOthers>> {
28
+ private resultFunction;
29
+ constructor(fn: ResultFunction<TResult, TMatcher>);
30
+ match(matchers: Matchers, key: string, ...others: string[]): (TResult & TMatcher)[];
31
+ private matchBaseMatchers;
32
+ private matchArrayBaseMatchers;
33
+ private matchTypeMatchers;
34
+ private isBaseMatchers;
35
+ private isArrayBaseMatchers;
36
+ private alwaysArray;
37
+ private isObject;
38
+ private isPassed;
39
+ private omit;
40
+ private buildResultMatcher;
41
+ private buildResultFunction;
42
+ }
43
+ declare function useInitxMatcher<TResult, TMatcher extends Matcher<MatcherOthers> = Matcher<MatcherOthers>>(fn: ResultFunction<TResult, TMatcher>): InitxMatcher<TResult, TMatcher>;
44
+
1
45
  interface PackageInfo {
2
46
  root: string;
3
47
  name: string;
@@ -27,19 +71,6 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
71
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
72
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
29
73
 
30
- type MaybeArray<T> = T | T[];
31
- type MaybePromise<T> = T | Promise<T>;
32
- interface BaseMatchers {
33
- matching: MaybeArray<string | RegExp>;
34
- /**
35
- * Description of the handler
36
- *
37
- * If multiple handlers are matched, this description will be displayed
38
- */
39
- description: string;
40
- }
41
- type TypeMatchers = Record<string, BaseMatchers>;
42
- type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
43
74
  type PluginStore = Record<string, any>;
44
75
  interface HandlerInfo {
45
76
  handler: () => MaybePromise<void>;
@@ -72,30 +103,29 @@ interface InitxRunContext extends InitxBaseContext {
72
103
  */
73
104
  packageInfo: PackageInfo;
74
105
  }
75
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
106
+ interface InitxContext<TStore extends PluginStore = PluginStore, TMatcher extends MatcherOthers<MatcherOthersDefault> = MatcherOthers<MatcherOthersDefault>> extends InitxRunContext {
76
107
  /**
77
108
  * Store
78
109
  *
79
110
  * Store data in memory, and write to disk when the program exits
80
111
  */
81
112
  store: TStore;
113
+ /**
114
+ * Matcher
115
+ *
116
+ * Matched matcher object, you can get custom fields, excluded `matching`
117
+ */
118
+ matcher: Matcher<TMatcher>;
82
119
  }
83
- declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
120
+ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore, TMatcher extends MatcherOthers = MatcherOthers> {
84
121
  abstract matchers: Matchers;
85
- abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
122
+ abstract handle(context: InitxContext<TStore, TMatcher>, ...others: string[]): MaybePromise<void>;
86
123
  defaultStore?: TStore;
87
124
  run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
88
- private matchBaseMatchers;
89
- private matchArrayBaseMatchers;
90
- private matchTypeMatchers;
91
- private isBaseMatchers;
92
- private isArrayBaseMatchers;
93
- private isObject;
94
- private isPassed;
95
125
  private executeHandle;
96
126
  }
97
127
 
98
- declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
99
- declare function writeStore({ name }: PackageInfo): void;
128
+ declare function createStore(name: string, defaultStore?: Record<string, any>): any;
129
+ declare function writeStore(name: string): void;
100
130
 
101
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
131
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type Matcher, type MatcherOthers, type MatcherOthersDefault, type Matchers, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
package/dist/index.mjs CHANGED
@@ -4,11 +4,117 @@ import fs from 'fs-extra';
4
4
  import { defu } from 'defu';
5
5
  import { c } from '@initx-plugin/utils';
6
6
 
7
+ var __defProp$1 = Object.defineProperty;
8
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __publicField$1 = (obj, key, value) => {
10
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
11
+ return value;
12
+ };
13
+ class InitxMatcher {
14
+ constructor(fn) {
15
+ __publicField$1(this, "resultFunction");
16
+ this.resultFunction = fn;
17
+ }
18
+ match(matchers, key, ...others) {
19
+ if (this.isBaseMatchers(matchers)) {
20
+ return this.matchBaseMatchers(matchers, key, ...others);
21
+ }
22
+ if (this.isArrayBaseMatchers(matchers)) {
23
+ return this.matchArrayBaseMatchers(matchers, key, ...others);
24
+ }
25
+ if (this.isObject(matchers)) {
26
+ return this.matchTypeMatchers(matchers, key, ...others);
27
+ }
28
+ return [];
29
+ }
30
+ // BaseMatchers
31
+ matchBaseMatchers(matchers, key, ...others) {
32
+ if (!this.isPassed(matchers.matching, key)) {
33
+ return [];
34
+ }
35
+ return this.alwaysArray(
36
+ this.buildResultFunction(matchers, ...others)
37
+ );
38
+ }
39
+ matchArrayBaseMatchers(matchers, key, ...others) {
40
+ const handlers = [];
41
+ for (let i = 0; i < matchers.length; i++) {
42
+ const matcher = matchers[i];
43
+ const isPassed = this.isPassed(matcher.matching, key);
44
+ if (isPassed) {
45
+ handlers.push(
46
+ this.buildResultFunction(matcher, ...others)
47
+ );
48
+ }
49
+ }
50
+ return handlers;
51
+ }
52
+ matchTypeMatchers(matchers, key, ...others) {
53
+ const handlers = [];
54
+ const keys = Object.keys(matchers);
55
+ for (let i = 0; i < keys.length; i++) {
56
+ const matcher = matchers[keys[i]];
57
+ const isPassed = this.isPassed(matcher.matching, key);
58
+ if (isPassed) {
59
+ handlers.push(
60
+ this.buildResultFunction(matcher, keys[i], ...others)
61
+ );
62
+ }
63
+ }
64
+ return handlers;
65
+ }
66
+ isBaseMatchers(matchers) {
67
+ const keys = Object.keys(matchers);
68
+ const requiredKeys = ["matching", "description"];
69
+ return this.isObject(matchers) && keys.length >= 2 && requiredKeys.every((key) => keys.includes(key));
70
+ }
71
+ isArrayBaseMatchers(matchers) {
72
+ return Array.isArray(matchers) && matchers.every(this.isBaseMatchers.bind(this));
73
+ }
74
+ alwaysArray(value) {
75
+ return Array.isArray(value) ? value : [value];
76
+ }
77
+ isObject(value) {
78
+ return typeof value === "object" && value !== null && !Array.isArray(value);
79
+ }
80
+ isPassed(matchers, key) {
81
+ const tests = Array.isArray(matchers) ? matchers : [matchers];
82
+ return tests.some((test) => {
83
+ if (typeof test === "string") {
84
+ return test === key;
85
+ }
86
+ return test.test(key);
87
+ });
88
+ }
89
+ omit(obj, keys) {
90
+ const result = {};
91
+ for (const key in obj) {
92
+ if (!keys.includes(key)) {
93
+ result[key] = obj[key];
94
+ }
95
+ }
96
+ return result;
97
+ }
98
+ buildResultMatcher(matcher) {
99
+ return this.omit(matcher, ["matching"]);
100
+ }
101
+ buildResultFunction(matcher, ...others) {
102
+ const buildedMatcher = this.buildResultMatcher(matcher);
103
+ return this.resultFunction(
104
+ buildedMatcher,
105
+ ...others
106
+ );
107
+ }
108
+ }
109
+ function useInitxMatcher(fn) {
110
+ return new InitxMatcher(fn);
111
+ }
112
+
7
113
  let rewritedCache = null;
8
114
  const INITX_DIR = path.resolve(homedir(), ".initx");
9
115
  const STORE_FILE_NAME = "store.json";
10
116
  const resolveStore = (name) => path.resolve(INITX_DIR, name, STORE_FILE_NAME);
11
- function createStore({ name }, defaultStore = {}) {
117
+ function createStore(name, defaultStore = {}) {
12
118
  fs.ensureDirSync(path.resolve(INITX_DIR, name));
13
119
  const storePath = resolveStore(name);
14
120
  const generateResult = (resultData) => {
@@ -27,7 +133,7 @@ function createStore({ name }, defaultStore = {}) {
27
133
  }
28
134
  return generateResult(json);
29
135
  }
30
- function writeStore({ name }) {
136
+ function writeStore(name) {
31
137
  if (!rewritedCache) {
32
138
  return;
33
139
  }
@@ -72,81 +178,23 @@ class InitxPlugin {
72
178
  __publicField(this, "defaultStore");
73
179
  }
74
180
  run(context, ...others) {
75
- if (this.isBaseMatchers(this.matchers)) {
76
- return this.matchBaseMatchers(this.matchers, context, ...others);
77
- }
78
- if (this.isArrayBaseMatchers(this.matchers)) {
79
- return this.matchArrayBaseMatchers(this.matchers, context, ...others);
80
- }
81
- if (this.isObject(this.matchers)) {
82
- return this.matchTypeMatchers(this.matchers, context, ...others);
83
- }
84
- return [];
85
- }
86
- // BaseMatchers
87
- matchBaseMatchers(matchers, context, ...others) {
88
- if (!this.isPassed(matchers.matching, context.key)) {
89
- return [];
90
- }
91
- return [
92
- {
93
- handler: () => this.executeHandle(context, ...others),
94
- description: matchers.description
95
- }
96
- ];
97
- }
98
- matchArrayBaseMatchers(matchers, context, ...others) {
99
- const handlers = [];
100
- for (let i = 0; i < matchers.length; i++) {
101
- const matcher = matchers[i];
102
- const isPassed = this.isPassed(matcher.matching, context.key);
103
- if (isPassed) {
104
- handlers.push({
105
- handler: () => this.executeHandle(context, ...others),
106
- description: matcher.description
107
- });
108
- }
109
- }
110
- return handlers;
111
- }
112
- matchTypeMatchers(matchers, context, ...others) {
113
- const handlers = [];
114
- const keys = Object.keys(matchers);
115
- for (let i = 0; i < keys.length; i++) {
116
- const matcher = matchers[keys[i]];
117
- const isPassed = this.isPassed(matcher.matching, context.key);
118
- if (isPassed) {
119
- handlers.push({
120
- handler: () => this.executeHandle(context, keys[i], ...others),
121
- description: matcher.description
122
- });
123
- }
124
- }
125
- return handlers;
126
- }
127
- isBaseMatchers(matchers) {
128
- const keys = Object.keys(matchers);
129
- return this.isObject(matchers) && keys.length === 2 && keys.every((key) => key === "matching" || key === "description");
130
- }
131
- isArrayBaseMatchers(matchers) {
132
- return Array.isArray(matchers) && matchers.every(this.isBaseMatchers.bind(this));
133
- }
134
- isObject(value) {
135
- return typeof value === "object" && value !== null && !Array.isArray(value);
136
- }
137
- isPassed(matchers, key) {
138
- const tests = Array.isArray(matchers) ? matchers : [matchers];
139
- return tests.some((test) => {
140
- if (typeof test === "string") {
141
- return test === key;
142
- }
143
- return test.test(key);
144
- });
181
+ const initxMatcher = useInitxMatcher(
182
+ (matcher, ...others2) => ({
183
+ handler: () => this.executeHandle(context, matcher, ...others2),
184
+ description: matcher.description
185
+ })
186
+ );
187
+ const matchedHandlers = initxMatcher.match(
188
+ this.matchers,
189
+ context.key,
190
+ ...others
191
+ );
192
+ return matchedHandlers;
145
193
  }
146
- async executeHandle(context, ...others) {
147
- const store = createStore(context.packageInfo, this.defaultStore);
148
- await this.handle({ ...context, store }, ...others);
149
- writeStore(context.packageInfo);
194
+ async executeHandle(context, matcher, ...others) {
195
+ const store = createStore(context.packageInfo.name, this.defaultStore);
196
+ await this.handle({ ...context, matcher, store }, ...others);
197
+ writeStore(context.packageInfo.name);
150
198
  }
151
199
  }
152
200
 
@@ -209,4 +257,4 @@ function matchPlugins(plugins, { key, cliOptions }, ...others) {
209
257
  return matchedHandlers;
210
258
  }
211
259
 
212
- export { InitxPlugin, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
260
+ export { InitxPlugin, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@initx-plugin/core",
3
3
  "type": "module",
4
- "version": "0.0.23",
4
+ "version": "0.0.24",
5
5
  "description": "core module for initx plugins",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/initx-collective/initx#readme",
@@ -25,7 +25,7 @@
25
25
  "defu": "^6.1.4",
26
26
  "fs-extra": "^11.2.0",
27
27
  "importx": "^0.5.0",
28
- "@initx-plugin/utils": "0.0.23"
28
+ "@initx-plugin/utils": "0.0.24"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "^11.0.4"