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