@yongdall/plugins 0.2.0 → 0.3.2
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/index.d.mts +2 -1
- package/index.mjs +20 -5
- package/index.mjs.map +1 -1
- package/package.json +4 -4
package/index.d.mts
CHANGED
|
@@ -12,9 +12,10 @@ declare function loadPluginProfiles<T extends object>(name: string, scriptOnly?:
|
|
|
12
12
|
* @returns {AsyncIterableIterator<[plugin: string, config: T]>}
|
|
13
13
|
*/
|
|
14
14
|
declare function loadPluginScripts<T extends object>(name: string): AsyncIterableIterator<[plugin: string, config: T]>;
|
|
15
|
-
/** @type {Record<string, {root: string; development: boolean}>} */
|
|
15
|
+
/** @type {Record<string, {root: string; source: string; development: boolean}>} */
|
|
16
16
|
declare const pluginRoots: Record<string, {
|
|
17
17
|
root: string;
|
|
18
|
+
source: string;
|
|
18
19
|
development: boolean;
|
|
19
20
|
}>;
|
|
20
21
|
//#endregion
|
package/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as pathFn from "node:path";
|
|
2
2
|
import * as fsPromises from "node:fs/promises";
|
|
3
3
|
import { pluginIdRegex } from "@yongdall/common";
|
|
4
|
-
import
|
|
4
|
+
import { loadCfgWithPath, loadProjectConfiguration, loadProjectConfigurationPath } from "@yongdall/configuration";
|
|
5
5
|
import loadConfiguration, { loadConfigurationWithPath } from "@yongdall/load-configuration";
|
|
6
6
|
|
|
7
7
|
//#region cli/plugins/index.mjs
|
|
8
|
-
/** @type {Record<string, {root: string; development: boolean}>} */
|
|
8
|
+
/** @type {Record<string, {root: string; source: string; development: boolean}>} */
|
|
9
9
|
const pluginRoots = Object.create(null);
|
|
10
10
|
/**
|
|
11
11
|
*
|
|
@@ -28,7 +28,7 @@ async function findNodeModule(root, name) {
|
|
|
28
28
|
*/
|
|
29
29
|
async function getPlugins() {
|
|
30
30
|
const result = await loadCfgWithPath("plugins").catch(() => {});
|
|
31
|
-
const [path, pluginList] = result && result[1] ? result : [
|
|
31
|
+
const [path, pluginList] = result && result[1] ? result : [await loadProjectConfigurationPath(), await loadProjectConfiguration().then((v) => v.plugins)];
|
|
32
32
|
if (!pluginList) return [];
|
|
33
33
|
const basePath = pathFn.dirname(path);
|
|
34
34
|
if (Array.isArray(pluginList)) return pluginList.map((a) => [
|
|
@@ -47,10 +47,25 @@ for (const [plugin, define, basePath] of await getPlugins()) {
|
|
|
47
47
|
if (!pluginIdRegex.exec(plugin)) throw new Error(`无效的应用名称:${plugin}`);
|
|
48
48
|
const path = typeof define === "object" && define ? define.path : define;
|
|
49
49
|
const development = typeof define === "object" && define ? define.development : false;
|
|
50
|
-
const
|
|
51
|
-
|
|
50
|
+
const url = typeof path === "string" ? path : "";
|
|
51
|
+
const index = url.indexOf("#");
|
|
52
|
+
const packagePath = index >= 0 ? url.slice(0, index) : url;
|
|
53
|
+
const packageName = pluginIdRegex.test(packagePath) ? packagePath : "";
|
|
54
|
+
const source = !packageName && packagePath ? pathFn.resolve(basePath, packagePath) : await findNodeModule(basePath, packageName || plugin);
|
|
55
|
+
if (!source) throw new Error(`找不到应用:${plugin}`);
|
|
56
|
+
let pluginPath = source;
|
|
57
|
+
if (index >= 0) {
|
|
58
|
+
const dirOptional = url[index + 1] === "?";
|
|
59
|
+
const dir = url.slice(dirOptional ? index + 2 : index + 1);
|
|
60
|
+
if (dir) {
|
|
61
|
+
const pluginDir = pathFn.join(pluginPath, dir);
|
|
62
|
+
if (await fsPromises.stat(pluginDir).then((s) => s.isDirectory(), () => false)) pluginPath = pluginDir;
|
|
63
|
+
else if (!dirOptional) throw new Error(`找不到应用 ${plugin} 的目录: ${dir}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
52
66
|
pluginRoots[plugin] = {
|
|
53
67
|
root: pluginPath,
|
|
68
|
+
source,
|
|
54
69
|
development: Boolean(development)
|
|
55
70
|
};
|
|
56
71
|
}
|
package/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../cli/plugins/index.mjs"],"sourcesContent":["import * as pathFn from 'node:path';\nimport * as fsPromises from 'node:fs/promises';\nimport { pluginIdRegex } from '@yongdall/common';\nimport
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../cli/plugins/index.mjs"],"sourcesContent":["import * as pathFn from 'node:path';\nimport * as fsPromises from 'node:fs/promises';\nimport { pluginIdRegex } from '@yongdall/common';\nimport { loadCfgWithPath, loadProjectConfiguration, loadProjectConfigurationPath } from '@yongdall/configuration';\nimport loadConfiguration, { loadConfigurationWithPath } from '@yongdall/load-configuration';\n\n/** @type {Record<string, {root: string; source: string; development: boolean}>} */\nconst pluginRoots = Object.create(null);\n\n/**\n * \n * @param {string} root \n * @param {string} name \n */\nasync function findNodeModule(root, name) {\n\tfor (let basepath = root; ;) {\n\t\tconst path = pathFn.resolve(basepath, 'node_modules', name);\n\t\tconst packageJson = await fsPromises.readFile(pathFn.resolve(path, 'package.json'), 'utf-8').then(v => JSON.parse(v)).catch(() => null);\n\t\tconst version = packageJson?.version;\n\t\tif (version && typeof version === 'string') {\n\t\t\treturn path;\n\t\t}\n\t\tconst parent = pathFn.dirname(basepath);\n\t\tif (parent === basepath) { break; }\n\t\tbasepath = parent;\n\t}\n\treturn null;\n}\n/**\n * @returns {Promise<[string, string | boolean | {path?: string; development?: boolean}, string][]>}\n */\nasync function getPlugins() {\n\tconst result = await loadCfgWithPath('plugins').catch(() => { });\n\tconst [path, pluginList] = result && result[1] ? result : [\n\t\tawait loadProjectConfigurationPath(),\n\t\tawait loadProjectConfiguration().then(v => v.plugins),\n\t];\n\tif (!pluginList) { return []; }\n\tconst basePath = pathFn.dirname(path);\n\tif (Array.isArray(pluginList)) {\n\t\treturn pluginList.map(a => [a, true, basePath]);\n\t}\n\treturn Object.entries(pluginList).map(([a, b]) => [a, b, basePath]);\n}\n\nfor (const [plugin, define, basePath] of await getPlugins()) {\n\tif (!define) { continue; }\n\tconst r = pluginIdRegex.exec(plugin);\n\tif (!r) { throw new Error(`无效的应用名称:${plugin}`); }\n\tconst path = typeof define === 'object' && define ? define.path : define;\n\tconst development = typeof define === 'object' && define ? define.development : false;\n\n\tconst url = typeof path === 'string' ? path : '';\n\tconst index = url.indexOf('#');\n\tconst packagePath = index >= 0 ? url.slice(0, index) : url;\n\tconst packageName = pluginIdRegex.test(packagePath) ? packagePath : '';\n\n\tconst source = !packageName && packagePath\n\t\t? pathFn.resolve(basePath, packagePath)\n\t\t: await findNodeModule(basePath, packageName || plugin);\n\tif (!source) { throw new Error(`找不到应用:${plugin}`); }\n\tlet pluginPath = source;\n\n\tif (index >= 0) {\n\t\tconst dirOptional = url[index + 1] === '?';\n\t\tconst dir = url.slice(dirOptional ? index + 2 : index + 1);\n\t\tif (dir) {\n\t\t\tconst pluginDir = pathFn.join(pluginPath, dir);\n\t\t\tif (await fsPromises.stat(pluginDir).then(s => s.isDirectory(), () => false)) {\n\t\t\t\tpluginPath = pluginDir;\n\t\t\t} else if (!dirOptional) {\n\t\t\t\tthrow new Error(`找不到应用 ${plugin} 的目录: ${dir}`);\n\t\t\t}\n\t\t}\n\n\t}\n\tpluginRoots[plugin] = { root: pluginPath, source, development: Boolean(development) };\n}\n\nexport default pluginRoots;\n\n/** \n * @template {object} T\n * @param {string} name\n * @param {boolean} [scriptOnly] \n * @returns {AsyncIterableIterator<[plugin: string, config: T | null, path: string]>}\n */\nexport async function* loadPluginProfiles(name, scriptOnly) {\n\tfor (const [plugin, { root }] of Object.entries(pluginRoots)) {\n\t\t/** @type {[string, T | null] | null} */\n\t\tconst result = await loadConfigurationWithPath(pathFn.resolve(root, `${name}.yongdall`), scriptOnly)\n\t\t\t|| await loadConfigurationWithPath(pathFn.resolve(root, `yongdall/${name}`), scriptOnly)\n\t\t\t|| await loadConfigurationWithPath(pathFn.resolve(root, `${name}/yongdall`), scriptOnly);\n\t\tif (!result) { continue; }\n\t\tconst [path, config] = result;\n\t\tyield [plugin, config, path];\n\t}\n}\n\n/** \n * @template {object} T\n * @param {string} name\n * @returns {AsyncIterableIterator<[plugin: string, config: T]>}\n */\nexport async function* loadPluginScripts(name) {\n\tfor (const [plugin, { root }] of Object.entries(pluginRoots)) {\n\t\t/** @type {T | null} */\n\t\tconst result = await loadConfiguration(pathFn.resolve(root, `${name}.yongdall`), true)\n\t\t\t|| await loadConfiguration(pathFn.resolve(root, `yongdall/${name}`), true)\n\t\t\t|| await loadConfiguration(pathFn.resolve(root, `${name}/yongdall`), true);\n\t\tif (result === null) { continue; }\n\t\tyield [plugin, result];\n\t}\n}\n"],"mappings":";;;;;;;;AAOA,MAAM,cAAc,OAAO,OAAO,KAAK;;;;;;AAOvC,eAAe,eAAe,MAAM,MAAM;AACzC,MAAK,IAAI,WAAW,QAAS;EAC5B,MAAM,OAAO,OAAO,QAAQ,UAAU,gBAAgB,KAAK;EAE3D,MAAM,WADc,MAAM,WAAW,SAAS,OAAO,QAAQ,MAAM,eAAe,EAAE,QAAQ,CAAC,MAAK,MAAK,KAAK,MAAM,EAAE,CAAC,CAAC,YAAY,KAAK,GAC1G;AAC7B,MAAI,WAAW,OAAO,YAAY,SACjC,QAAO;EAER,MAAM,SAAS,OAAO,QAAQ,SAAS;AACvC,MAAI,WAAW,SAAY;AAC3B,aAAW;;AAEZ,QAAO;;;;;AAKR,eAAe,aAAa;CAC3B,MAAM,SAAS,MAAM,gBAAgB,UAAU,CAAC,YAAY,GAAI;CAChE,MAAM,CAAC,MAAM,cAAc,UAAU,OAAO,KAAK,SAAS,CACzD,MAAM,8BAA8B,EACpC,MAAM,0BAA0B,CAAC,MAAK,MAAK,EAAE,QAAQ,CACrD;AACD,KAAI,CAAC,WAAc,QAAO,EAAE;CAC5B,MAAM,WAAW,OAAO,QAAQ,KAAK;AACrC,KAAI,MAAM,QAAQ,WAAW,CAC5B,QAAO,WAAW,KAAI,MAAK;EAAC;EAAG;EAAM;EAAS,CAAC;AAEhD,QAAO,OAAO,QAAQ,WAAW,CAAC,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7D,MAAM,SAAS,MAAM,0BAA0B,OAAO,QAAQ,MAAM,GAAG,KAAK,WAAW,EAAE,WAAW,IAChG,MAAM,0BAA0B,OAAO,QAAQ,MAAM,YAAY,OAAO,EAAE,WAAW,IACrF,MAAM,0BAA0B,OAAO,QAAQ,MAAM,GAAG,KAAK,WAAW,EAAE,WAAW;AACzF,MAAI,CAAC,OAAU;EACf,MAAM,CAAC,MAAM,UAAU;AACvB,QAAM;GAAC;GAAQ;GAAQ;GAAK;;;;;;;;AAS9B,gBAAuB,kBAAkB,MAAM;AAC9C,MAAK,MAAM,CAAC,QAAQ,EAAE,WAAW,OAAO,QAAQ,YAAY,EAAE;;EAE7D,MAAM,SAAS,MAAM,kBAAkB,OAAO,QAAQ,MAAM,GAAG,KAAK,WAAW,EAAE,KAAK,IAClF,MAAM,kBAAkB,OAAO,QAAQ,MAAM,YAAY,OAAO,EAAE,KAAK,IACvE,MAAM,kBAAkB,OAAO,QAAQ,MAAM,GAAG,KAAK,WAAW,EAAE,KAAK;AAC3E,MAAI,WAAW,KAAQ;AACvB,QAAM,CAAC,QAAQ,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yongdall/plugins",
|
|
3
|
-
"version": "0.2
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"plugins.yongdall": "./bin.mjs"
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"#index": "./index.mjs"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@yongdall/common": "^0.
|
|
14
|
-
"@yongdall/configuration": "^0.
|
|
15
|
-
"@yongdall/load-configuration": "^0.
|
|
13
|
+
"@yongdall/common": "^0.3.0",
|
|
14
|
+
"@yongdall/configuration": "^0.3.0",
|
|
15
|
+
"@yongdall/load-configuration": "^0.3.0",
|
|
16
16
|
"yaml": "^2.5.0"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|