@initx-plugin/core 0.0.17 → 0.0.19

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,3 +1,21 @@
1
+ interface PackageInfo {
2
+ root: string;
3
+ name: string;
4
+ version: string;
5
+ description: string;
6
+ author: string;
7
+ homepage?: string;
8
+ }
9
+ interface InitxPluginInfo {
10
+ packageInfo: PackageInfo;
11
+ instance: InitxPlugin;
12
+ }
13
+ type MatchedPlugin = HandlerInfo & {
14
+ packageInfo: PackageInfo;
15
+ };
16
+ declare function loadPlugins(): Promise<InitxPluginInfo[]>;
17
+ declare function matchPlugins(plugins: InitxPluginInfo[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
18
+
1
19
  type MaybeArray<T> = T | T[];
2
20
  type MaybePromise<T> = T | Promise<T>;
3
21
  interface BaseMatchers {
@@ -11,11 +29,12 @@ interface BaseMatchers {
11
29
  }
12
30
  type TypeMatchers = Record<string, BaseMatchers>;
13
31
  type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
32
+ type PluginConfig = Record<string, any>;
14
33
  interface HandlerInfo {
15
34
  handler: () => MaybePromise<void>;
16
35
  description: string;
17
36
  }
18
- interface InitxCtx {
37
+ interface InitxBaseContext {
19
38
  /**
20
39
  * Matching string
21
40
  *
@@ -36,10 +55,23 @@ interface InitxCtx {
36
55
  */
37
56
  optionsList: string[];
38
57
  }
39
- declare abstract class InitxHandler {
58
+ interface InitxContext<TConfig extends PluginConfig = PluginConfig> extends InitxBaseContext {
59
+ /**
60
+ * Store
61
+ *
62
+ * Store data in memory, and write to disk when the program exits
63
+ */
64
+ store: TConfig;
65
+ /**
66
+ * Package info
67
+ */
68
+ packageInfo: PackageInfo;
69
+ }
70
+ declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig> {
40
71
  abstract matchers: Matchers;
41
- abstract handle(options: InitxCtx, ...others: string[]): MaybePromise<void>;
42
- run(options: InitxCtx, ...others: string[]): HandlerInfo[];
72
+ abstract handle(options: InitxContext<TConfig>, ...others: string[]): MaybePromise<void>;
73
+ defaultConfig?: TConfig;
74
+ run(context: InitxContext<TConfig>, ...others: string[]): HandlerInfo[];
43
75
  private matchBaseMatchers;
44
76
  private matchArrayBaseMatchers;
45
77
  private matchTypeMatchers;
@@ -47,19 +79,10 @@ declare abstract class InitxHandler {
47
79
  private isArrayBaseMatchers;
48
80
  private isObject;
49
81
  private isPassed;
82
+ private executeHandle;
50
83
  }
51
84
 
52
- interface PackageInfo {
53
- name: string;
54
- version: string;
55
- description: string;
56
- author: string;
57
- homepage?: string;
58
- }
59
- interface InitxPlugin {
60
- packageInfo: PackageInfo;
61
- handler: InitxHandler;
62
- }
63
- declare function loadPlugins(): Promise<InitxPlugin[]>;
85
+ declare function createStore(root: string, defaultStore?: Record<string, any>): any;
86
+ declare function writeStore(root: string): void;
64
87
 
65
- export { type HandlerInfo, type InitxCtx, InitxHandler, type InitxPlugin, type PackageInfo, loadPlugins };
88
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type MatchedPlugin, type PackageInfo, createStore, loadPlugins, matchPlugins, writeStore };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,21 @@
1
+ interface PackageInfo {
2
+ root: string;
3
+ name: string;
4
+ version: string;
5
+ description: string;
6
+ author: string;
7
+ homepage?: string;
8
+ }
9
+ interface InitxPluginInfo {
10
+ packageInfo: PackageInfo;
11
+ instance: InitxPlugin;
12
+ }
13
+ type MatchedPlugin = HandlerInfo & {
14
+ packageInfo: PackageInfo;
15
+ };
16
+ declare function loadPlugins(): Promise<InitxPluginInfo[]>;
17
+ declare function matchPlugins(plugins: InitxPluginInfo[], { key, cliOptions }: InitxBaseContext, ...others: string[]): MatchedPlugin[];
18
+
1
19
  type MaybeArray<T> = T | T[];
2
20
  type MaybePromise<T> = T | Promise<T>;
3
21
  interface BaseMatchers {
@@ -11,11 +29,12 @@ interface BaseMatchers {
11
29
  }
12
30
  type TypeMatchers = Record<string, BaseMatchers>;
13
31
  type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
32
+ type PluginConfig = Record<string, any>;
14
33
  interface HandlerInfo {
15
34
  handler: () => MaybePromise<void>;
16
35
  description: string;
17
36
  }
18
- interface InitxCtx {
37
+ interface InitxBaseContext {
19
38
  /**
20
39
  * Matching string
21
40
  *
@@ -36,10 +55,23 @@ interface InitxCtx {
36
55
  */
37
56
  optionsList: string[];
38
57
  }
39
- declare abstract class InitxHandler {
58
+ interface InitxContext<TConfig extends PluginConfig = PluginConfig> extends InitxBaseContext {
59
+ /**
60
+ * Store
61
+ *
62
+ * Store data in memory, and write to disk when the program exits
63
+ */
64
+ store: TConfig;
65
+ /**
66
+ * Package info
67
+ */
68
+ packageInfo: PackageInfo;
69
+ }
70
+ declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig> {
40
71
  abstract matchers: Matchers;
41
- abstract handle(options: InitxCtx, ...others: string[]): MaybePromise<void>;
42
- run(options: InitxCtx, ...others: string[]): HandlerInfo[];
72
+ abstract handle(options: InitxContext<TConfig>, ...others: string[]): MaybePromise<void>;
73
+ defaultConfig?: TConfig;
74
+ run(context: InitxContext<TConfig>, ...others: string[]): HandlerInfo[];
43
75
  private matchBaseMatchers;
44
76
  private matchArrayBaseMatchers;
45
77
  private matchTypeMatchers;
@@ -47,19 +79,10 @@ declare abstract class InitxHandler {
47
79
  private isArrayBaseMatchers;
48
80
  private isObject;
49
81
  private isPassed;
82
+ private executeHandle;
50
83
  }
51
84
 
52
- interface PackageInfo {
53
- name: string;
54
- version: string;
55
- description: string;
56
- author: string;
57
- homepage?: string;
58
- }
59
- interface InitxPlugin {
60
- packageInfo: PackageInfo;
61
- handler: InitxHandler;
62
- }
63
- declare function loadPlugins(): Promise<InitxPlugin[]>;
85
+ declare function createStore(root: string, defaultStore?: Record<string, any>): any;
86
+ declare function writeStore(root: string): void;
64
87
 
65
- export { type HandlerInfo, type InitxCtx, InitxHandler, type InitxPlugin, type PackageInfo, loadPlugins };
88
+ export { type HandlerInfo, type InitxBaseContext, type InitxContext, InitxPlugin, type InitxPluginInfo, type MatchedPlugin, type PackageInfo, createStore, loadPlugins, matchPlugins, writeStore };
package/dist/index.mjs CHANGED
@@ -1,58 +1,131 @@
1
1
  import path from 'node:path';
2
2
  import fs from 'fs-extra';
3
+ import { defu } from 'defu';
3
4
  import { c } from '@initx-plugin/utils';
4
5
 
5
- class InitxHandler {
6
- run(options, ...others) {
6
+ const STORE_FILE_NAME = "store.json";
7
+ const stores = /* @__PURE__ */ new Map();
8
+ function createStore(root, defaultStore = {}) {
9
+ const storePath = path.resolve(root, STORE_FILE_NAME);
10
+ const generateResult = (resultData) => {
11
+ writeJson(storePath, resultData);
12
+ return useProxy(root, resultData);
13
+ };
14
+ if (!fs.existsSync(storePath)) {
15
+ return generateResult(defaultStore);
16
+ }
17
+ let json;
18
+ try {
19
+ const fileJson = fs.readJsonSync(storePath);
20
+ json = defu(fileJson, defaultStore);
21
+ } catch (e) {
22
+ json = defaultStore;
23
+ }
24
+ return generateResult(json);
25
+ }
26
+ function writeStore(root) {
27
+ const storePath = path.resolve(root, STORE_FILE_NAME);
28
+ if (!stores.has(root)) {
29
+ return;
30
+ }
31
+ const store = stores.get(root);
32
+ if (store.rewrited) {
33
+ fs.writeJsonSync(storePath, store.data || {});
34
+ }
35
+ }
36
+ function writeJson(path2, data) {
37
+ fs.writeJsonSync(path2, data, {
38
+ spaces: 2
39
+ });
40
+ }
41
+ function useProxy(root, obj = {}) {
42
+ if (!stores.has(root)) {
43
+ stores.set(root, {
44
+ rewrited: false
45
+ });
46
+ }
47
+ const isPlainObject = (value) => {
48
+ return Object.prototype.toString.call(value) === "[object Object]";
49
+ };
50
+ const createDeepProxy = (target) => {
51
+ return new Proxy(target, {
52
+ get(target2, key) {
53
+ const value = Reflect.get(target2, key);
54
+ if (isPlainObject(value)) {
55
+ return createDeepProxy(value);
56
+ }
57
+ return value;
58
+ },
59
+ set(target2, key, value) {
60
+ stores.get(root).rewrited = true;
61
+ return Reflect.set(target2, key, value);
62
+ }
63
+ });
64
+ };
65
+ const store = createDeepProxy(obj);
66
+ stores.get(root).data = store;
67
+ return store;
68
+ }
69
+
70
+ var __defProp = Object.defineProperty;
71
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
72
+ var __publicField = (obj, key, value) => {
73
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
74
+ return value;
75
+ };
76
+ class InitxPlugin {
77
+ constructor() {
78
+ __publicField(this, "defaultConfig");
79
+ }
80
+ run(context, ...others) {
7
81
  if (this.isBaseMatchers(this.matchers)) {
8
- return this.matchBaseMatchers(this.matchers, options, ...others);
82
+ return this.matchBaseMatchers(this.matchers, context, ...others);
9
83
  }
10
84
  if (this.isArrayBaseMatchers(this.matchers)) {
11
- return this.matchArrayBaseMatchers(this.matchers, options, ...others);
85
+ return this.matchArrayBaseMatchers(this.matchers, context, ...others);
12
86
  }
13
87
  if (this.isObject(this.matchers)) {
14
- return this.matchTypeMatchers(this.matchers, options, ...others);
88
+ return this.matchTypeMatchers(this.matchers, context, ...others);
15
89
  }
16
90
  return [];
17
91
  }
18
92
  // BaseMatchers
19
- matchBaseMatchers(matchers, options, ...others) {
20
- if (!this.isPassed(matchers.matching, options.key)) {
93
+ matchBaseMatchers(matchers, context, ...others) {
94
+ if (!this.isPassed(matchers.matching, context.key)) {
21
95
  return [];
22
96
  }
23
97
  return [
24
98
  {
25
- handler: () => this.handle(options, ...others),
99
+ handler: () => this.executeHandle(context, ...others),
26
100
  description: matchers.description
27
101
  }
28
102
  ];
29
103
  }
30
- matchArrayBaseMatchers(matchers, options, ...others) {
104
+ matchArrayBaseMatchers(matchers, context, ...others) {
31
105
  const handlers = [];
32
106
  for (let i = 0; i < matchers.length; i++) {
33
107
  const matcher = matchers[i];
34
- const isPassed = this.isPassed(matcher.matching, options.key);
108
+ const isPassed = this.isPassed(matcher.matching, context.key);
35
109
  if (isPassed) {
36
110
  handlers.push({
37
- handler: () => this.handle(options, ...others),
111
+ handler: () => this.executeHandle(context, ...others),
38
112
  description: matcher.description
39
113
  });
40
114
  }
41
115
  }
42
116
  return handlers;
43
117
  }
44
- matchTypeMatchers(matchers, options, ...others) {
118
+ matchTypeMatchers(matchers, context, ...others) {
45
119
  const handlers = [];
46
120
  const keys = Object.keys(matchers);
47
121
  for (let i = 0; i < keys.length; i++) {
48
122
  const matcher = matchers[keys[i]];
49
- const isPassed = this.isPassed(matcher.matching, options.key);
123
+ const isPassed = this.isPassed(matcher.matching, context.key);
50
124
  if (isPassed) {
51
125
  handlers.push({
52
- handler: () => this.handle(options, keys[i], ...others),
126
+ handler: () => this.executeHandle(context, keys[i], ...others),
53
127
  description: matcher.description
54
128
  });
55
- break;
56
129
  }
57
130
  }
58
131
  return handlers;
@@ -76,6 +149,10 @@ class InitxHandler {
76
149
  return test.test(key);
77
150
  });
78
151
  }
152
+ async executeHandle(context, ...others) {
153
+ await this.handle(context, ...others);
154
+ writeStore(context.packageInfo.root);
155
+ }
79
156
  }
80
157
 
81
158
  async function loadPlugins() {
@@ -84,17 +161,23 @@ async function loadPlugins() {
84
161
  const communityPlugins = fs.readdirSync(nodeModules);
85
162
  const officialPluginPath = path.join(nodeModules, "@initx-plugin");
86
163
  const officialPlugins = fs.existsSync(officialPluginPath) ? fs.readdirSync(officialPluginPath).map((name) => `@initx-plugin/${name}`) : [];
164
+ const regexps = {
165
+ plugin: /^(?:@initx-plugin\/|initx-plugin-)/,
166
+ exclude: /@initx-plugin\/(?:core|utils)$/
167
+ };
87
168
  const pluginsName = [
88
169
  ...officialPlugins,
89
170
  ...communityPlugins
90
171
  ].filter(
91
- (name) => /^(?:@initx-plugin\/|initx-plugin-)/.test(name) && !/@initx-plugin\/(?:core|utils)$/.test(name)
172
+ (name) => regexps.plugin.test(name) && !regexps.exclude.test(name)
92
173
  );
93
174
  const x = await import('importx');
94
175
  return Promise.all(pluginsName.map(async (dirname) => {
95
- const InitxHandlerClass = await x.import(path.join(nodeModules, dirname), import.meta.url).then((x2) => x2.default);
176
+ const pluginRoot = path.join(nodeModules, dirname);
177
+ const InitxPluginClass = await x.import(pluginRoot, import.meta.url).then((x2) => x2.default);
96
178
  const packageAll = JSON.parse(fs.readFileSync(path.join(nodeModules, dirname, "package.json"), "utf-8"));
97
179
  const packageInfo = {
180
+ root: pluginRoot,
98
181
  name: packageAll.name,
99
182
  version: packageAll.version,
100
183
  description: packageAll.description,
@@ -103,9 +186,29 @@ async function loadPlugins() {
103
186
  };
104
187
  return {
105
188
  packageInfo,
106
- handler: new InitxHandlerClass()
189
+ instance: new InitxPluginClass()
107
190
  };
108
191
  }));
109
192
  }
193
+ function matchPlugins(plugins, { key, cliOptions }, ...others) {
194
+ const matchedHandlers = [];
195
+ for (const plugin of plugins) {
196
+ const { instance, packageInfo } = plugin;
197
+ const store = createStore(packageInfo.root, instance.defaultConfig);
198
+ const matched = instance.run({
199
+ key,
200
+ store,
201
+ cliOptions,
202
+ packageInfo,
203
+ optionsList: Object.keys(cliOptions).filter((key2) => cliOptions[key2] === true).map((key2) => `--${key2}`)
204
+ }, ...others);
205
+ matchedHandlers.push(...matched.map((item) => ({
206
+ handler: item.handler,
207
+ description: item.description,
208
+ packageInfo
209
+ })));
210
+ }
211
+ return matchedHandlers;
212
+ }
110
213
 
111
- export { InitxHandler, loadPlugins };
214
+ export { InitxPlugin, createStore, 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.17",
4
+ "version": "0.0.19",
5
5
  "description": "core module for initx plugins",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/initx-collective/initx#readme",
@@ -22,9 +22,10 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
+ "defu": "^6.1.4",
25
26
  "fs-extra": "^11.2.0",
26
27
  "importx": "^0.5.0",
27
- "@initx-plugin/utils": "0.0.17"
28
+ "@initx-plugin/utils": "0.0.19"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/fs-extra": "^11.0.4"