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

Function parseFile

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

Source from the content-addressed store, hash-verified

316}
317
318function parseFile(file: string, contents: string, npmDeps = {}, urls = {}) {
319 let deps = new Set<string>();
320 for (let [, specifier] of contents.matchAll(/import (?:.|\n)*?['"](.+?)['"]/g)) {
321 specifier = specifier.replace(
322 /(vanilla-starter|tailwind-starter)\//g,
323 (m, s) => 'starters/' + (s === 'vanilla-starter' ? 'docs' : 'tailwind') + '/src/'
324 );
325
326 if (specifier.startsWith('url:')) {
327 urls[specifier] = resolveUrl(specifier.slice(4), file);
328 continue;
329 }
330
331 if (!/^(\.|starters)/.test(specifier)) {
332 let dep = specifier.startsWith('@')
333 ? specifier.split('/').slice(0, 2).join('/')
334 : specifier.split('/')[0];
335 npmDeps[dep] ??= '^' + getPackageVersion(dep);
336 continue;
337 }
338
339 let resolved = specifier.startsWith('.')
340 ? path.resolve(path.dirname(file), specifier)
341 : path.resolve('../../../' + specifier);
342 if (path.extname(resolved) === '') {
343 if (fs.existsSync(resolved + '.tsx')) {
344 resolved += '.tsx';
345 } else if (fs.existsSync(resolved + '.ts')) {
346 resolved += '.ts';
347 }
348 }
349
350 deps.add(resolved);
351 }
352
353 return deps;
354}
355
356export interface DownloadFiles {
357 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