@initx-plugin/core 0.0.21 → 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>;
@@ -55,23 +66,25 @@ interface InitxBaseContext {
55
66
  */
56
67
  optionsList: string[];
57
68
  }
58
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxBaseContext {
69
+ interface InitxRunContext extends InitxBaseContext {
70
+ /**
71
+ * Package info
72
+ */
73
+ packageInfo: PackageInfo;
74
+ }
75
+ interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
59
76
  /**
60
77
  * Store
61
78
  *
62
79
  * Store data in memory, and write to disk when the program exits
63
80
  */
64
81
  store: TStore;
65
- /**
66
- * Package info
67
- */
68
- packageInfo: PackageInfo;
69
82
  }
70
83
  declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
71
84
  abstract matchers: Matchers;
72
85
  abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
73
86
  defaultStore?: TStore;
74
- run(context: InitxContext<TStore>, ...others: string[]): HandlerInfo[];
87
+ run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
75
88
  private matchBaseMatchers;
76
89
  private matchArrayBaseMatchers;
77
90
  private matchTypeMatchers;
@@ -85,4 +98,4 @@ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
85
98
  declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
86
99
  declare function writeStore({ name }: PackageInfo): void;
87
100
 
88
- 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>;
@@ -55,23 +66,25 @@ interface InitxBaseContext {
55
66
  */
56
67
  optionsList: string[];
57
68
  }
58
- interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxBaseContext {
69
+ interface InitxRunContext extends InitxBaseContext {
70
+ /**
71
+ * Package info
72
+ */
73
+ packageInfo: PackageInfo;
74
+ }
75
+ interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxRunContext {
59
76
  /**
60
77
  * Store
61
78
  *
62
79
  * Store data in memory, and write to disk when the program exits
63
80
  */
64
81
  store: TStore;
65
- /**
66
- * Package info
67
- */
68
- packageInfo: PackageInfo;
69
82
  }
70
83
  declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
71
84
  abstract matchers: Matchers;
72
85
  abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
73
86
  defaultStore?: TStore;
74
- run(context: InitxContext<TStore>, ...others: string[]): HandlerInfo[];
87
+ run(context: InitxRunContext, ...others: string[]): HandlerInfo[];
75
88
  private matchBaseMatchers;
76
89
  private matchArrayBaseMatchers;
77
90
  private matchTypeMatchers;
@@ -85,4 +98,4 @@ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
85
98
  declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
86
99
  declare function writeStore({ name }: PackageInfo): void;
87
100
 
88
- 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
  });
@@ -148,12 +144,13 @@ class InitxPlugin {
148
144
  });
149
145
  }
150
146
  async executeHandle(context, ...others) {
151
- await this.handle(context, ...others);
147
+ const store = createStore(context.packageInfo, this.defaultStore);
148
+ await this.handle({ ...context, store }, ...others);
152
149
  writeStore(context.packageInfo);
153
150
  }
154
151
  }
155
152
 
156
- async function loadPlugins() {
153
+ async function fetchPlugins() {
157
154
  const { content: npmPath } = await c("npm", ["config", "get", "prefix"]);
158
155
  const nodeModules = path.join(npmPath, "node_modules");
159
156
  const communityPlugins = fs.readdirSync(nodeModules);
@@ -163,19 +160,24 @@ async function loadPlugins() {
163
160
  plugin: /^(?:@initx-plugin\/|initx-plugin-)/,
164
161
  exclude: /@initx-plugin\/(?:core|utils)$/
165
162
  };
166
- const pluginsName = [
163
+ return [
167
164
  ...officialPlugins,
168
165
  ...communityPlugins
169
166
  ].filter(
170
167
  (name) => regexps.plugin.test(name) && !regexps.exclude.test(name)
171
- );
168
+ ).map((name) => ({
169
+ name,
170
+ root: path.join(nodeModules, name)
171
+ }));
172
+ }
173
+ async function loadPlugins() {
174
+ const pluginsInfo = await fetchPlugins();
172
175
  const x = await import('importx');
173
- return Promise.all(pluginsName.map(async (dirname) => {
174
- const pluginRoot = path.join(nodeModules, dirname);
175
- const InitxPluginClass = await x.import(pluginRoot, import.meta.url).then((x2) => x2.default);
176
- 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"));
177
179
  const packageInfo = {
178
- root: pluginRoot,
180
+ root,
179
181
  name: packageAll.name,
180
182
  version: packageAll.version,
181
183
  description: packageAll.description,
@@ -192,10 +194,8 @@ function matchPlugins(plugins, { key, cliOptions }, ...others) {
192
194
  const matchedHandlers = [];
193
195
  for (const plugin of plugins) {
194
196
  const { instance, packageInfo } = plugin;
195
- const store = createStore(packageInfo, instance.defaultStore);
196
197
  const matched = instance.run({
197
198
  key,
198
- store,
199
199
  cliOptions,
200
200
  packageInfo,
201
201
  optionsList: Object.keys(cliOptions).filter((key2) => cliOptions[key2] === true).map((key2) => `--${key2}`)
@@ -209,4 +209,4 @@ function matchPlugins(plugins, { key, cliOptions }, ...others) {
209
209
  return matchedHandlers;
210
210
  }
211
211
 
212
- 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.21",
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.21"
28
+ "@initx-plugin/utils": "0.0.23"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "^11.0.4"