-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathcodeBrowser.ts
More file actions
258 lines (214 loc) · 6.01 KB
/
codeBrowser.ts
File metadata and controls
258 lines (214 loc) · 6.01 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { type CodeBrowserTreeDirectory, type UnpkgMeta } from '~/types';
export const PREVIEW_DISABLED_FILES = [
'a',
'aar',
'bin',
'caffemodel',
'car',
'class',
'dex',
'emd',
'exe',
'extrieve',
'jar',
'keystore',
'gz',
'mat',
'nib',
'otf',
'pb',
'pbd',
'rc',
'raw',
'so',
'strings',
'swiftdoc',
'swiftsourceinfo',
'tfl',
'tflite',
'tgz',
'ttf',
'xcuserstate',
];
export const IMAGE_FILES = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'ico'];
export const FILE_WARNINGS = [
{
message: 'This file should not be part of the bundle and can be safely ignored.',
fileNames: [
'*.iml',
'*.keystore',
'.all-contributorsrc',
'.babelrc',
'.buckconfig',
'.clang-format*',
'.eslintrc.js',
'.flowconfig',
'.gitattributes',
'.gitkeep',
'.gitmodules',
'.licence-config.yaml',
'.prettierignore',
'.project',
'.swiftformat',
'.swiftlint.yml',
'.travis.yml',
'.watchmanconfig',
'babel.config.js',
'eslint.config*.js',
'expo-module.config.json',
'gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
'jest*.config.js',
'local.properties',
'manifest-*makeinstall*',
'metro.config.js',
'nitro.json',
'publish.gradle',
'*prettierrc.js',
'proguard-rules.pro',
'react-native.config.js',
'rollup.config.js',
'settings.gradle',
'spotless.gradle',
'tsconfig*.json',
'*.tsbuildinfo',
'tsup.config.ts',
'turbo.json',
'typedoc.json',
// LOCK FILES
'yarn.lock',
'pnpm-lock.yaml',
'bun.lock',
'bun.lockb',
'package-lock.json',
],
},
];
const FILE_WARNING_MATCHERS = FILE_WARNINGS.map(warning => ({
...warning,
matchers: warning.fileNames.map(createFileWarningMatcher),
}));
const SOURCE_MAP_PARENT_EXTENSIONS = new Set([
'cjs',
'cts',
'js',
'jsx',
'mjs',
'mts',
'ts',
'tsx',
]);
const DECLARATION_FILE_PARENT_EXTENSIONS = new Map([
['.d.cts', ['cjs']],
['.d.mts', ['mjs']],
['.d.ts', ['js', 'mjs', 'cjs']],
]);
const HASH_FILE_EXTENSIONS = new Set(['md5', 'sha1', 'sha3', 'sha256', 'sha512']);
export function getFileWarning(fileName?: string) {
if (!fileName) {
return undefined;
}
return FILE_WARNING_MATCHERS.find(warning => warning.matchers.some(matcher => matcher(fileName)));
}
export function getCodeBrowserFilePath(path: string, prefix?: string) {
return prefix ? path.replace(prefix, '') : path;
}
export function getCodeBrowserNestedFileParentPath(path: string) {
return getCodeBrowserNestedFileParentPaths(path)[0] ?? null;
}
export function getCodeBrowserNestedFileParentPaths(path: string) {
const lowerCasedPath = path.toLowerCase();
for (const [declarationFileSuffix, parentExtensions] of DECLARATION_FILE_PARENT_EXTENSIONS) {
if (!lowerCasedPath.endsWith(declarationFileSuffix)) {
continue;
}
const declarationFileBasePath = path.slice(0, -declarationFileSuffix.length);
return parentExtensions.map(parentExtension => `${declarationFileBasePath}.${parentExtension}`);
}
const nestedFileExtension = path.split('.').pop()?.toLowerCase();
if (!nestedFileExtension) {
return [];
}
const nestedFileParentPath = path.slice(0, -(nestedFileExtension.length + 1));
if (nestedFileExtension === 'map') {
const sourceMapParentExtension = nestedFileParentPath.split('.').pop()?.toLowerCase();
if (!sourceMapParentExtension || !SOURCE_MAP_PARENT_EXTENSIONS.has(sourceMapParentExtension)) {
return [];
}
return [nestedFileParentPath];
}
if (HASH_FILE_EXTENSIONS.has(nestedFileExtension)) {
return [nestedFileParentPath];
}
return [];
}
export function buildCodeBrowserFileTree(
files: UnpkgMeta['files'],
prefix?: string
): CodeBrowserTreeDirectory {
const root = createCodeBrowserTreeDirectory('', '');
files.forEach(file => {
const relativePath = getCodeBrowserFilePath(file.path, prefix);
const pathSegments = relativePath.split('/').filter(Boolean);
if (pathSegments.length === 0) {
return;
}
const fileName = pathSegments.pop();
if (!fileName) {
return;
}
let currentDirectory = root;
pathSegments.forEach(segment => {
const nextPath = currentDirectory.path ? `${currentDirectory.path}/${segment}` : segment;
currentDirectory.directories[segment] ??= createCodeBrowserTreeDirectory(segment, nextPath);
currentDirectory = currentDirectory.directories[segment];
});
currentDirectory.files.push({
name: fileName,
path: relativePath,
});
});
nestCodeBrowserSidecarFiles(root);
return root;
}
function createCodeBrowserTreeDirectory(name: string, path: string): CodeBrowserTreeDirectory {
return {
name,
path,
directories: {},
files: [],
};
}
function nestCodeBrowserSidecarFiles(directory: CodeBrowserTreeDirectory) {
const filesByPath = new Map(directory.files.map(file => [file.path, file]));
const nestedFilePaths = new Set<string>();
directory.files.forEach(file => {
const nestedFileParent = getCodeBrowserNestedFileParentPaths(file.path)
.map(nestedFileParentPath => filesByPath.get(nestedFileParentPath))
.find(Boolean);
if (!nestedFileParent) {
return;
}
nestedFileParent.nestedFiles ??= [];
nestedFileParent.nestedFiles.push(file);
nestedFilePaths.add(file.path);
});
directory.files = directory.files.filter(file => !nestedFilePaths.has(file.path));
Object.values(directory.directories).forEach(nestCodeBrowserSidecarFiles);
}
function createFileWarningMatcher(pattern: string) {
if (!pattern.includes('*')) {
return (fileName: string) => fileName === pattern;
}
const regex = new RegExp(
`^${pattern
.split('*')
.map(pathSegment => escapeRegularExpression(pathSegment))
.join('.*')}$`
);
return (fileName: string) => regex.test(fileName);
}
function escapeRegularExpression(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}