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

fix: resolveNets.ts diagnostic error handling #1198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cli/resolveNets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,52 @@ import { register } from "../deps/shims/register-ts-node.ts"
import * as path from "../deps/std/path.ts"
import { NetSpec } from "../nets/mod.ts"

const TS_MODULE_HAS_NO_EXPORTED_MEMBER = 2305
const TS_CANNOT_FIND_MODULE_OR_ITS_CORRESPONDING_TYPE_DECLARATIONS = 2307

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 await _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 TS_MODULE_HAS_NO_EXPORTED_MEMBER:
errorMessage += " Did you import from \"capi/nets\"?"
break
case TS_CANNOT_FIND_MODULE_OR_ITS_CORRESPONDING_TYPE_DECLARATIONS:
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