forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.ts
More file actions
379 lines (318 loc) · 9.77 KB
/
modules.ts
File metadata and controls
379 lines (318 loc) · 9.77 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import memoize from 'lodash/memoize';
import getTemplateDefinition from '../templates';
import parse from '../templates/configuration/parse';
import { Directory, Module, Sandbox } from '../types';
const compareTitle = (
original: string,
test: string,
ignoredExtensions: Array<string>
) => {
if (original === test) return true;
return ignoredExtensions.some(ext => original === `${test}.${ext}`);
};
const throwError = (path: string) => {
throw new Error(`Cannot find module in ${path}`);
};
export function resolveDirectory(
_path: string | undefined,
modules: Array<Module>,
directories: Array<Directory>,
_startdirectoryShortid: string | undefined = undefined
) {
if (!_path) {
return throwError('');
}
let path = _path;
let startdirectoryShortid = _startdirectoryShortid;
// If paths start with {{sandboxRoot}} we see them as root paths
if (path.startsWith('{{sandboxRoot}}')) {
startdirectoryShortid = undefined;
path = _path.replace('{{sandboxRoot}}/', './');
}
// Split path
const splitPath = path
.replace(/^.\//, '')
.split('/')
.filter(Boolean);
const foundDirectoryShortid = splitPath.reduce(
(dirId: string | undefined, pathPart: string, i: number) => {
// Meaning this is the last argument, so the directory
if (i === splitPath.length) {
return dirId;
}
if (pathPart === '..') {
// Find the parent
const dir = directories.find(d => d.shortid === dirId);
if (dir == null) throwError(path);
return dir.directoryShortid;
}
const directoriesInDirectory = directories.filter(
// eslint-disable-next-line eqeqeq
m => m.directoryShortid == dirId
);
const nextDirectory = directoriesInDirectory.find(d =>
compareTitle(d.title, pathPart, [])
);
if (nextDirectory == null) throwError(path);
return nextDirectory.shortid;
},
startdirectoryShortid
);
return directories.find(d => d.shortid === foundDirectoryShortid);
}
export function getModulesAndDirectoriesInDirectory(
directory: Directory,
modules: Array<Module>,
directories: Array<Directory>
) {
const { path } = directory;
return {
removedModules: modules.filter(moduleItem =>
moduleItem.path.startsWith(path)
),
removedDirectories: directories.filter(
directoryItem =>
directoryItem.path.startsWith(path) && directoryItem !== directory
),
};
}
export function getModulesInDirectory(
_path: string | undefined,
modules: Array<Module>,
directories: Array<Directory>,
_startdirectoryShortid: string | undefined = undefined
) {
if (!_path) return throwError('');
let path = _path;
// If paths start with {{sandboxRoot}} we see them as root paths
if (path.startsWith('{{sandboxRoot}}')) {
path = _path.replace('{{sandboxRoot}}/', './');
}
// Split path
const splitPath = path
.replace(/^.\//, '')
.split('/')
.filter(Boolean);
const dirPath = path
.replace(/^.\//, '')
.split('/')
.filter(Boolean);
dirPath.pop();
const dir = resolveDirectory(
dirPath.join('/') || '/',
modules,
directories,
_startdirectoryShortid
);
const foundDirectoryShortid = dir ? dir.shortid : null;
const lastPath = splitPath[splitPath.length - 1];
const modulesInFoundDirectory = modules.filter(
// eslint-disable-next-line eqeqeq
m => m.directoryShortid == foundDirectoryShortid
);
return {
modules: modulesInFoundDirectory,
foundDirectoryShortid,
lastPath,
splitPath,
};
}
/**
* Convert the module path to a module
*/
export const resolveModule = (
path: string | undefined,
modules: Array<Module>,
directories: Array<Directory>,
startdirectoryShortid: string | undefined = undefined,
ignoredExtensions: Array<string> = ['js', 'jsx', 'json']
): Module => {
const {
modules: modulesInFoundDirectory,
lastPath,
splitPath,
foundDirectoryShortid,
} = getModulesInDirectory(path, modules, directories, startdirectoryShortid);
// Find module with same name
const foundModule = modulesInFoundDirectory.find(m =>
compareTitle(m.title, lastPath, ignoredExtensions)
);
if (foundModule) return foundModule;
// Check all directories in said directory for same name
const directoriesInFoundDirectory = directories.filter(
// eslint-disable-next-line eqeqeq
m => m.directoryShortid == foundDirectoryShortid
);
const foundDirectory = directoriesInFoundDirectory.find(m =>
compareTitle(m.title, lastPath, ignoredExtensions)
);
// If it refers to a directory
if (foundDirectory) {
// Find module named index
const indexModule = modules.find(
m =>
// eslint-disable-next-line eqeqeq
m.directoryShortid == foundDirectory.shortid &&
compareTitle(m.title, 'index', ignoredExtensions)
);
if (indexModule == null) throwError(path);
return indexModule;
}
if (splitPath[splitPath.length - 1] === '') {
// Last resort, check if there is something in the same folder called index
const indexModule = modulesInFoundDirectory.find(m =>
compareTitle(m.title, 'index', ignoredExtensions)
);
if (indexModule) return indexModule;
}
return throwError(path);
};
function findById(entities: Array<Module | Directory>, id: string) {
return entities.find(e => e.id === id);
}
function findByShortid(
entities: Array<Module | Directory>,
shortid: string | undefined
) {
return entities.find(e => e.shortid === shortid);
}
const getPath = (
arrayToLookIn: Array<Module> | Array<Directory>,
modules: Array<Module>,
directories: Array<Directory>,
id: string
) => {
const module = findById(arrayToLookIn, id);
if (!module) return '';
let directory = findByShortid(directories, module.directoryShortid);
let path = '/';
if (directory == null && module.directoryShortid) {
// Parent got deleted, return '';
return '';
}
while (directory != null) {
path = `/${directory.title}${path}`;
const lastDirectoryShortid = directory.directoryShortid;
directory = findByShortid(directories, directory.directoryShortid);
// In this case it couldn't find the parent directory of this dir, so probably
// deleted. we just return '' in that case
if (!directory && lastDirectoryShortid) {
return '';
}
}
return `${path}${module.title}`;
};
const memoizeFunction = (modules, directories, id) =>
id +
modules.map(m => m.id + m.title + m.directoryShortid).join(',') +
directories.map(d => d.id + d.title + d.directoryShortid).join(',');
export const getModulePath = (
modules: Array<Module>,
directories: Array<Directory>,
id: string
) => getPath(modules, modules, directories, id);
export const getDirectoryPath = (
modules: Array<Module>,
directories: Array<Directory>,
id: string
) => getPath(directories, modules, directories, id);
export const getChildren = memoize(
(
modules: Array<Module> = [],
directories: Array<Directory> = [],
id: string
) => [
...directories.filter(d => d.directoryShortid === id),
...modules.filter(m => m.directoryShortid === id),
],
memoizeFunction
);
export const isMainModule = (
module: Module,
modules: Module[],
directories: Directory[],
entry: string = 'index.js'
) => {
const path = getModulePath(modules, directories, module.id);
return path.replace('/', '') === entry;
};
export const findMainModule = (sandbox?: Sandbox) => {
const resolve = resolveModuleWrapped(sandbox);
// first attempt: try loading the first file that exists from
// the list of possible defaults in the template defination
const templateDefinition = getTemplateDefinition(sandbox.template);
const parsedConfigs = parse(
sandbox.template,
templateDefinition.configurationFiles,
resolve,
sandbox
);
const defaultOpenedFiles = templateDefinition.getDefaultOpenedFiles(
parsedConfigs
);
const defaultOpenModule = defaultOpenedFiles
.map(path => resolve(path))
.find(module => Boolean(module));
if (defaultOpenModule) {
return defaultOpenModule;
}
// second attempt: try loading the entry file if it exists
const entryModule = resolve(sandbox.entry);
if (entryModule) {
return entryModule;
}
// third attempt: give up and load the first file in the list
return sandbox.modules[0];
};
export const findCurrentModule = (
modules: Module[],
directories: Directory[],
modulePath: string = '',
mainModule: Module
): Module => {
// cleanPath, encode and replace first /
const cleanPath = decodeURIComponent(modulePath).replace('/', '');
let foundModule = null;
try {
foundModule = resolveModule(cleanPath, modules, directories);
} catch (e) {
/* leave empty */
}
return (
foundModule ||
modules.find(m => m.id === modulePath) ||
modules.find(m => m.shortid === modulePath) || // deep-links requires this
mainModule
);
};
export const resolveModuleWrapped = sandbox => (path: string) => {
try {
return resolveModule(path, sandbox.modules, sandbox.directories);
} catch (e) {
return undefined;
}
};
export const resolveDirectoryWrapped = sandbox => (path: string) => {
try {
return resolveDirectory(path, sandbox.modules, sandbox.directories);
} catch (e) {
return undefined;
}
};
const inDirectoryMemoize = (directories, sourceShortid, destinationShortid) =>
sourceShortid +
destinationShortid +
directories.map(d => d.id + d.title + d.directoryShortid).join(',');
export const inDirectory = memoize(
(directories: Directory[], rootShortid: string, shortid: string) => {
let directory = findByShortid(directories, shortid);
while (directory) {
if (directory.directoryShortid === rootShortid) {
return true;
}
directory = findByShortid(directories, directory.directoryShortid);
}
return false;
},
inDirectoryMemoize
);