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

Function parseFile

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

Source from the content-addressed store, hash-verified

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