@yongdall/plugins 0.1.0
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/bin.mjs +8 -0
- package/bin.mjs.map +1 -0
- package/index.d.mts +8 -0
- package/index.mjs +49 -0
- package/index.mjs.map +1 -0
- package/package.json +20 -0
package/bin.mjs
ADDED
package/bin.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.mjs","names":[],"sources":["../../cli/plugins/bin.mjs"],"sourcesContent":["#!/usr/bin/env node\nimport pluginRoots from '#index';\nfor (const [plugin, {root: path}] of Object.entries(pluginRoots)) {\n\tconsole.log(`${plugin}: ${path}`);\n}\n"],"mappings":";;;;AAEA,KAAK,MAAM,CAAC,QAAQ,EAAC,MAAM,WAAU,OAAO,QAAQ,YAAY,CAC/D,SAAQ,IAAI,GAAG,OAAO,IAAI,OAAO"}
|
package/index.d.mts
ADDED
package/index.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as pathFn from "node:path";
|
|
2
|
+
import * as fsPromises from "node:fs/promises";
|
|
3
|
+
import { pluginIdRegex } from "@yongdall/common";
|
|
4
|
+
import configuration, { configRoot, loadCfg } from "@yongdall/configuration";
|
|
5
|
+
|
|
6
|
+
//#region cli/plugins/index.mjs
|
|
7
|
+
/** @type {Record<string, {root: string; development: boolean}>} */
|
|
8
|
+
const pluginRoots = Object.create(null);
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param {string} root
|
|
12
|
+
* @param {string} name
|
|
13
|
+
*/
|
|
14
|
+
async function findNodeModule(root, name) {
|
|
15
|
+
for (let basepath = root;;) {
|
|
16
|
+
const path = pathFn.resolve(basepath, "node_modules", name);
|
|
17
|
+
const version = (await fsPromises.readFile(pathFn.resolve(path, "package.json"), "utf-8").then((v) => JSON.parse(v)).catch(() => null))?.version;
|
|
18
|
+
if (version && typeof version === "string") return path;
|
|
19
|
+
const parent = pathFn.dirname(basepath);
|
|
20
|
+
if (parent === basepath) break;
|
|
21
|
+
basepath = parent;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @returns {Promise<[string, string | true | {path?: string; development?: boolean}][]>}
|
|
27
|
+
*/
|
|
28
|
+
async function getPlugins() {
|
|
29
|
+
const pluginList = await loadCfg("plugins").catch(() => {}) || configuration.plugins;
|
|
30
|
+
if (!pluginList) return [];
|
|
31
|
+
if (Array.isArray(pluginList)) return pluginList.map((a) => [a, true]);
|
|
32
|
+
return Object.entries(pluginList);
|
|
33
|
+
}
|
|
34
|
+
for (const [plugin, define] of await getPlugins()) {
|
|
35
|
+
if (!pluginIdRegex.exec(plugin)) throw new Error(`无效的应用名称:${plugin}`);
|
|
36
|
+
const path = typeof define === "object" && define ? define.path : define;
|
|
37
|
+
const development = typeof define === "object" && define ? define.development : false;
|
|
38
|
+
const pluginPath = path && typeof path === "string" ? pathFn.resolve(configRoot, path) : await findNodeModule(configRoot, plugin);
|
|
39
|
+
if (!pluginPath) throw new Error(`找不到应用:${plugin}`);
|
|
40
|
+
pluginRoots[plugin] = {
|
|
41
|
+
root: pluginPath,
|
|
42
|
+
development: Boolean(development)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
var plugins_default = pluginRoots;
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { plugins_default as default };
|
|
49
|
+
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
ADDED
|
@@ -0,0 +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 configuration, { loadCfg, configRoot } from '@yongdall/configuration';\n\n/** @type {Record<string, {root: 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 | true | {path?: string; development?: boolean}][]>}\n */\nasync function getPlugins() {\n\tconst pluginList = await loadCfg('plugins').catch(() => { }) || configuration.plugins;\n\tif (!pluginList) { return []; }\n\tif (Array.isArray(pluginList)) {\n\t\treturn pluginList.map(a => [a, true]);\n\t}\n\treturn Object.entries(pluginList);\n}\n\nfor (const [plugin, define] of await getPlugins()) {\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\tconst pluginPath = path && typeof path === 'string'\n\t\t? pathFn.resolve(configRoot, path)\n\t\t: await findNodeModule(configRoot, plugin);\n\tif (!pluginPath) { throw new Error(`找不到应用:${plugin}`); }\n\tpluginRoots[plugin] = {root: pluginPath, development: Boolean(development)};\n}\n\nexport default pluginRoots;\n"],"mappings":";;;;;;;AAMA,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,aAAa,MAAM,QAAQ,UAAU,CAAC,YAAY,GAAI,IAAI,cAAc;AAC9E,KAAI,CAAC,WAAc,QAAO,EAAE;AAC5B,KAAI,MAAM,QAAQ,WAAW,CAC5B,QAAO,WAAW,KAAI,MAAK,CAAC,GAAG,KAAK,CAAC;AAEtC,QAAO,OAAO,QAAQ,WAAW;;AAGlC,KAAK,MAAM,CAAC,QAAQ,WAAW,MAAM,YAAY,EAAE;AAElD,KAAI,CADM,cAAc,KAAK,OAAO,CAC1B,OAAM,IAAI,MAAM,WAAW,SAAS;CAC9C,MAAM,OAAO,OAAO,WAAW,YAAY,SAAS,OAAO,OAAO;CAClE,MAAM,cAAc,OAAO,WAAW,YAAY,SAAS,OAAO,cAAc;CAChF,MAAM,aAAa,QAAQ,OAAO,SAAS,WACxC,OAAO,QAAQ,YAAY,KAAK,GAChC,MAAM,eAAe,YAAY,OAAO;AAC3C,KAAI,CAAC,WAAc,OAAM,IAAI,MAAM,SAAS,SAAS;AACrD,aAAY,UAAU;EAAC,MAAM;EAAY,aAAa,QAAQ,YAAY;EAAC;;AAG5E,sBAAe"}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yongdall/plugins",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"bin": {
|
|
6
|
+
"plugins.yongdall": "./bin.mjs"
|
|
7
|
+
},
|
|
8
|
+
"main": "./index.mjs",
|
|
9
|
+
"imports": {
|
|
10
|
+
"#index": "./index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@yongdall/common": "^0.1.0",
|
|
14
|
+
"@yongdall/configuration": "^0.1.0",
|
|
15
|
+
"yaml": "^2.5.0"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./index.mjs"
|
|
19
|
+
}
|
|
20
|
+
}
|