@initx-plugin/core 0.0.22 → 0.0.23

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
@@ -7,14 +7,25 @@ interface PackageInfo {
7
7
  homepage?: string;
8
8
  }
9
9
  interface InitxPluginInfo {
10
+ /**
11
+ * Plugin name
12
+ */
13
+ name: string;
14
+ /**
15
+ * Plugin root path
16
+ */
17
+ root: string;
18
+ }
19
+ interface LoadPluginResult {
10
20
  packageInfo: PackageInfo;
11
21
  instance: InitxPlugin;
12
22
  }
13
23
  type MatchedPlugin = HandlerInfo & {
14
24
  packageInfo: PackageInfo;
15
25
  };
16
- declare function loadPlugins(): Promise<InitxPluginInfo[]>;
17
- declare function matchPlugins(plugins: InitxPluginInfo[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
26
+ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
+ declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
+ declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
18
29
 
19
30
  type MaybeArray<T> = T | T[];
20
31
  type MaybePromise<T> = T | Promise<T>;
@@ -87,4 +98,4 @@ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
87
98
  declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
88
99
  declare function writeStore({ name }: PackageInfo): void;
89
100
 
90
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type MatchedPlugin, type PackageInfo, createStore, loadPlugins, matchPlugins, writeStore };
101
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
package/dist/index.d.ts CHANGED
@@ -7,14 +7,25 @@ interface PackageInfo {
7
7
  homepage?: string;
8
8
  }
9
9
  interface InitxPluginInfo {
10
+ /**
11
+ * Plugin name
12
+ */
13
+ name: string;
14
+ /**
15
+ * Plugin root path
16
+ */
17
+ root: string;
18
+ }
19
+ interface LoadPluginResult {
10
20
  packageInfo: PackageInfo;
11
21
  instance: InitxPlugin;
12
22
  }
13
23
  type MatchedPlugin = HandlerInfo & {
14
24
  packageInfo: PackageInfo;
15
25
  };
16
- declare function loadPlugins(): Promise<InitxPluginInfo[]>;
17
- declare function matchPlugins(plugins: InitxPluginInfo[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
26
+ declare function fetchPlugins(): Promise<InitxPluginInfo[]>;
27
+ declare function loadPlugins(): Promise<LoadPluginResult[]>;
28
+ declare function matchPlugins(plugins: LoadPluginResult[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
18
29
 
19
30
  type MaybeArray<T> = T | T[];
20
31
  type MaybePromise<T> = T | Promise<T>;
@@ -87,4 +98,4 @@ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
87
98
  declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
88
99
  declare function writeStore({ name }: PackageInfo): void;
89
100
 
90
- export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type MatchedPlugin, type PackageInfo, createStore, loadPlugins, matchPlugins, writeStore };
101
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type LoadPluginResult, type MatchedPlugin, type PackageInfo, createStore, fetchPlugins, loadPlugins, matchPlugins, writeStore };
package/dist/index.mjs CHANGED
@@ -4,17 +4,16 @@ import fs from 'fs-extra';
4
4
  import { defu } from 'defu';
5
5
  import { c } from '@initx-plugin/utils';
6
6
 
7
+ let rewritedCache = null;
7
8
  const INITX_DIR = path.resolve(homedir(), ".initx");
8
9
  const STORE_FILE_NAME = "store.json";
9
- const REWRITED_FILE_NAME = ".rewrited";
10
10
  const resolveStore = (name) => path.resolve(INITX_DIR, name, STORE_FILE_NAME);
11
- const resolveRewrited = (name) => path.resolve(INITX_DIR, name, REWRITED_FILE_NAME);
12
11
  function createStore({ name }, defaultStore = {}) {
13
12
  fs.ensureDirSync(path.resolve(INITX_DIR, name));
14
13
  const storePath = resolveStore(name);
15
14
  const generateResult = (resultData) => {
16
15
  writeJson(storePath, resultData);
17
- return useProxy(name, resultData);
16
+ return useProxy(resultData);
18
17
  };
19
18
  if (!fs.existsSync(storePath)) {
20
19
  return generateResult(defaultStore);
@@ -29,20 +28,17 @@ function createStore({ name }, defaultStore = {}) {
29
28
  return generateResult(json);
30
29
  }
31
30
  function writeStore({ name }) {
32
- const rewritedPath = resolveRewrited(name);
33
- if (!fs.existsSync(rewritedPath)) {
31
+ if (!rewritedCache) {
34
32
  return;
35
33
  }
36
- const rewrited = fs.readJsonSync(rewritedPath);
37
- writeJson(resolveStore(name), rewrited);
38
- fs.removeSync(rewritedPath);
34
+ writeJson(resolveStore(name), rewritedCache);
39
35
  }
40
36
  function writeJson(path2, data) {
41
37
  fs.writeJsonSync(path2, data, {
42
38
  spaces: 2
43
39
  });
44
40
  }
45
- function useProxy(name, obj = {}) {
41
+ function useProxy(obj = {}) {
46
42
  const isPlainObject = (value) => {
47
43
  return Object.prototype.toString.call(value) === "[object Object]";
48
44
  };
@@ -57,7 +53,7 @@ function useProxy(name, obj = {}) {
57
53
  },
58
54
  set(target2, key, value) {
59
55
  const success = Reflect.set(target2, key, value);
60
- fs.writeJsonSync(resolveRewrited(name), target2);
56
+ rewritedCache = target2;
61
57
  return success;
62
58
  }
63
59
  });
@@ -154,7 +150,7 @@ class InitxPlugin {
154
150
  }
155
151
  }
156
152
 
157
- async function loadPlugins() {
153
+ async function fetchPlugins() {
158
154
  const { content: npmPath } = await c("npm", ["config", "get", "prefix"]);
159
155
  const nodeModules = path.join(npmPath, "node_modules");
160
156
  const communityPlugins = fs.readdirSync(nodeModules);
@@ -164,19 +160,24 @@ async function loadPlugins() {
164
160
  plugin: /^(?:@initx-plugin\/|initx-plugin-)/,
165
161
  exclude: /@initx-plugin\/(?:core|utils)$/
166
162
  };
167
- const pluginsName = [
163
+ return [
168
164
  ...officialPlugins,
169
165
  ...communityPlugins
170
166
  ].filter(
171
167
  (name) => regexps.plugin.test(name) && !regexps.exclude.test(name)
172
- );
168
+ ).map((name) => ({
169
+ name,
170
+ root: path.join(nodeModules, name)
171
+ }));
172
+ }
173
+ async function loadPlugins() {
174
+ const pluginsInfo = await fetchPlugins();
173
175
  const x = await import('importx');
174
- return Promise.all(pluginsName.map(async (dirname) => {
175
- const pluginRoot = path.join(nodeModules, dirname);
176
- const InitxPluginClass = await x.import(pluginRoot, import.meta.url).then((x2) => x2.default);
177
- const packageAll = JSON.parse(fs.readFileSync(path.join(nodeModules, dirname, "package.json"), "utf-8"));
176
+ return Promise.all(pluginsInfo.map(async ({ root }) => {
177
+ const InitxPluginClass = await x.import(root, import.meta.url).then((x2) => x2.default);
178
+ const packageAll = fs.readJsonSync(path.join(root, "package.json"));
178
179
  const packageInfo = {
179
- root: pluginRoot,
180
+ root,
180
181
  name: packageAll.name,
181
182
  version: packageAll.version,
182
183
  description: packageAll.description,
@@ -208,4 +209,4 @@ function matchPlugins(plugins, { key, cliOptions }, ...others) {
208
209
  return matchedHandlers;
209
210
  }
210
211
 
211
- export { InitxPlugin, createStore, loadPlugins, matchPlugins, writeStore };
212
+ export { InitxPlugin, createStore, fetchPlugins, loadPlugins, matchPlugins, 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.22",
4
+ "version": "0.0.23",
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.22"
28
+ "@initx-plugin/utils": "0.0.23"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "^11.0.4"