@aomex/internal-file-import 0.0.27 → 1.0.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/CHANGELOG.md +3 -3
- package/LICENSE +1 -1
- package/README.md +15 -0
- package/dist/index.d.ts +7 -13
- package/dist/index.js +30 -26
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @aomex/internal-file-import
|
|
2
2
|
|
|
3
|
-
## 0.0
|
|
3
|
+
## 1.0.0
|
|
4
4
|
|
|
5
|
-
###
|
|
5
|
+
### Minor Changes
|
|
6
6
|
|
|
7
|
-
- [`
|
|
7
|
+
- [`11103a2`](https://github.com/aomex/aomex/commit/11103a2aff4e081754c56b0ff18fa5130ca252e8) Thanks [@geekact](https://github.com/geekact)! - 重新制作
|
package/LICENSE
CHANGED
package/README.md
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
interface
|
|
1
|
+
interface GlobPathFullOptions {
|
|
2
2
|
pattern: string[];
|
|
3
3
|
ignore?: string[];
|
|
4
4
|
dot?: boolean;
|
|
5
|
-
/**
|
|
6
|
-
* 忽略 **node_modules** 文件夹。默认值:`true`
|
|
7
|
-
*/
|
|
8
|
-
ignoreNodeModules?: boolean;
|
|
9
|
-
/**
|
|
10
|
-
* 忽略所有 **.d.{ts,mts,cts}** 类型声明文件。默认值:`true`
|
|
11
|
-
*/
|
|
12
|
-
ignoreDTS?: boolean;
|
|
13
5
|
}
|
|
14
|
-
type
|
|
15
|
-
declare const
|
|
6
|
+
type GlobPathOptions = string | string[] | GlobPathFullOptions | GlobPathFullOptions[];
|
|
7
|
+
declare const normalizeGlobPath: (paths: GlobPathOptions) => GlobPathFullOptions[];
|
|
16
8
|
|
|
17
|
-
declare const
|
|
9
|
+
declare const pathToFiles: (paths: GlobPathOptions) => Promise<string[]>;
|
|
18
10
|
|
|
19
|
-
|
|
11
|
+
declare const getFileValues: <T extends unknown>(files: string[], filterValues?: (item?: unknown) => boolean) => Promise<T[]>;
|
|
12
|
+
|
|
13
|
+
export { type GlobPathFullOptions, type GlobPathOptions, getFileValues, normalizeGlobPath, pathToFiles };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
|
-
// src/path-to-
|
|
1
|
+
// src/path-to-file.ts
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { stat } from "node:fs/promises";
|
|
4
4
|
import { glob, hasMagic } from "glob";
|
|
5
|
+
|
|
6
|
+
// src/normalize-glob-path.ts
|
|
7
|
+
var normalizeGlobPath = (paths) => {
|
|
8
|
+
if (typeof paths === "string") {
|
|
9
|
+
return [{ pattern: [paths] }];
|
|
10
|
+
}
|
|
11
|
+
if (Array.isArray(paths)) {
|
|
12
|
+
if (!paths.length)
|
|
13
|
+
return [];
|
|
14
|
+
return isStringArray(paths) ? [{ pattern: paths }] : paths;
|
|
15
|
+
}
|
|
16
|
+
return [paths];
|
|
17
|
+
};
|
|
18
|
+
var isStringArray = (data) => {
|
|
19
|
+
return typeof data[0] === "string";
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/path-to-file.ts
|
|
5
23
|
var pathToFiles = async (paths) => {
|
|
6
|
-
const opts =
|
|
24
|
+
const opts = normalizeGlobPath(paths);
|
|
7
25
|
const files = await Promise.all(
|
|
8
26
|
opts.map((opt) => {
|
|
9
27
|
const { dot, pattern: patterns } = opt;
|
|
10
28
|
const ignore = (opt.ignore || []).slice();
|
|
11
|
-
ignore.push("**/node_modules
|
|
29
|
+
ignore.push("**/node_modules/**/**", "**/*.d.{ts,mts}");
|
|
12
30
|
const options = {
|
|
13
31
|
nodir: true,
|
|
14
32
|
dot,
|
|
@@ -20,7 +38,7 @@ var pathToFiles = async (paths) => {
|
|
|
20
38
|
return Promise.all(
|
|
21
39
|
patterns.map(async (pattern) => {
|
|
22
40
|
if (!path.isAbsolute(pattern)) {
|
|
23
|
-
pattern = path.
|
|
41
|
+
pattern = path.resolve(pattern);
|
|
24
42
|
}
|
|
25
43
|
pattern = pattern.replace(/\\/g, path.posix.sep);
|
|
26
44
|
if (!hasMagic(pattern, { magicalBraces: true })) {
|
|
@@ -36,39 +54,25 @@ var pathToFiles = async (paths) => {
|
|
|
36
54
|
);
|
|
37
55
|
return [...new Set(files.flat(2))].sort();
|
|
38
56
|
};
|
|
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
57
|
|
|
54
|
-
// src/file-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const total = /* @__PURE__ */ new Set();
|
|
58
|
+
// src/get-file-values.ts
|
|
59
|
+
var getFileValues = async (files, filterValues) => {
|
|
60
|
+
const totalFiles = /* @__PURE__ */ new Set();
|
|
58
61
|
await Promise.all(
|
|
59
62
|
files.map(async (file) => {
|
|
60
|
-
const modules = await import(
|
|
63
|
+
const modules = await import(file);
|
|
61
64
|
if (typeof modules === "object") {
|
|
62
65
|
Object.values(modules).forEach((item) => {
|
|
63
|
-
|
|
66
|
+
totalFiles.add(item);
|
|
64
67
|
});
|
|
65
68
|
}
|
|
66
69
|
})
|
|
67
70
|
);
|
|
68
|
-
return
|
|
71
|
+
return filterValues ? [...totalFiles].filter(filterValues) : [...totalFiles];
|
|
69
72
|
};
|
|
70
73
|
export {
|
|
71
|
-
|
|
74
|
+
getFileValues,
|
|
75
|
+
normalizeGlobPath,
|
|
72
76
|
pathToFiles
|
|
73
77
|
};
|
|
74
78
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/path-to-
|
|
1
|
+
{"version":3,"sources":["../src/path-to-file.ts","../src/normalize-glob-path.ts","../src/get-file-values.ts"],"sourcesContent":["import path from 'node:path';\nimport { stat } from 'node:fs/promises';\nimport { glob, type GlobOptionsWithFileTypesFalse, hasMagic } from 'glob';\nimport { normalizeGlobPath, type GlobPathOptions } from './normalize-glob-path';\n\nexport const pathToFiles = async (paths: GlobPathOptions): Promise<string[]> => {\n const opts = normalizeGlobPath(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 ignore.push('**/node_modules/**/**', '**/*.d.{ts,mts}');\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.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","export interface GlobPathFullOptions {\n pattern: string[];\n ignore?: string[];\n dot?: boolean;\n}\n\nexport type GlobPathOptions =\n | string\n | string[]\n | GlobPathFullOptions\n | GlobPathFullOptions[];\n\nexport const normalizeGlobPath = (paths: GlobPathOptions): GlobPathFullOptions[] => {\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[] | GlobPathFullOptions[]): data is string[] => {\n return typeof data[0] === 'string';\n};\n","export const getFileValues = async <T extends unknown>(\n files: string[],\n filterValues?: (item?: unknown) => boolean,\n): Promise<T[]> => {\n const totalFiles = new Set<T>();\n await Promise.all(\n files.map(async (file) => {\n const modules = await import(file);\n if (typeof modules === 'object') {\n Object.values(modules).forEach((item) => {\n totalFiles.add(item as any);\n });\n }\n }),\n );\n return filterValues ? [...totalFiles].filter(filterValues) : [...totalFiles];\n};\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,SAAS,YAAY;AACrB,SAAS,MAA0C,gBAAgB;;;ACU5D,IAAM,oBAAoB,CAAC,UAAkD;AAClF,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,CAAC,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC;AAAA,EAC9B;AAEA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,QAAI,CAAC,MAAM;AAAQ,aAAO,CAAC;AAC3B,WAAO,cAAc,KAAK,IAAI,CAAC,EAAE,SAAS,MAAM,CAAC,IAAI;AAAA,EACvD;AAEA,SAAO,CAAC,KAAK;AACf;AAEA,IAAM,gBAAgB,CAAC,SAA6D;AAClF,SAAO,OAAO,KAAK,CAAC,MAAM;AAC5B;;;ADtBO,IAAM,cAAc,OAAO,UAA8C;AAC9E,QAAM,OAAO,kBAAkB,KAAK;AAEpC,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;AACxC,aAAO,KAAK,yBAAyB,iBAAiB;AAE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
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "aomex根据文件路径导入模块",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"exports": {
|
|
@@ -28,7 +28,5 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"glob": "^10.3.10"
|
|
30
30
|
},
|
|
31
|
-
"scripts": {
|
|
32
|
-
"test": "vitest"
|
|
33
|
-
}
|
|
31
|
+
"scripts": {}
|
|
34
32
|
}
|