|
1 | 1 | // modified from https://github.com/Microsoft/typescript-tslint-plugin |
| 2 | +import * as fs from "fs"; |
2 | 3 | import * as path from "path"; |
3 | 4 |
|
4 | 5 | import * as mockRequire from "mock-require"; |
@@ -87,9 +88,33 @@ function convertRemoteToLocalCache(moduleName: string): string { |
87 | 88 | } |
88 | 89 | } |
89 | 90 |
|
90 | | - // "https://deno.land/x/std/log/mod.ts" to "$DENO_DIR/deps/https/deno.land/x/std/log/mod.ts" |
| 91 | + // "https://deno.land/x/std/log/mod" to "$DENO_DIR/deps/https/deno.land/x/std/log/mod" (no ".ts" because stripped) |
91 | 92 | const name = path.join(denoDir, "deps", moduleName.replace("://", "/")); |
92 | | - logger.info(`convert "${moduleName}" to "${name}".`); |
| 93 | + const redirectedName = fallbackHeader(name); |
| 94 | + logger.info(`convert "${moduleName}" to "${redirectedName}".`); |
93 | 95 |
|
94 | | - return name; |
| 96 | + return redirectedName; |
| 97 | +} |
| 98 | + |
| 99 | +interface IDenoModuleHeaders { |
| 100 | + mime_type: string; |
| 101 | + redirect_to: string; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * If moduleName is not found, recursively search for headers and "redirect_to" property. |
| 106 | + */ |
| 107 | +function fallbackHeader(modulePath: string): string { |
| 108 | + const validPath = modulePath.endsWith(".ts") ? modulePath : `${modulePath}.ts`; |
| 109 | + if (fs.existsSync(validPath)) { |
| 110 | + return modulePath; |
| 111 | + } |
| 112 | + |
| 113 | + const headersPath = `${validPath}.headers.json`; |
| 114 | + if (fs.existsSync(headersPath)) { |
| 115 | + const headers: IDenoModuleHeaders = JSON.parse(fs.readFileSync(headersPath, { encoding: "utf-8" })); |
| 116 | + logger.info(`redirect "${modulePath}" to "${headers.redirect_to}".`); |
| 117 | + return convertRemoteToLocalCache(stripExtNameDotTs(headers.redirect_to)); |
| 118 | + } |
| 119 | + return modulePath; |
95 | 120 | } |
0 commit comments