@initx-plugin/core 0.0.23 → 0.0.25

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,36 @@
1
+ type MaybeArray<T> = T | T[];
2
+ type MaybePromise<T> = T | Promise<T>;
3
+
4
+ interface MatcherSetup {
5
+ /**
6
+ * Matching string or RegExp
7
+ *
8
+ * The key that was used to match the handler
9
+ */
10
+ matching: MaybeArray<string | RegExp>;
11
+ }
12
+ type BaseMatchers<TMatcher> = TMatcher & MatcherSetup;
13
+ type TypeMatchers<TMatcher> = Record<string, BaseMatchers<TMatcher>>;
14
+ type ResultFunction<TResult, TMatcher> = (matcher: TMatcher, ...others: string[]) => TResult;
15
+ type Matchers<TMatcher extends object = object> = MaybeArray<BaseMatchers<TMatcher>> | TypeMatchers<TMatcher>;
16
+ declare class InitxMatcher$1<TResult, TMatcher extends object> {
17
+ private resultFunction;
18
+ constructor(fn: ResultFunction<TResult, TMatcher>);
19
+ match(matchers: Matchers<TMatcher>, key: string, ...others: string[]): TResult[];
20
+ private matchBaseMatchers;
21
+ private matchArrayBaseMatchers;
22
+ private matchTypeMatchers;
23
+ private isBaseMatchers;
24
+ private isArrayBaseMatchers;
25
+ private alwaysArray;
26
+ private isObject;
27
+ private isPassed;
28
+ private omit;
29
+ private buildResultMatcher;
30
+ private buildResultFunction;
31
+ }
32
+ declare function useInitxMatcher<TResult, TMatcher extends object = object>(fn: ResultFunction<TResult, TMatcher>): InitxMatcher$1<TResult, TMatcher>;
33
+
1
34
  interface PackageInfo {
2
35
  root: string;
3
36
  name: string;
@@ -27,20 +60,15 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
60
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
61
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
29
62
 
30
- type MaybeArray<T> = T | T[];
31
- type MaybePromise<T> = T | Promise<T>;
32
- interface BaseMatchers {
33
- matching: MaybeArray<string | RegExp>;
63
+ type InitxMatcher<TMatcher extends object = object> = TMatcher & {
34
64
  /**
35
65
  * Description of the handler
36
66
  *
37
67
  * If multiple handlers are matched, this description will be displayed
38
68
  */
39
69
  description: string;
40
- }
41
- type TypeMatchers = Record<string, BaseMatchers>;
42
- type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
43
- type PluginStore = Record<string, any>;
70
+ };
71
+ type InitxMatchers<TMatcher extends object = object> = Matchers<InitxMatcher<TMatcher>>;
44
72
  interface HandlerInfo {
45
73
  handler: () => MaybePromise<void>;
46
74
  description: string;
@@ -72,30 +100,29 @@ interface InitxRunContext extends InitxBaseContext {
72
100
  */
73
101
  packageInfo: PackageInfo;
74
102
  }
75
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
103
+ interface InitxContext<TStore extends object = object, TMatcher extends object = object> extends InitxRunContext {
76
104
  /**
77
105
  * Store
78
106
  *
79
107
  * Store data in memory, and write to disk when the program exits
80
108
  */
81
109
  store: TStore;
110
+ /**
111
+ * Matcher
112
+ *
113
+ * Matched matcher object, you can get custom fields, excluded `matching`
114
+ */
115
+ matcher: InitxMatcher<TMatcher>;
82
116
  }
83
- declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
84
- abstract matchers: Matchers;
85
- abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
117
+ declare abstract class InitxPlugin<TStore extends object = object> {
118
+ abstract matchers: InitxMatchers;
119
+ abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
86
120
  defaultStore?: TStore;
87
121
  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
122
  private executeHandle;
96
123
  }
97
124
 
98
- declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
99
- declare function writeStore({ name }: PackageInfo): void;
125
+ declare function createStore(name: string, defaultStore?: Record<string, any>): any;
126
+ declare function writeStore(name: string): void;
100
127
 
101
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
128
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, type InitxMatchers, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type Matchers, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,36 @@
1
+ type MaybeArray<T> = T | T[];
2
+ type MaybePromise<T> = T | Promise<T>;
3
+
4
+ interface MatcherSetup {
5
+ /**
6
+ * Matching string or RegExp
7
+ *
8
+ * The key that was used to match the handler
9
+ */
10
+ matching: MaybeArray<string | RegExp>;
11
+ }
12
+ type BaseMatchers<TMatcher> = TMatcher & MatcherSetup;
13
+ type TypeMatchers<TMatcher> = Record<string, BaseMatchers<TMatcher>>;
14
+ type ResultFunction<TResult, TMatcher> = (matcher: TMatcher, ...others: string[]) => TResult;
15
+ type Matchers<TMatcher extends object = object> = MaybeArray<BaseMatchers<TMatcher>> | TypeMatchers<TMatcher>;
16
+ declare class InitxMatcher$1<TResult, TMatcher extends object> {
17
+ private resultFunction;
18
+ constructor(fn: ResultFunction<TResult, TMatcher>);
19
+ match(matchers: Matchers<TMatcher>, key: string, ...others: string[]): TResult[];
20
+ private matchBaseMatchers;
21
+ private matchArrayBaseMatchers;
22
+ private matchTypeMatchers;
23
+ private isBaseMatchers;
24
+ private isArrayBaseMatchers;
25
+ private alwaysArray;
26
+ private isObject;
27
+ private isPassed;
28
+ private omit;
29
+ private buildResultMatcher;
30
+ private buildResultFunction;
31
+ }
32
+ declare function useInitxMatcher<TResult, TMatcher extends object = object>(fn: ResultFunction<TResult, TMatcher>): InitxMatcher$1<TResult, TMatcher>;
33
+
1
34
  interface PackageInfo {
2
35
  root: string;
3
36
  name: string;
@@ -27,20 +60,15 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
60
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
61
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
29
62
 
30
- type MaybeArray<T> = T | T[];
31
- type MaybePromise<T> = T | Promise<T>;
32
- interface BaseMatchers {
33
- matching: MaybeArray<string | RegExp>;
63
+ type InitxMatcher<TMatcher extends object = object> = TMatcher & {
34
64
  /**
35
65
  * Description of the handler
36
66
  *
37
67
  * If multiple handlers are matched, this description will be displayed
38
68
  */
39
69
  description: string;
40
- }
41
- type TypeMatchers = Record<string, BaseMatchers>;
42
- type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
43
- type PluginStore = Record<string, any>;
70
+ };
71
+ type InitxMatchers<TMatcher extends object = object> = Matchers<InitxMatcher<TMatcher>>;
44
72
  interface HandlerInfo {
45
73
  handler: () => MaybePromise<void>;
46
74
  description: string;
@@ -72,30 +100,29 @@ interface InitxRunContext extends InitxBaseContext {
72
100
  */
73
101
  packageInfo: PackageInfo;
74
102
  }
75
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
103
+ interface InitxContext<TStore extends object = object, TMatcher extends object = object> extends InitxRunContext {
76
104
  /**
77
105
  * Store
78
106
  *
79
107
  * Store data in memory, and write to disk when the program exits
80
108
  */
81
109
  store: TStore;
110
+ /**
111
+ * Matcher
112
+ *
113
+ * Matched matcher object, you can get custom fields, excluded `matching`
114
+ */
115
+ matcher: InitxMatcher<TMatcher>;
82
116
  }
83
- declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
84
- abstract matchers: Matchers;
85
- abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
117
+ declare abstract class InitxPlugin<TStore extends object = object> {
118
+ abstract matchers: InitxMatchers;
119
+ abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
86
120
  defaultStore?: TStore;
87
121
  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
122
  private executeHandle;
96
123
  }
97
124
 
98
- declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
99
- declare function writeStore({ name }: PackageInfo): void;
125
+ declare function createStore(name: string, defaultStore?: Record<string, any>): any;
126
+ declare function writeStore(name: string): void;
100
127
 
101
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
128
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, type InitxMatchers, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type Matchers, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
package/dist/index.mjs CHANGED
@@ -1,14 +1,120 @@
1
- import path from 'node:path';
2
1
  import { homedir } from 'node:os';
3
- import fs from 'fs-extra';
2
+ import path from 'node:path';
4
3
  import { defu } from 'defu';
4
+ import fs from 'fs-extra';
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"];
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.25",
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.25"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "^11.0.4"