MCPcopy Create free account
hub / github.com/adobe/react-spectrum / parseFile

Function parseFile

packages/dev/s2-docs/src/CodeBlock.tsx:342–378  ·  view source on GitHub ↗
(file: string, contents: string, npmDeps = {}, urls = {})

Source from the content-addressed store, hash-verified

340}
341
342function parseFile(file: string, contents: string, npmDeps = {}, urls = {}) {
343 let deps = new Set<string>();
344 for (let [, specifier] of contents.matchAll(/import (?:.|\n)*?['"](.+?)['"]/g)) {
345 specifier = specifier.replace(
346 STARTER_ALIAS_RE,
347 (m, s) => 'starters/' + STARTER_DIRS[s] + '/src/'
348 );
349
350 if (specifier.startsWith('url:')) {
351 urls[specifier] = resolveUrl(specifier.slice(4), file);
352 continue;
353 }
354
355 if (!/^(\.|starters)/.test(specifier)) {
356 let dep = specifier.startsWith('@')
357 ? specifier.split('/').slice(0, 2).join('/')
358 : specifier.split('/')[0];
359 npmDeps[dep] ??= '^' + getPackageVersion(dep);
360 continue;
361 }
362
363 let resolved = specifier.startsWith('.')
364 ? path.resolve(path.dirname(file), specifier)
365 : path.resolve(baseDir, specifier);
366 if (path.extname(resolved) === '') {
367 if (fs.existsSync(resolved + '.tsx')) {
368 resolved += '.tsx';
369 } else if (fs.existsSync(resolved + '.ts')) {
370 resolved += '.ts';
371 }
372 }
373
374 deps.add(resolved);
375 }
376
377 return deps;
378}
379
380export interface DownloadFiles {
381 files: {

Callers 2

findAllFilesFunction · 0.85
getExampleFilesFunction · 0.85

Calls 4

resolveUrlFunction · 0.85
getPackageVersionFunction · 0.85
sliceMethod · 0.80
addMethod · 0.65

Tested by

no test coverage detected