-
Notifications
You must be signed in to change notification settings - Fork 567
/
pathFileUtil.ts
59 lines (54 loc) 路 1.41 KB
/
pathFileUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { extname } from 'path';
import { readFileSync } from 'fs';
export function isPath(p): boolean {
// eslint-disable-next-line no-useless-escape
if (/(^[\.\/])|:|\\/.test(p)) {
return true;
}
return false;
}
export function isPathEqual(one: string, two: string) {
if (!one || !two) {
return false;
}
const ext = extname(one);
return one.replace(ext, '') === two;
}
export function getFileContentSync(filePath: any, encoding?: BufferEncoding) {
return typeof filePath === 'string'
? readFileSync(filePath, {
encoding,
})
: filePath;
}
export const PathFileUtil = {
isPath,
isPathEqual,
getFileContentSync,
};
export function getModuleRequirePathList(moduleName: string): string[] {
const moduleNameList = [moduleName, moduleName.replace(/\//g, '_')];
let moduleNameMap = {};
const modulePathList = [];
Object.keys(require.cache || {}).forEach(moduleName => {
let moduleIndex = -1;
for (const moduleName of moduleNameList) {
const index = moduleName.indexOf(moduleName);
if (index !== -1) {
moduleIndex = index;
break;
}
}
if (moduleIndex === -1) {
return;
}
const modulePath = moduleName.slice(0, moduleIndex);
if (moduleNameMap[modulePath]) {
return;
}
moduleNameMap[modulePath] = true;
modulePathList.push(modulePath);
});
moduleNameMap = undefined;
return modulePathList;
}