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

Function parseFile

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

Source from the content-addressed store, hash-verified

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