@initx-plugin/core 0.0.26 → 0.0.28

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,35 +1,6 @@
1
- type MaybeArray<T> = T | T[];
2
- type MaybePromise<T> = T | Promise<T>;
1
+ import { MatcherRules } from 'matchinitx';
3
2
 
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>;
3
+ type MaybePromise<T> = T | Promise<T>;
33
4
 
34
5
  interface PackageInfo {
35
6
  root: string;
@@ -60,7 +31,7 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
60
31
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
61
32
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
62
33
 
63
- type InitxMatcher<TMatcher extends object = object> = TMatcher & {
34
+ type InitxRuleFields<TRule extends object = object> = TRule & {
64
35
  /**
65
36
  * Description of the handler
66
37
  *
@@ -68,7 +39,7 @@ type InitxMatcher<TMatcher extends object = object> = TMatcher & {
68
39
  */
69
40
  description: string;
70
41
  };
71
- type InitxMatchers<TMatcher extends object = object> = Matchers<InitxMatcher<TMatcher>>;
42
+ type InitxMatcherRules<TRule extends object = object> = MatcherRules<InitxRuleFields<TRule>>;
72
43
  interface HandlerInfo {
73
44
  handler: () => MaybePromise<void>;
74
45
  description: string;
@@ -100,7 +71,7 @@ interface InitxRunContext extends InitxBaseContext {
100
71
  */
101
72
  packageInfo: PackageInfo;
102
73
  }
103
- interface InitxContext<TStore extends object = object, TMatcher extends object = object> extends InitxRunContext {
74
+ interface InitxContext<TStore extends object = object, TRule extends object = object> extends InitxRunContext {
104
75
  /**
105
76
  * Store
106
77
  *
@@ -108,14 +79,14 @@ interface InitxContext<TStore extends object = object, TMatcher extends object =
108
79
  */
109
80
  store: TStore;
110
81
  /**
111
- * Matcher
82
+ * Rule
112
83
  *
113
- * Matched matcher object, you can get custom fields, excluded `matching`
84
+ * Matched rule object, you can get custom fields, excluded `matching`
114
85
  */
115
- matcher: InitxMatcher<TMatcher>;
86
+ rule: InitxRuleFields<TRule>;
116
87
  }
117
88
  declare abstract class InitxPlugin<TStore extends object = object> {
118
- abstract matchers: InitxMatchers;
89
+ abstract rules: InitxMatcherRules;
119
90
  abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
120
91
  defaultStore?: TStore;
121
92
  run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
@@ -125,4 +96,4 @@ declare abstract class InitxPlugin<TStore extends object = object> {
125
96
  declare function createStore(name: string, defaultStore?: Record<string, any>): any;
126
97
  declare function writeStore(name: string): void;
127
98
 
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 };
99
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, type InitxMatcherRules as InitxMatchers, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
package/dist/index.d.ts CHANGED
@@ -1,35 +1,6 @@
1
- type MaybeArray<T> = T | T[];
2
- type MaybePromise<T> = T | Promise<T>;
1
+ import { MatcherRules } from 'matchinitx';
3
2
 
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>;
3
+ type MaybePromise<T> = T | Promise<T>;
33
4
 
34
5
  interface PackageInfo {
35
6
  root: string;
@@ -60,7 +31,7 @@ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
60
31
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
61
32
  declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
62
33
 
63
- type InitxMatcher<TMatcher extends object = object> = TMatcher & {
34
+ type InitxRuleFields<TRule extends object = object> = TRule & {
64
35
  /**
65
36
  * Description of the handler
66
37
  *
@@ -68,7 +39,7 @@ type InitxMatcher<TMatcher extends object = object> = TMatcher & {
68
39
  */
69
40
  description: string;
70
41
  };
71
- type InitxMatchers<TMatcher extends object = object> = Matchers<InitxMatcher<TMatcher>>;
42
+ type InitxMatcherRules<TRule extends object = object> = MatcherRules<InitxRuleFields<TRule>>;
72
43
  interface HandlerInfo {
73
44
  handler: () => MaybePromise<void>;
74
45
  description: string;
@@ -100,7 +71,7 @@ interface InitxRunContext extends InitxBaseContext {
100
71
  */
101
72
  packageInfo: PackageInfo;
102
73
  }
103
- interface InitxContext<TStore extends object = object, TMatcher extends object = object> extends InitxRunContext {
74
+ interface InitxContext<TStore extends object = object, TRule extends object = object> extends InitxRunContext {
104
75
  /**
105
76
  * Store
106
77
  *
@@ -108,14 +79,14 @@ interface InitxContext<TStore extends object = object, TMatcher extends object =
108
79
  */
109
80
  store: TStore;
110
81
  /**
111
- * Matcher
82
+ * Rule
112
83
  *
113
- * Matched matcher object, you can get custom fields, excluded `matching`
84
+ * Matched rule object, you can get custom fields, excluded `matching`
114
85
  */
115
- matcher: InitxMatcher<TMatcher>;
86
+ rule: InitxRuleFields<TRule>;
116
87
  }
117
88
  declare abstract class InitxPlugin<TStore extends object = object> {
118
- abstract matchers: InitxMatchers;
89
+ abstract rules: InitxMatcherRules;
119
90
  abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
120
91
  defaultStore?: TStore;
121
92
  run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
@@ -125,4 +96,4 @@ declare abstract class InitxPlugin<TStore extends object = object> {
125
96
  declare function createStore(name: string, defaultStore?: Record<string, any>): any;
126
97
  declare function writeStore(name: string): void;
127
98
 
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 };
99
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, type InitxMatcherRules as InitxMatchers, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
package/dist/index.mjs CHANGED
@@ -1,259 +1 @@
1
- import { homedir } from 'node:os';
2
- import path from 'node:path';
3
- import { defu } from 'defu';
4
- import fs from 'fs-extra';
5
- import { c } from '@initx-plugin/utils';
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
-
113
- let rewritedCache = null;
114
- const INITX_DIR = path.resolve(homedir(), ".initx");
115
- const STORE_FILE_NAME = "store.json";
116
- const resolveStore = (name) => path.resolve(INITX_DIR, name, STORE_FILE_NAME);
117
- function createStore(name, defaultStore = {}) {
118
- fs.ensureDirSync(path.resolve(INITX_DIR, name));
119
- const storePath = resolveStore(name);
120
- const generateResult = (resultData) => {
121
- writeJson(storePath, resultData);
122
- return useProxy(resultData);
123
- };
124
- if (!fs.existsSync(storePath)) {
125
- return generateResult(defaultStore);
126
- }
127
- let json;
128
- try {
129
- const fileJson = fs.readJsonSync(storePath);
130
- json = defu(fileJson, defaultStore);
131
- } catch (e) {
132
- json = defaultStore;
133
- }
134
- return generateResult(json);
135
- }
136
- function writeStore(name) {
137
- if (!rewritedCache) {
138
- return;
139
- }
140
- writeJson(resolveStore(name), rewritedCache);
141
- }
142
- function writeJson(path2, data) {
143
- fs.writeJsonSync(path2, data, {
144
- spaces: 2
145
- });
146
- }
147
- function useProxy(obj = {}) {
148
- const isPlainObject = (value) => {
149
- return Object.prototype.toString.call(value) === "[object Object]";
150
- };
151
- const createDeepProxy = (target) => {
152
- return new Proxy(target, {
153
- get(target2, key) {
154
- const value = Reflect.get(target2, key);
155
- if (isPlainObject(value)) {
156
- return createDeepProxy(value);
157
- }
158
- return value;
159
- },
160
- set(target2, key, value) {
161
- const success = Reflect.set(target2, key, value);
162
- rewritedCache = target2;
163
- return success;
164
- }
165
- });
166
- };
167
- return createDeepProxy(obj);
168
- }
169
-
170
- var __defProp = Object.defineProperty;
171
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
172
- var __publicField = (obj, key, value) => {
173
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
174
- return value;
175
- };
176
- class InitxPlugin {
177
- constructor() {
178
- __publicField(this, "defaultStore");
179
- }
180
- run(context, ...others) {
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;
193
- }
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);
198
- }
199
- }
200
-
201
- async function fetchPlugins() {
202
- const { content: nodeModules } = await c("npm", ["root", "-g"]);
203
- const communityPlugins = fs.readdirSync(nodeModules);
204
- const officialPluginPath = path.join(nodeModules, "@initx-plugin");
205
- const officialPlugins = fs.existsSync(officialPluginPath) ? fs.readdirSync(officialPluginPath).map((name) => `@initx-plugin/${name}`) : [];
206
- const regexps = {
207
- plugin: /^(?:@initx-plugin\/|initx-plugin-)/,
208
- exclude: /@initx-plugin\/(?:core|utils)$/
209
- };
210
- return [
211
- ...officialPlugins,
212
- ...communityPlugins
213
- ].filter(
214
- (name) => regexps.plugin.test(name) && !regexps.exclude.test(name)
215
- ).map((name) => ({
216
- name,
217
- root: path.join(nodeModules, name)
218
- }));
219
- }
220
- async function loadPlugins() {
221
- const pluginsInfo = await fetchPlugins();
222
- const x = await import('importx');
223
- return Promise.all(pluginsInfo.map(async ({ root }) => {
224
- const InitxPluginClass = await x.import(root, import.meta.url).then((x2) => x2.default);
225
- const packageAll = fs.readJsonSync(path.join(root, "package.json"));
226
- const packageInfo = {
227
- root,
228
- name: packageAll.name,
229
- version: packageAll.version,
230
- description: packageAll.description,
231
- author: packageAll.author,
232
- homepage: packageAll.homepage
233
- };
234
- return {
235
- packageInfo,
236
- instance: new InitxPluginClass()
237
- };
238
- }));
239
- }
240
- function matchPlugins(plugins, { key, cliOptions }, ...others) {
241
- const matchedHandlers = [];
242
- for (const plugin of plugins) {
243
- const { instance, packageInfo } = plugin;
244
- const matched = instance.run({
245
- key,
246
- cliOptions,
247
- packageInfo,
248
- optionsList: Object.keys(cliOptions).filter((key2) => cliOptions[key2] === true).map((key2) => `--${key2}`)
249
- }, ...others);
250
- matchedHandlers.push(...matched.map((item) => ({
251
- handler: item.handler,
252
- description: item.description,
253
- packageInfo
254
- })));
255
- }
256
- return matchedHandlers;
257
- }
258
-
259
- export { InitxPlugin, createStore, fetchPlugins, loadPlugins, matchPlugins, useInitxMatcher, writeStore };
1
+ import{useInitxMatcher as S}from"matchinitx";import{homedir as k}from"node:os";import u from"node:path";import{defu as j}from"defu";import c from"fs-extra";import{c as w}from"@initx-plugin/utils";let p=null;const m=u.resolve(k(),".initx"),I="store.json",f=r=>u.resolve(m,r,I);function d(r,e={}){c.ensureDirSync(u.resolve(m,r));const n=f(r),i=o=>(h(n,o),P(o));if(!c.existsSync(n))return i(e);let t;try{const o=c.readJsonSync(n);t=j(o,e)}catch{t=e}return i(t)}function g(r){p&&h(f(r),p)}function h(r,e){c.writeJsonSync(r,e,{spaces:2})}function P(r={}){const e=i=>Object.prototype.toString.call(i)==="[object Object]",n=i=>new Proxy(i,{get(t,o){const a=Reflect.get(t,o);return e(a)?n(a):a},set(t,o,a){const l=Reflect.set(t,o,a);return p=t,l}});return n(r)}class v{defaultStore;run(e,...n){return S((i,...t)=>({handler:()=>this.executeHandle(e,i,...t),description:i.description})).match(this.rules,e.key,...n)}async executeHandle(e,n,...i){const t=d(e.packageInfo.name,this.defaultStore);await this.handle({...e,rule:n,store:t},...i),g(e.packageInfo.name)}}async function y(){const{content:r}=await w("npm",["root","-g"]),e=c.readdirSync(r),n=u.join(r,"@initx-plugin"),i=c.existsSync(n)?c.readdirSync(n).map(o=>`@initx-plugin/${o}`):[],t={plugin:/^(?:@initx-plugin\/|initx-plugin-)/,exclude:/@initx-plugin\/(?:core|utils)$/};return[...i,...e].filter(o=>t.plugin.test(o)&&!t.exclude.test(o)).map(o=>({name:o,root:u.join(r,o)}))}async function O(){const r=await y(),e=await import("importx");return Promise.all(r.map(async({root:n})=>{const i=await e.import(n,import.meta.url).then(o=>o.default),t=c.readJsonSync(u.join(n,"package.json"));return{packageInfo:{root:n,name:t.name,version:t.version,description:t.description,author:t.author,homepage:t.homepage},instance:new i}}))}function b(r,{key:e,cliOptions:n},...i){const t=[];for(const o of r){const{instance:a,packageInfo:l}=o,x=a.run({key:e,cliOptions:n,packageInfo:l,optionsList:Object.keys(n).filter(s=>n[s]===!0).map(s=>`--${s}`)},...i);t.push(...x.map(s=>({handler:s.handler,description:s.description,packageInfo:l})))}return t}export{v as InitxPlugin,d as createStore,y as fetchPlugins,O as loadPlugins,b as matchPlugins,g as 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.26",
4
+ "version": "0.0.28",
5
5
  "description": "core module for initx plugins",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/initx-collective/initx#readme",
@@ -24,8 +24,9 @@
24
24
  "dependencies": {
25
25
  "defu": "^6.1.4",
26
26
  "fs-extra": "^11.2.0",
27
- "importx": "^0.5.0",
28
- "@initx-plugin/utils": "0.0.26"
27
+ "importx": "^0.5.1",
28
+ "matchinitx": "^0.0.2",
29
+ "@initx-plugin/utils": "0.0.28"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/fs-extra": "^11.0.4"