Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: resolves nets ts diagnostic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed Jul 17, 2023
1 parent d0878c8 commit 85c7588
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion cli/resolveNets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,48 @@ import { NetSpec } from "../nets/mod.ts"

const $nets = $.record($.instance(NetSpec as new() => NetSpec, $.tuple(), (_: NetSpec) => []))

const $tsDiagnostics = $.field("diagnosticCodes", $.array($.u32))

export async function resolveNets(maybeNetsPath?: string): Promise<Record<string, NetSpec>> {
const resolvedNetsPath = await resolveNetsPath(maybeNetsPath)
if (resolvedNetsPath.endsWith(".ts")) {
await register()
}
// shimmed by dnt
let nets = await _import(resolvedNetsPath)
let nets = await importNets(resolvedNetsPath)
if ("default" in nets) nets = nets.default
$.assert($nets, nets)
for (const key in nets) nets[key]!.name = key
return nets
}

async function importNets(netsPath: string) {
try {
return _import(netsPath)
} catch (err) {
let errorMessage = `Failed to import nets file ${netsPath}`
if ($.is($tsDiagnostics, err)) {
for (const code of new Set(err.diagnosticCodes)) {
errorMessage += `: Typescript compiler error TS${code}`
switch (code) {
case 2305:
errorMessage += ": Did you forget to import from \"capi/nets\"?"
break
case 2307:
errorMessage +=
": Did you set your tsconfig module to \"ESNext\" and moduleResolution to \"node16\"?"
break
default:
break
}
}
} else {
console.error(err)
}
throw new Error(errorMessage)
}
}

async function resolveNetsPath(maybeNetsPath?: string): Promise<string> {
if (maybeNetsPath) return path.resolve(maybeNetsPath)
for (const p of ["nets.ts", "nets.js"]) {
Expand Down

0 comments on commit 85c7588

Please sign in to comment.