MCPcopy Create free account
hub / github.com/arktypeio/arktype / readAndWrite

Function readAndWrite

ark/aot/nextProgram.ts:418–590  ·  view source on GitHub ↗
(
	root: string,
	opts: PrecompilePluginOptions,
	dir: string,
	{ scripts, foreign }: Tree,
	aliases: Record<string, unknown>,
	modules: string | undefined
)

Source from the content-addressed store, hash-verified

416 })
417
418const readAndWrite = async (
419 root: string,
420 opts: PrecompilePluginOptions,
421 dir: string,
422 { scripts, foreign }: Tree,
423 aliases: Record<string, unknown>,
424 modules: string | undefined
425): Promise<ProgramDone> => {
426 checkArktypeVersion()
427 if (foreign) {
428 return refusalOf(
429 `${printablePathOf(root, foreign)} may import what it can't see`
430 )
431 }
432 if (!modules) return refusalOf("no node_modules can hold its modules")
433 const cacheDir = join(modules, ".cache")
434 const unitsRoot = join(cacheDir, "ark-units")
435 const unitsDir = join(unitsRoot, hashOf(root))
436 const host = createNodeHost(opts)
437 // a unit imports arktype's runtime from there, which must be the program's
438 const unitsFrom =
439 existsSync(unitsRoot) ? realPathOf(unitsRoot)
440 : existsSync(cacheDir) ? join(realPathOf(cacheDir), "ark-units")
441 : join(realPathOf(modules), ".cache/ark-units")
442 const [fromUnits, fromRoot] = [unitsFrom, root].map(from =>
443 host.resolve("arktype/runtime", join(from, "index.js"))
444 )
445 if (!fromRoot || fromUnits !== fromRoot) {
446 return refusalOf(
447 `${printablePathOf(root, unitsRoot)} resolves another arktype`
448 )
449 }
450 const transformedFilter = transformedFilterOf(
451 optedInPackagesOf(root, opts.dependencies)
452 )
453 const isTransformed = (id: string) =>
454 transformedFilter.include.test(id) && !transformedFilter.exclude.test(id)
455 let queried: string | undefined
456 // as Turbopack resolves, through resolveAlias. Turbopack starts once this
457 // is done, so each module's loader call checks the read's against its own
458 const resolveFile = (specifier: string, importer: string) => {
459 const aliased = aliasOf(aliases, specifier)
460 const resolved =
461 aliased === undefined ?
462 host.resolve(specifier, importer)
463 : host.resolve(aliased, join(root, "next.config.js"))
464 return resolved && realPathOf(resolved.split("?")[0])
465 }
466 const scanned = await scanProgram(
467 "next",
468 scripts.map(id => ({ id })),
469 {
470 resolve: async (specifier, importer) => {
471 if (isBuiltin(specifier)) return null
472 const query = specifier.indexOf("?")
473 const resolved = resolveFile(
474 query === -1 ? specifier : specifier.slice(0, query),
475 importer

Callers 1

readProgramFunction · 0.85

Calls 15

checkArktypeVersionFunction · 0.90
printablePathOfFunction · 0.90
createNodeHostFunction · 0.90
realPathOfFunction · 0.90
transformedFilterOfFunction · 0.90
optedInPackagesOfFunction · 0.90
scanProgramFunction · 0.90
isAssetFunction · 0.90
createAsyncPrecompilerFunction · 0.90
writeLargerMessageFunction · 0.90
importsOfFunction · 0.90
appendSourceMapFunction · 0.90

Tested by

no test coverage detected