@initx-plugin/core 0.0.29 → 0.0.30

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,6 +1,7 @@
1
1
  import { MatcherRules } from 'matchinitx';
2
2
 
3
3
  type MaybePromise<T> = T | Promise<T>;
4
+ type OptionalValue = string | RegExp | undefined;
4
5
 
5
6
  interface PackageInfo {
6
7
  root: string;
@@ -29,7 +30,7 @@ type MatchedPlugin = HandlerInfo & {
29
30
  };
30
31
  declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
31
32
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
32
- declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
33
+ declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): Promise<MatchedPlugin[]>;
33
34
 
34
35
  type InitxRuleFields<TRule extends object = object> = TRule & {
35
36
  /**
@@ -38,12 +39,25 @@ type InitxRuleFields<TRule extends object = object> = TRule & {
38
39
  * If multiple handlers are matched, this description will be displayed
39
40
  */
40
41
  description: string;
42
+ /**
43
+ * Optional values
44
+ *
45
+ * Check if the first value is in the optional values.
46
+ *
47
+ * If no matching value is found in the optional value, the handler will not be executed.
48
+ */
49
+ optional?: OptionalValue[];
50
+ /**
51
+ * Verify function
52
+ *
53
+ * If the function returns false, the handler will not be executed.
54
+ */
55
+ verify?: (context: InitxRunContext, ...others: string[]) => MaybePromise<boolean>;
41
56
  };
42
57
  type InitxMatcherRules<TRule extends object = object> = MatcherRules<InitxRuleFields<TRule>>;
43
- interface HandlerInfo {
58
+ type HandlerInfo = InitxRuleFields & {
44
59
  handler: () => MaybePromise<void>;
45
- description: string;
46
- }
60
+ };
47
61
  interface InitxBaseContext {
48
62
  /**
49
63
  * Matching string
@@ -89,7 +103,7 @@ declare abstract class InitxPlugin<TStore extends object = object> {
89
103
  abstract rules: InitxMatcherRules;
90
104
  abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
91
105
  defaultStore?: TStore;
92
- run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
106
+ run(context: InitxRunContext, ...others: string[]): Promise<HandlerInfo[]>;
93
107
  private executeHandle;
94
108
  }
95
109
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { MatcherRules } from 'matchinitx';
2
2
 
3
3
  type MaybePromise<T> = T | Promise<T>;
4
+ type OptionalValue = string | RegExp | undefined;
4
5
 
5
6
  interface PackageInfo {
6
7
  root: string;
@@ -29,7 +30,7 @@ type MatchedPlugin = HandlerInfo & {
29
30
  };
30
31
  declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
31
32
  declare function loadPlugins(): Promise<LoadPluginResult[]>;
32
- declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
33
+ declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): Promise<MatchedPlugin[]>;
33
34
 
34
35
  type InitxRuleFields<TRule extends object = object> = TRule & {
35
36
  /**
@@ -38,12 +39,25 @@ type InitxRuleFields<TRule extends object = object> = TRule & {
38
39
  * If multiple handlers are matched, this description will be displayed
39
40
  */
40
41
  description: string;
42
+ /**
43
+ * Optional values
44
+ *
45
+ * Check if the first value is in the optional values.
46
+ *
47
+ * If no matching value is found in the optional value, the handler will not be executed.
48
+ */
49
+ optional?: OptionalValue[];
50
+ /**
51
+ * Verify function
52
+ *
53
+ * If the function returns false, the handler will not be executed.
54
+ */
55
+ verify?: (context: InitxRunContext, ...others: string[]) => MaybePromise<boolean>;
41
56
  };
42
57
  type InitxMatcherRules<TRule extends object = object> = MatcherRules<InitxRuleFields<TRule>>;
