@aomex/internal-file-import 0.0.27

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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @aomex/internal-file-import
2
+
3
+ ## 0.0.27
4
+
5
+ ### Patch Changes
6
+
7
+ - [`0776719`](https://github.com/aomex/aomex/commit/077671963401f1dafb5b03722899452d45df13fc) Thanks [@geekact](https://github.com/geekact)! - refactor: 文件搜索转移到新的包@aomex/internal-file-import
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 geekact
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ interface Options {
2
+ pattern: string[];
3
+ ignore?: string[];
4
+ dot?: boolean;
5
+ /**
6
+ * 忽略 **node_modules** 文件夹。默认值:`true`
7
+ */
8
+ ignoreNodeModules?: boolean;
9
+ /**
10
+ * 忽略所有 **.d.{ts,mts,cts}** 类型声明文件。默认值:`true`
11
+ */
12
+ ignoreDTS?: boolean;
13
+ }
14
+ type PathToFileOptions = string | string[] | Options | Options[];
15
+ declare const pathToFiles: (paths: PathToFileOptions) => Promise<string[]>;
16
+
17
+ declare const fileToModules: <T extends unknown>(files: string[], filter?: ((item?: T | undefined) => boolean) | undefined) => Promise<T[]>;
18
+
19
+ export { PathToFileOptions, fileToModules, pathToFiles };
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ // src/path-to-files.ts
2
+ import path from "node:path";
3
+ import { stat } from "node:fs/promises";
4
+ import { glob, hasMagic } from "glob";
5
+ var pathToFiles = async (paths) => {
6
+ const opts = normalizeSearchPath(paths);
7
+ const files = await Promise.all(
8
+ opts.map((opt) => {
9
+ const { dot, pattern: patterns } = opt;
10
+ const ignore = (opt.ignore || []).slice();
11
+ ignore.push("**/node_modules/**", "**/*.d.{ts,mts,cts}");
12
+ const options = {
13
+ nodir: true,
14
+ dot,
15
+ ignore,
16
+ absolute: true,
17
+ withFileTypes: false,
18
+ maxDepth: Infinity
19
+ };
20
+ return Promise.all(
21
+ patterns.map(async (pattern) => {
22
+ if (!path.isAbsolute(pattern)) {
23
+ pattern = path.posix.resolve(pattern);
24
+ }
25
+ pattern = pattern.replace(/\\/g, path.posix.sep);
26
+ if (!hasMagic(pattern, { magicalBraces: true })) {
27
+ const stats = await stat(pattern);
28
+ if (!stats.isFile()) {
29
+ pattern = path.posix.join(pattern, "**", "*.{ts,js,mts,mjs}");
30
+ }
31
+ }
32
+ return glob(pattern, options);
33
+ })
34
+ );
35
+ })
36
+ );
37
+ return [...new Set(files.flat(2))].sort();
38
+ };
39
+ var normalizeSearchPath = (paths) => {
40
+ if (typeof paths === "string") {
41
+ return [{ pattern: [paths] }];
42
+ }
43
+ if (Array.isArray(paths)) {
44
+ if (!paths.length)
45
+ return [];
46
+ return isStringArray(paths) ? [{ pattern: paths }] : paths;
47
+ }
48
+ return [paths];
49
+ };
50
+ var isStringArray = (data) => {
51
+ return typeof data[0] === "string";
52
+ };
53
+
54
+ // src/file-to-modules.ts
55
+ import { pathToFileURL } from "node:url";
56
+ var fileToModules = async (files, filter) => {
57
+ const total = /* @__PURE__ */ new Set();
58
+ await Promise.all(
59
+ files.map(async (file) => {
60
+ const modules = await import(pathToFileURL(file).toString());
61
+ if (typeof modules === "object") {
62
+ Object.values(modules).forEach((item) => {
63
+ total.add(item);
64
+ });
65
+ }
66
+ })
67
+ );
68
+ return filter ? [...total].filter(filter) : [...total];
69
+ };
70
+ export {
71
+ fileToModules,
72
+ pathToFiles
73
+ };
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/path-to-files.ts","../src/file-to-modules.ts"],"sourcesContent":["import path from 'node:path';\nimport { stat } from 'node:fs/promises';\nimport { glob, type GlobOptionsWithFileTypesFalse, hasMagic } from 'glob';\n\ninterface Options {\n pattern: string[];\n ignore?: string[];\n dot?: boolean;\n /**\n * 忽略 **node_modules** 文件夹。默认值:`true`\n */\n ignoreNodeModules?: boolean;\n /**\n * 忽略所有 **.d.{ts,mts,cts}** 类型声明文件。默认值:`true`\n */\n ignoreDTS?: boolean;\n}\n\nexport type PathToFileOptions = string | string[] | Options | Options[];\n\nexport const pathToFiles = async (\n paths: PathToFileOptions,\n): Promise<string[]> => {\n const opts = normalizeSearchPath(paths);\n\n const files = await Promise.all(\n opts.map((opt) => {\n const { dot, pattern: patterns } = opt;\n const ignore = (opt.ignore || []).slice();\n // FIXME: 即使不被ignore,也只能搜索到 node_modules/abc/xyz.js 这一层级的文件,再往下的子目录就被忽略了。\n ignore.push('**/node_modules/**', '**/*.d.{ts,mts,cts}');\n\n const options: GlobOptionsWithFileTypesFalse = {\n nodir: true,\n dot,\n ignore,\n absolute: true,\n withFileTypes: false,\n maxDepth: Infinity,\n };\n\n return Promise.all(\n patterns.map(async (pattern) => {\n if (!path.isAbsolute(pattern)) {\n pattern = path.posix.resolve(pattern);\n }\n\n /**\n * 在windows系统默认使用反斜杠`\\`拼接路径,但是glob只支持正斜杠`/`.\n * @link https://github.com/isaacs/node-glob#windows\n */\n pattern = pattern.replace(/\\\\/g, path.posix.sep);\n\n /**\n * glob.hasMagic 不支持带反斜杠(\\)的路径。从glob@9.0.0起,花括号不再是魔术路径\n * @see https://github.com/isaacs/node-glob/issues/496\n */\n if (!hasMagic(pattern, { magicalBraces: true })) {\n const stats = await stat(pattern);\n if (!stats.isFile()) {\n pattern = path.posix.join(pattern, '**', '*.{ts,js,mts,mjs}');\n }\n }\n\n return glob(pattern, options);\n }),\n );\n }),\n );\n\n return [...new Set(files.flat(2))].sort();\n};\n\nconst normalizeSearchPath = (paths: PathToFileOptions): Options[] => {\n if (typeof paths === 'string') {\n return [{ pattern: [paths] }];\n }\n\n if (Array.isArray(paths)) {\n if (!paths.length) return [];\n return isStringArray(paths) ? [{ pattern: paths }] : paths;\n }\n\n return [paths];\n};\n\nconst isStringArray = (data: string[] | Options[]): data is string[] => {\n return typeof data[0] === 'string';\n};\n","import { pathToFileURL } from 'node:url';\n\nexport const fileToModules = async <T extends unknown>(\n files: string[],\n filter?: (item?: T) => boolean,\n): Promise<T[]> => {\n const total = new Set<T>();\n await Promise.all(\n files.map(async (file) => {\n const modules = await import(pathToFileURL(file).toString());\n if (typeof modules === 'object') {\n Object.values(modules).forEach((item) => {\n total.add(item as any);\n });\n }\n }),\n );\n return filter ? [...total].filter(filter) : [...total];\n};\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,SAAS,YAAY;AACrB,SAAS,MAA0C,gBAAgB;AAkB5D,IAAM,cAAc,OACzB,UACsB;AACtB,QAAM,OAAO,oBAAoB,KAAK;AAEtC,QAAM,QAAQ,MAAM,QAAQ;AAAA,IAC1B,KAAK,IAAI,CAAC,QAAQ;AAChB,YAAM,EAAE,KAAK,SAAS,SAAS,IAAI;AACnC,YAAM,UAAU,IAAI,UAAU,CAAC,GAAG,MAAM;AAExC,aAAO,KAAK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QAAM,QAAQ;AAAA,IACZ,MAAM,IAAI,OAAO,SAAS;AACxB,YAAM,UAAU,MAAM,OAAO,cAAc,IAAI,EAAE,SAAS;AAC1D,UAAI,OAAO,YAAY,UAAU;AAC/B,eAAO,OAAO,OAAO,EAAE,QAAQ,CAAC,SAAS;AACvC,gBAAM,IAAI,IAAW;AAAA,QACvB,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,SAAS,CAAC,GAAG,KAAK,EAAE,OAAO,MAAM,IAAI,CAAC,GAAG,KAAK;AACvD;","names":[]}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@aomex/internal-file-import",
3
+ "version": "0.0.27",
4
+ "description": "ESM文件转换为模块",
5
+ "type": "module",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "LICENSE",
20
+ "package.json",
21
+ "README.md",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "keywords": [],
25
+ "repository": "git@github.com:aomex/aomex.git",
26
+ "homepage": "https://aomex.js.org",
27
+ "license": "MIT",
28
+ "dependencies": {
29
+ "glob": "^10.3.10"
30
+ },
31
+ "scripts": {
32
+ "test": "vitest"
33
+ }
34
+ }