(
kind: string,
optionName: string,
basedir: string,
query: unknown,
importName = "default"
)
| 37 | const getTstlDirectory = () => path.dirname(__dirname); |
| 38 | |
| 39 | export function resolvePlugin( |
| 40 | kind: string, |
| 41 | optionName: string, |
| 42 | basedir: string, |
| 43 | query: unknown, |
| 44 | importName = "default" |
| 45 | ): { error?: ts.Diagnostic; result?: unknown } { |
| 46 | if (typeof query !== "string") { |
| 47 | return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "string") }; |
| 48 | } |
| 49 | |
| 50 | const isModuleNotFoundError = (error: any) => error.code === "MODULE_NOT_FOUND"; |
| 51 | |
| 52 | let resolved: string; |
| 53 | try { |
| 54 | resolved = resolve.sync(query, { basedir, extensions: [".js", ".ts", ".tsx"] }); |
| 55 | } catch (err) { |
| 56 | if (!isModuleNotFoundError(err)) throw err; |
| 57 | return { error: diagnosticFactories.couldNotResolveFrom(kind, query, basedir) }; |
| 58 | } |
| 59 | |
| 60 | const hasNoRequireHook = require.extensions[".ts"] === undefined; |
| 61 | if (hasNoRequireHook && (resolved.endsWith(".ts") || resolved.endsWith(".tsx"))) { |
| 62 | try { |
| 63 | const tsNodePath = resolve.sync("ts-node", { basedir: getTstlDirectory() }); |
| 64 | const tsNode: typeof import("ts-node") = require(tsNodePath); |
| 65 | tsNode.register({ transpileOnly: true }); |
| 66 | } catch (err) { |
| 67 | if (!isModuleNotFoundError(err)) throw err; |
| 68 | return { error: diagnosticFactories.toLoadItShouldBeTranspiled(kind, query) }; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const commonjsModule = require(resolved); |
| 73 | const factoryModule = commonjsModule.__esModule ? commonjsModule : { default: commonjsModule }; |
| 74 | const result = factoryModule[importName]; |
| 75 | if (result === undefined) { |
| 76 | return { error: diagnosticFactories.shouldHaveAExport(kind, query, importName) }; |
| 77 | } |
| 78 | |
| 79 | return { result }; |
| 80 | } |
no test coverage detected