@initx-plugin/core 0.2.0 → 0.3.1
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 +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -179,6 +179,11 @@ declare class NpmPluginSystem<T = any> implements PluginSystem<T> {
|
|
|
179
179
|
update(packageName: string, version?: string): Promise<void>;
|
|
180
180
|
load(packageName: string): Promise<T>;
|
|
181
181
|
resolve(packageName: string, ...paths: string[]): string;
|
|
182
|
+
/**
|
|
183
|
+
* Ensure plugin cache is valid. Rebuild if invalid.
|
|
184
|
+
* Call this after updating core packages or if cache was manually deleted.
|
|
185
|
+
*/
|
|
186
|
+
ensureCacheValid(): Promise<void>;
|
|
182
187
|
}
|
|
183
188
|
|
|
184
189
|
type Constructor<T> = new (...args: any[]) => T;
|
package/dist/index.d.ts
CHANGED
|
@@ -179,6 +179,11 @@ declare class NpmPluginSystem<T = any> implements PluginSystem<T> {
|
|
|
179
179
|
update(packageName: string, version?: string): Promise<void>;
|
|
180
180
|
load(packageName: string): Promise<T>;
|
|
181
181
|
resolve(packageName: string, ...paths: string[]): string;
|
|
182
|
+
/**
|
|
183
|
+
* Ensure plugin cache is valid. Rebuild if invalid.
|
|
184
|
+
* Call this after updating core packages or if cache was manually deleted.
|
|
185
|
+
*/
|
|
186
|
+
ensureCacheValid(): Promise<void>;
|
|
182
187
|
}
|
|
183
188
|
|
|
184
189
|
type Constructor<T> = new (...args: any[]) => T;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{homedir as
|
|
1
|
+
import{homedir as R}from"node:os";import u from"fs-extra";import l,{resolve as w,join as o}from"pathe";import{useInitxMatcher as J}from"matchinitx";import{defu as A}from"defu";import j,{cwd as T}from"node:process";import{exec as U}from"node:child_process";import{createRequire as y}from"node:module";import{promisify as V}from"node:util";import{pathToFileURL as H}from"node:url";const v=w(R(),".initx"),x=w(v,"stores"),D="store.json",p=w(v,"plugins"),E=".plugins.json",z=u.existsSync(w(p,"lib"))?"lib/node_modules":"node_modules";let P=!1,$={};const S=n=>l.resolve(x,n,D);function I(n,e={}){u.ensureDirSync(l.resolve(x,n));const t=S(n),a=r=>(O(t,r),G(r));if(!u.existsSync(t))return a(e);let i;try{const r=u.readJsonSync(t);i=A(r,e)}catch{i=e}return a(i)}function N(n){P&&O(S(n),$)}function O(n,e){u.writeJsonSync(n,e,{spaces:2})}function G(n={}){const e=a=>typeof a=="object"&&a!==null&&new Set(["[object Object]","[object Array]"]).has(Object.prototype.toString.call(a)),t=a=>new Proxy(a,{get(i,r){const s=Reflect.get(i,r);return e(s)?t(s):s},set(i,r,s){const c=Reflect.set(i,r,s);return P=!0,c},deleteProperty(i,r){const s=Reflect.deleteProperty(i,r);return P=!0,s}});return $=t(n),$}const W=y(import.meta.url),{pathExists:L,readJSON:q,writeJSON:B}=W("fs-extra");class X{cachePath;constructor(e=p){this.cachePath=o(e,E)}async read(){return await L(this.cachePath)?await q(this.cachePath):{}}async write(e){await B(this.cachePath,e)}async updateOne(e,t){const a=await this.read();a[e]=t,await this.write(a)}async removeOne(e){const t=await this.read();e in t&&(delete t[e],await this.write(t))}async rebuild(e){await this.write(e)}async validate(){if(!await L(this.cachePath))return!1;const e=await this.read();for(const t of Object.values(e))if(!t.version||t.resolved===void 0||t.overridden===void 0)return!1;return!0}async clear(){await this.write({})}}const K=y(import.meta.url),{ensureDir:Q,pathExists:m,readJSON:g}=K("fs-extra"),Y=V(U);class Z{constructor(e,t={}){this.pluginDir=e,this.registry=t.registry||"https://registry.npmjs.org",this.npmCommand=t.npmPath||"npm",this.cache=new X(e)}registry;npmCommand;cache;async executeNpmCommand(e){const t=`${this.npmCommand} ${e}`;try{return await Y(t)}catch(a){throw a.code==="ENOENT"?new Error(`npm command not found: ${this.npmCommand}. Please ensure npm is installed or specify a custom npm path using the 'npmPath' option.`):a}}async install(e,t){const{packageName:a,cachedInfo:i}=await this.resolvePackageName(e);if(h.exclude.test(a))throw new Error(`Cannot install core package: ${a}`);await Q(this.pluginDir);let r;if(k(e))await this.installFromLocal(e),r=i;else{await this.installFromRegistry(e,t);const s=await this.getPackageInfo(e);if(!s)throw new Error(`Failed to get package info for ${e}, package.json may not exist`);r=s}await this.cache.updateOne(a,r)}async resolvePackageName(e){if(k(e)){const t=_(e),a=await g(o(t,"package.json")),i=a.name;if(typeof i!="string"||i.length===0)throw new Error(`Local package at ${t} must define a valid name in package.json`);return{packageName:i,cachedInfo:{version:a.version||"",resolved:"",overridden:!1,description:a.description||""}}}return{packageName:e}}async installFromLocal(e){const t=_(e);if(!await he(t))throw new Error(`Local path does not exist or does not contain a valid package.json: ${t}`);const a=`install "${t}" --prefix "${this.pluginDir}"`;try{await this.executeNpmCommand(a)}catch(i){throw new Error(`Failed to install plugin from local path ${t}: ${i.message}`)}}async installFromRegistry(e,t){const a=t?`@${t}`:"",i=`install ${e}${a} --prefix "${this.pluginDir}" --registry ${this.registry}`;try{await this.executeNpmCommand(i)}catch(r){throw new Error(`Failed to install plugin ${e}: ${r.message}`)}}async uninstall(e){const t=o(this.pluginDir,"node_modules",e);if(!await m(t))throw new Error(`Plugin ${e} is not installed`);const a=`uninstall ${e} --prefix "${this.pluginDir}"`;try{await this.executeNpmCommand(a),await this.cache.removeOne(e)}catch(i){throw new Error(`Failed to uninstall plugin ${e}: ${i.message}`)}}async list(){const e=await this.cache.read();return Object.keys(e).length>0&&await this.cache.validate()?e:await this.rebuildCache()}async rebuildCache(){const e={};try{const t=o(T(),"package.json"),a=await g(t),i={...a.dependencies,...a.devDependencies};for(const[r]of Object.entries(i)){if(!h.plugin.test(r)||h.exclude.test(r))continue;const s=await this.getPackageInfo(r);s&&(e[r]=s)}}catch{}return await this.cache.rebuild(e),e}async ensureCacheValid(){await this.cache.validate()||await this.rebuildCache()}async getPackageInfo(e){const t=o(this.pluginDir,"node_modules",e,"package.json");if(!await m(t))return null;try{const a=await g(t);return{version:a.version,resolved:"",overridden:!1,description:a.description||""}}catch{return null}}async getPackageDescription(e){const t=o(this.pluginDir,"node_modules",e,"package.json");if(!await m(t))return"";try{return(await g(t)).description||""}catch{return""}}async search(e){const t=`search ${e} --json --registry ${this.registry}`;try{const{stdout:a}=await this.executeNpmCommand(t);return JSON.parse(a)}catch(a){throw new Error(`Failed to search plugins: ${a.message}`)}}async isInstalled(e){const t=o(this.pluginDir,"node_modules",e);return await m(t)}async getInstalledVersion(e){const t=o(this.pluginDir,"node_modules",e,"package.json");if(!await m(t))return null;try{return(await g(t)).version}catch{return null}}}const ee=y(import.meta.url),{pathExists:te,readJSON:ae}=ee("fs-extra");class ne{constructor(e){this.pluginDir=e}cache=new Map;async load(e){if(this.cache.has(e))return this.cache.get(e);const t=o(this.pluginDir,"node_modules",e),a=o(t,"package.json");if(!await te(a))throw new Error(`Plugin package not found: ${e}`);const i=await ae(a),r=o(t,i.main||"index.js");try{const s=await import(H(r).href),c=s.default??s;if(typeof c!="function")throw new TypeError("Plugin must export a class or function");return this.cache.set(e,c),c}catch(s){throw new Error(`Failed to load plugin ${e}: ${s.message}`)}}unload(e){return this.cache.delete(e)}clearCache(){this.cache.clear()}getLoadedPlugins(){return Array.from(this.cache.keys())}}class ie{npmManager;pluginLoader;pluginDir;constructor(e,t={}){this.pluginDir=t.pluginDir??p,this.npmManager=new Z(this.pluginDir,{registry:t.registry,npmPath:t.npmPath}),this.pluginLoader=new ne(this.pluginDir)}async search(e){return await this.npmManager.search(e)}async install(e,t){await this.npmManager.install(e,t)}async uninstall(e){this.pluginLoader.unload(e),await this.npmManager.uninstall(e)}async list(){const e=await this.npmManager.list();return Object.entries(e).map(([t,a])=>({name:t,version:a.version,description:a.description,isLocal:k(a.resolved)}))}async update(e,t){await this.uninstall(e),await this.install(e,t)}async load(e){return await this.pluginLoader.load(e)}resolve(e,...t){return o(this.pluginDir,"node_modules",e,...t)}async ensureCacheValid(){await this.npmManager.ensureCacheValid()}}function b(n,e){return new ie(n,e)}const f=b("initx",{pluginDir:p}),h={plugin:/^(?:@initx-plugin\/|initx-plugin-)/,exclude:/@initx-plugin\/(?:core|utils)$/,pluginId:/^[a-z][a-z0-9-]*$/,relativePath:/^\.\.?(?:$|[/\\])/};async function re(n){const e=l.resolve(n,"package.json");if(!u.existsSync(e))return[];const t=u.readJsonSync(e),{dependencies:a={},devDependencies:i={}}=t,r=[];return Object.keys({...a,...i}).forEach(s=>{if(!h.plugin.test(s)||h.exclude.test(s))return;const c=l.resolve(n,"node_modules",s);u.existsSync(c)&&r.push({name:s,version:t.version,description:t.description,root:c})}),r}async function se(){return re(j.cwd())}async function C(){return(await f.list()).filter(n=>h.plugin.test(n.name)&&!h.exclude.test(n.name)).map(n=>({name:n.name,version:n.version,description:n.description,root:l.resolve(p,"node_modules",n.name)}))}async function oe(){const n=await se(),e=n.map(({name:a})=>a),t=[...(await C()).filter(({name:a})=>!e.includes(a)),...n];return Promise.all(t.map(async({name:a,root:i})=>{const r=await f.load(a),s=u.readJsonSync(l.resolve(i,"package.json"));return{packageInfo:{root:i,name:s.name,version:s.version,description:s.description,author:s.author,homepage:s.homepage},instance:new r}}))}async function ce(n,{key:e,cliOptions:t},...a){const i=[];for(const r of n){const{instance:s,packageInfo:c}=r,F=await s.run({key:e,cliOptions:t,packageInfo:c,optionsList:Object.keys(t).filter(d=>t[d]===!0).map(d=>`--${d}`)},...a);i.push(...F.map(d=>({handler:d.handler,description:d.description,packageInfo:c})))}return i}function le(n,e){return n.some(t=>typeof t=="string"||typeof t>"u"?t===e:t.test(e))}function ue(n){return n.push("--prefix",p),n}function k(n){return!!(n.startsWith("file:")||l.isAbsolute(n)||h.relativePath.test(n))}function _(n,e=j.cwd()){if(n.startsWith("file:")){const t=n.slice(5);return l.isAbsolute(t)?t:l.resolve(e,t)}return l.resolve(e,n)}async function he(n){try{const e=l.join(n,"package.json");return await u.pathExists(e)}catch{return!1}}class pe{defaultStore;async run(e,...t){const a=J((i,...r)=>({handler:()=>this.executeHandle(e,i,...r),...i}));return(await Promise.all(a.match(this.rules,e.key,...t).map(async i=>i.verify&&!i.verify(e,...t)||i.optional&&!le(i.optional,t[0])?!1:i))).filter(Boolean)}async executeHandle(e,t,...a){const i=I(e.packageInfo.name,this.defaultStore);await this.handle({...e,rule:t,store:i},...a),N(e.packageInfo.name)}}const M="@initx-plugin/manager";async function de(){try{return(await f.list()).some(n=>n.name===M)}catch{return!1}}async function me(){await f.install(M)}export{v as INITX_DIR,pe as InitxPlugin,z as NODE_MODULES_DIR,E as PLUGINS_CACHE_FILE,p as PLUGIN_DIR,x as STORE_DIR,D as STORE_FILE_NAME,b as createNpmPlugin,I as createStore,de as detectManager,C as fetchPlugins,me as installManager,oe as loadPlugins,ce as matchPlugins,f as pluginSystem,ue as withPluginPrefix,N 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.
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"description": "core module for initx plugins",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/initx-collective/initx#readme",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"fs-extra": "^11.3.4",
|
|
27
27
|
"matchinitx": "^0.0.4",
|
|
28
28
|
"pathe": "^2.0.3",
|
|
29
|
-
"@initx-plugin/utils": "0.
|
|
29
|
+
"@initx-plugin/utils": "0.3.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/fs-extra": "^11.0.4"
|