43
- interface HandlerInfo {
58
+ type HandlerInfo = InitxRuleFields & {
44
59
  handler: () => MaybePromise<void>;
45
- description: string;
46
- }
60
+ };
47
61
  interface InitxBaseContext {
48
62
  /**
49
63
  * Matching string
@@ -89,7 +103,7 @@ declare abstract class InitxPlugin<TStore extends object = object> {
89
103
  abstract rules: InitxMatcherRules;
90
104
  abstract handle(context: InitxContext, ...others: string[]): MaybePromise<void>;
91
105
  defaultStore?: TStore;
92
- run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
106
+ run(context: InitxRunContext, ...others: string[]): Promise<HandlerInfo[]>;
93
107
  private executeHandle;
94
108
  }
95
109
 
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{useInitxMatcher as k}from"matchinitx";import{homedir as w}from"node:os";import a from"node:path";import{defu as I}from"defu";import s from"fs-extra";import d from"node:process";import{c as $}from"@initx-plugin/utils";let m=null;const f=a.resolve(w(),".initx"),v="store.json",g=n=>a.resolve(f,n,v);function y(n,o={}){s.ensureDirSync(a.resolve(f,n));const i=g(n),e=c=>(x(i,c),O(c));if(!s.existsSync(i))return e(o);let t;try{const c=s.readJsonSync(i);t=I(c,o)}catch{t=o}return e(t)}function h(n){m&&x(g(n),m)}function x(n,o){s.writeJsonSync(n,o,{spaces:2})}function O(n={}){const o=e=>Object.prototype.toString.call(e)==="[object Object]",i=e=>new Proxy(e,{get(t,c){const r=Reflect.get(t,c);return o(r)?i(r):r},set(t,c,r){const u=Reflect.set(t,c,r);return m=t,u}});return i(n)}class P{defaultStore;run(o,...i){return k((e,...t)=>({handler:()=>this.executeHandle(o,e,...t),description:e.description})).match(this.rules,o.key,...i)}async executeHandle(o,i,...e){const t=y(o.packageInfo.name,this.defaultStore);await this.handle({...o,rule:i,store:t},...e),h(o.packageInfo.name)}}const l={plugin:/^(?:@initx-plugin\/|initx-plugin-)/,exclude:/@initx-plugin\/(?:core|utils)$/};async function b(){const n=a.join(d.cwd(),"package.json");if(!s.existsSync(n))return[];const o=s.readJsonSync(n),{dependencies:i={},devDependencies:e={}}=o;return Object.keys({...i,...e}).filter(t=>l.plugin.test(t)&&!l.exclude.test(t)).map(t=>({name:t,root:a.join(d.cwd(),"node_modules",t)}))}async function S(){const{content:n}=await $("npm",["root","-g"]),o=s.readdirSync(n),i=a.join(n,"@initx-plugin");return[...s.existsSync(i)?s.readdirSync(i).map(e=>`@initx-plugin/${e}`):[],...o].filter(e=>l.plugin.test(e)&&!l.exclude.test(e)).map(e=>({name:e,root:a.join(n,e)}))}async function J(){const n=await b(),o=n.map(({name:t})=>t),i=[...(await S()).filter(({name:t})=>!o.includes(t)),...n],e=await import("importx");return Promise.all(i.map(async({root:t})=>{const c=await e.import(t,import.meta.url).then(u=>u.default),r=s.readJsonSync(a.join(t,"package.json"));return{packageInfo:{root:t,name:r.name,version:r.version,description:r.description,author:r.author,homepage:r.homepage},instance:new c}}))}function D(n,{key:o,cliOptions:i},...e){const t=[];for(const c of n){const{instance:r,packageInfo:u}=c,j=r.run({key:o,cliOptions:i,packageInfo:u,optionsList:Object.keys(i).filter(p=>i[p]===!0).map(p=>`--${p}`)},...e);t.push(...j.map(p=>({handler:p.handler,description:p.description,packageInfo:u})))}return t}export{P as InitxPlugin,y as createStore,S as fetchPlugins,J as loadPlugins,D as matchPlugins,h as writeStore};
1
+ import{useInitxMatcher as j}from"matchinitx";import{homedir as k}from"node:os";import s from"node:path";import{defu as v}from"defu";import c from"fs-extra";import f from"node:process";import{c as I}from"@initx-plugin/utils";let m=null;const d=s.resolve(k(),".initx"),O="store.json",y=o=>s.resolve(d,o,O);function g(o,t={}){c.ensureDirSync(s.resolve(d,o));const n=y(o),i=r=>(x(n,r),P(r));if(!c.existsSync(n))return i(t);let e;try{const r=c.readJsonSync(n);e=v(r,t)}catch{e=t}return i(e)}function h(o){m&&x(y(o),m)}function x(o,t){c.writeJsonSync(o,t,{spaces:2})}function P(o={}){const t=i=>Object.prototype.toString.call(i)==="[object Object]",n=i=>new Proxy(i,{get(e,r){const a=Reflect.get(e,r);return t(a)?n(a):a},set(e,r,a){const l=Reflect.set(e,r,a);return m=e,l}});return n(o)}const p={plugin:/^(?:@initx-plugin\/|initx-plugin-)/,exclude:/@initx-plugin\/(?:core|utils)$/};async function $(){const o=s.join(f.cwd(),"package.json");if(!c.existsSync(o))return[];const t=c.readJsonSync(o),{dependencies:n={},devDependencies:i={}}=t;return Object.keys({...n,...i}).filter(e=>p.plugin.test(e)&&!p.exclude.test(e)).map(e=>({name:e,root:s.join(f.cwd(),"node_modules",e)}))}async function S(){const{content:o}=await I("npm",["root","-g"]),t=c.readdirSync(o),n=s.join(o,"@initx-plugin");return[...c.existsSync(n)?c.readdirSync(n).map(i=>`@initx-plugin/${i}`):[],...t].filter(i=>p.plugin.test(i)&&!p.exclude.test(i)).map(i=>({name:i,root:s.join(o,i)}))}async function b(){const o=await $(),t=o.map(({name:e})=>e),n=[...(await S()).filter(({name:e})=>!t.includes(e)),...o],i=await import("importx");return Promise.all(n.map(async({root:e})=>{const r=await i.import(e,import.meta.url).then(l=>l.default),a=c.readJsonSync(s.join(e,"package.json"));return{packageInfo:{root:e,name:a.name,version:a.version,description:a.description,author:a.author,homepage:a.homepage},instance:new r}}))}async function J(o,{key:t,cliOptions:n},...i){const e=[];for(const r of o){const{instance:a,packageInfo:l}=r,w=await a.run({key:t,cliOptions:n,packageInfo:l,optionsList:Object.keys(n).filter(u=>n[u]===!0).map(u=>`--${u}`)},...i);e.push(...w.map(u=>({handler:u.handler,description:u.description,packageInfo:l})))}return e}function D(o,t){return o.some(n=>typeof n=="string"||typeof n>"u"?n===t:n.test(t))}class H{defaultStore;async run(t,...n){const i=j((e,...r)=>({handler:()=>this.executeHandle(t,e,...r),...e}));return(await Promise.all(i.match(this.rules,t.key,...n).map(async e=>e.verify&&!e.verify(t,...n)||e.optional&&!D(e.optional,n[0])?!1:e))).filter(Boolean)}async executeHandle(t,n,...i){const e=g(t.packageInfo.name,this.defaultStore);await this.handle({...t,rule:n,store:e},...i),h(t.packageInfo.name)}}export{H as InitxPlugin,g as createStore,S as fetchPlugins,b as loadPlugins,J as matchPlugins,h 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.29",
4
+ "version": "0.0.30",
5
5
  "description": "core module for initx plugins",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/initx-collective/initx#readme",
@@ -23,10 +23,10 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "defu": "^6.1.4",
26
- "fs-extra": "^11.2.0",
27
- "importx": "^0.5.1",
28
- "matchinitx": "^0.0.2",
29
- "@initx-plugin/utils": "0.0.29"
26
+ "fs-extra": "^11.3.0",
27
+ "importx": "^0.5.2",
28
+ "matchinitx": "^0.0.4",
29
+ "@initx-plugin/utils": "0.0.30"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/fs-extra": "^11.0.4"