-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathresolve-utils.ts
104 lines (87 loc) · 3.68 KB
/
resolve-utils.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { normalizePath } from '@utils';
import type * as d from '../../../declarations';
const COMMON_DIR_MODULE_EXTS = ['.tsx', '.ts', '.mjs', '.js', '.jsx', '.json', '.md'];
/**
* Determine if a stringified file path is a TypeScript declaration file based on the extension at the end of the path.
* @param p the path to evaluate
* @returns `true` if the path ends in `.d.ts` (case-sensitive), `false` otherwise.
*/
export const isDtsFile = (p: string) => p.endsWith('.d.ts') || p.endsWith('.d.mts');
/**
* Determine if a stringified file path is a TypeScript file based on the extension at the end of the path. This
* function does _not_ consider type declaration files (`.d.ts` files) to be TypeScript files.
* @param p the path to evaluate
* @returns `true` if the path ends in `.ts` (case-sensitive) but does _not_ end in `.d.ts`, `false` otherwise.
*/
export const isTsFile = (p: string) => !isDtsFile(p) && p.endsWith('.ts');
/**
* Determine if a stringified file path is a TSX file based on the extension at the end of the path
* @param p the path to evaluate
* @returns `true` if the path ends in `.tsx` (case-sensitive), `false` otherwise.
*/
export const isTsxFile = (p: string) => p.endsWith('.tsx');
/**
* Determine if a stringified file path is a JSX file based on the extension at the end of the path
* @param p the path to evaluate
* @returns `true` if the path ends in `.jsx` (case-sensitive), `false` otherwise.
*/
export const isJsxFile = (p: string) => p.endsWith('.jsx');
/**
* Determine if a stringified file path is a JavaScript file based on the extension at the end of the path
* @param p the path to evaluate
* @returns `true` if the path ends in `.js` (case-sensitive), `false` otherwise.
*/
export const isJsFile = (p: string) => p.endsWith('.js');
export const isCommonDirModuleFile = (p: string) => COMMON_DIR_MODULE_EXTS.some((ext) => p.endsWith(ext));
export const setPackageVersion = (pkgVersions: Map<string, string>, pkgName: string, pkgVersion: string) => {
pkgVersions.set(pkgName, pkgVersion);
};
export const setPackageVersionByContent = (pkgVersions: Map<string, string>, pkgContent: string) => {
try {
const pkg = JSON.parse(pkgContent) as d.PackageJsonData;
if (pkg.name && pkg.version) {
setPackageVersion(pkgVersions, pkg.name, pkg.version);
}
} catch (e) {}
};
export const isLocalModule = (p: string) => p.startsWith('.') || p.startsWith('/');
export const isStencilCoreImport = (p: string) => p.startsWith('@stencil/core');
export const isNodeModulePath = (p: string) => normalizePath(p).split('/').includes('node_modules');
export const getModuleId = (orgImport: string) => {
if (orgImport.startsWith('~')) {
orgImport = orgImport.substring(1);
}
const splt = orgImport.split('/');
const m = {
moduleId: null as string,
filePath: null as string,
scope: null as string,
scopeSubModuleId: null as string,
};
if (orgImport.startsWith('@') && splt.length > 1) {
m.moduleId = splt.slice(0, 2).join('/');
m.filePath = splt.slice(2).join('/');
m.scope = splt[0];
m.scopeSubModuleId = splt[1];
} else {
m.moduleId = splt[0];
m.filePath = splt.slice(1).join('/');
}
return m;
};
export const getPackageDirPath = (p: string, moduleId: string) => {
const parts = normalizePath(p).split('/');
const m = getModuleId(moduleId);
for (let i = parts.length - 1; i >= 1; i--) {
if (parts[i - 1] === 'node_modules') {
if (m.scope) {
if (parts[i] === m.scope && parts[i + 1] === m.scopeSubModuleId) {
return parts.slice(0, i + 2).join('/');
}
} else if (parts[i] === m.moduleId) {
return parts.slice(0, i + 1).join('/');
}
}
}
return null;
};