@initx-plugin/core 0.0.19 → 0.0.21

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
@@ -29,7 +29,7 @@ interface BaseMatchers {
29
29
  }
30
30
  type TypeMatchers = Record<string, BaseMatchers>;
31
31
  type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
32
- type PluginConfig = Record<string, any>;
32
+ type PluginStore = Record<string, any>;
33
33
  interface HandlerInfo {
34
34
  handler: () => MaybePromise<void>;
35
35
  description: string;
@@ -55,23 +55,23 @@ interface InitxBaseContext {
55
55
  */
56
56
  optionsList: string[];
57
57
  }
58
- interface InitxContext<TConfig extends PluginConfig = PluginConfig> extends InitxBaseContext {
58
+ interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxBaseContext {
59
59
  /**
60
60
  * Store
61
61
  *
62
62
  * Store data in memory, and write to disk when the program exits
63
63
  */
64
- store: TConfig;
64
+ store: TStore;
65
65
  /**
66
66
  * Package info
67
67
  */
68
68
  packageInfo: PackageInfo;
69
69
  }
70
- declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig> {
70
+ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
71
71
  abstract matchers: Matchers;
72
- abstract handle(options: InitxContext<TConfig>, ...others: string[]): MaybePromise<void>;
73
- defaultConfig?: TConfig;
74
- run(context: InitxContext<TConfig>, ...others: string[]): HandlerInfo[];
72
+ abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
73
+ defaultStore?: TStore;
74
+ run(context: InitxContext<TStore>, ...others: string[]): HandlerInfo[];
75
75
  private matchBaseMatchers;
76
76
  private matchArrayBaseMatchers;
77
77
  private matchTypeMatchers;
@@ -82,7 +82,7 @@ declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig>
82
82
  private executeHandle;
83
83
  }
84
84
 
85
- declare function createStore(root: string, defaultStore?: Record<string, any>): any;
86
- declare function writeStore(root: string): void;
85
+ declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
86
+ declare function writeStore({ name }: PackageInfo): void;
87
87
 
88
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
@@ -29,7 +29,7 @@ interface BaseMatchers {
29
29
  }
30
30
  type TypeMatchers = Record<string, BaseMatchers>;
31
31
  type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
32
- type PluginConfig = Record<string, any>;
32
+ type PluginStore = Record<string, any>;
33
33
  interface HandlerInfo {
34
34
  handler: () => MaybePromise<void>;
35
35
  description: string;
@@ -55,23 +55,23 @@ interface InitxBaseContext {
55
55
  */
56
56
  optionsList: string[];
57
57
  }
58
- interface InitxContext<TConfig extends PluginConfig = PluginConfig> extends InitxBaseContext {
58
+ interface InitxContext<TStore extends PluginStore = PluginStore> extends InitxBaseContext {
59
59
  /**
60
60
  * Store
61
61
  *
62
62
  * Store data in memory, and write to disk when the program exits
63
63
  */
64
- store: TConfig;
64
+ store: TStore;
65
65
  /**
66
66
  * Package info
67
67
  */
68
68
  packageInfo: PackageInfo;
69
69
  }
70
- declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig> {
70
+ declare abstract class InitxPlugin<TStore extends PluginStore = PluginStore> {
71
71
  abstract matchers: Matchers;
72
- abstract handle(options: InitxContext<TConfig>, ...others: string[]): MaybePromise<void>;
73
- defaultConfig?: TConfig;
74
- run(context: InitxContext<TConfig>, ...others: string[]): HandlerInfo[];
72
+ abstract handle(options: InitxContext<TStore>, ...others: string[]): MaybePromise<void>;
73
+ defaultStore?: TStore;
74
+ run(context: InitxContext<TStore>, ...others: string[]): HandlerInfo[];
75
75
  private matchBaseMatchers;
76
76
  private matchArrayBaseMatchers;
77
77
  private matchTypeMatchers;
@@ -82,7 +82,7 @@ declare abstract class InitxPlugin<TConfig extends PluginConfig = PluginConfig>
82
82
  private executeHandle;
83
83
  }
84
84
 
85
- declare function createStore(root: string, defaultStore?: Record<string, any>): any;
86
- declare function writeStore(root: string): void;
85
+ declare function createStore({ name }: PackageInfo, defaultStore?: Record<string, any>): any;
86
+ declare function writeStore({ name }: PackageInfo): void;
87
87
 
88
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,15 +1,20 @@
1
1
  import path from 'node:path';
2
+ import { homedir } from 'node:os';
2
3
  import fs from 'fs-extra';
3
4
  import { defu } from 'defu';
4
5
  import { c } from '@initx-plugin/utils';
5
6
 
7
+ const INITX_DIR = path.resolve(homedir(), ".initx");
6
8
  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);
9
+ const REWRITED_FILE_NAME = ".rewrited";
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
+ function createStore({ name }, defaultStore = {}) {
13
+ fs.ensureDirSync(path.resolve(INITX_DIR, name));
14
+ const storePath = resolveStore(name);
10
15
  const generateResult = (resultData) => {
11
16
  writeJson(storePath, resultData);
12
- return useProxy(root, resultData);
17
+ return useProxy(name, resultData);
13
18
  };
14
19
  if (!fs.existsSync(storePath)) {
15
20
  return generateResult(defaultStore);
@@ -23,27 +28,21 @@ function createStore(root, defaultStore = {}) {
23
28
  }
24
29
  return generateResult(json);
25
30
  }
26
- function writeStore(root) {
27
- const storePath = path.resolve(root, STORE_FILE_NAME);
28
- if (!stores.has(root)) {
31
+ function writeStore({ name }) {
32
+ const rewritedPath = resolveRewrited(name);
33
+ if (!fs.existsSync(rewritedPath)) {
29
34
  return;
30
35
  }
31
- const store = stores.get(root);
32
- if (store.rewrited) {
33
- fs.writeJsonSync(storePath, store.data || {});
34
- }
36
+ const rewrited = fs.readJsonSync(rewritedPath);
37
+ writeJson(resolveStore(name), rewrited);
38
+ fs.removeSync(rewritedPath);
35
39
  }
36
40
  function writeJson(path2, data) {
37
41
  fs.writeJsonSync(path2, data, {
38
42
  spaces: 2
39
43
  });
40
44
  }
41
- function useProxy(root, obj = {}) {
42
- if (!stores.has(root)) {
43
- stores.set(root, {
44
- rewrited: false
45
- });
46
- }
45
+ function useProxy(name, obj = {}) {
47
46
  const isPlainObject = (value) => {
48
47
  return Object.prototype.toString.call(value) === "[object Object]";
49
48
  };
@@ -57,14 +56,13 @@ function useProxy(root, obj = {}) {
57
56
  return value;
58
57
  },
59
58
  set(target2, key, value) {
60
- stores.get(root).rewrited = true;
61
- return Reflect.set(target2, key, value);
59
+ const success = Reflect.set(target2, key, value);
60
+ fs.writeJsonSync(resolveRewrited(name), target2);
61
+ return success;
62
62
  }
63
63
  });
64
64
  };
65
- const store = createDeepProxy(obj);
66
- stores.get(root).data = store;
67
- return store;
65
+ return createDeepProxy(obj);
68
66
  }
69
67
 
70
68
  var __defProp = Object.defineProperty;
@@ -75,7 +73,7 @@ var __publicField = (obj, key, value) => {
75
73
  };
76
74
  class InitxPlugin {
77
75
  constructor() {
78
- __publicField(this, "defaultConfig");
76
+ __publicField(this, "defaultStore");
79
77
  }
80
78
  run(context, ...others) {
81
79
  if (this.isBaseMatchers(this.matchers)) {
@@ -151,7 +149,7 @@ class InitxPlugin {
151
149
  }
152
150
  async executeHandle(context, ...others) {
153
151
  await this.handle(context, ...others);
154
- writeStore(context.packageInfo.root);
152
+ writeStore(context.packageInfo);
155
153
  }
156
154
  }
157
155
 
@@ -194,7 +192,7 @@ function matchPlugins(plugins, { key, cliOptions }, ...others) {
194
192
  const matchedHandlers = [];
195
193
  for (const plugin of plugins) {
196
194
  const { instance, packageInfo } = plugin;
197
- const store = createStore(packageInfo.root, instance.defaultConfig);
195
+ const store = createStore(packageInfo, instance.defaultStore);
198
196
  const matched = instance.run({
199
197
  key,
200
198
  store,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@initx-plugin/core",
3
3
  "type": "module",
4
- "version": "0.0.19",
4
+ "version": "0.0.21",
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.19"
28
+ "@initx-plugin/utils": "0.0.21"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "^11.0.4"