Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 32bc366

Browse files
lsagetlethiasjustjavac
authored andcommitted
Add headers fallback when module is not found (#3)
1 parent 7906a5c commit 32bc366

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/index.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// modified from https://github.com/Microsoft/typescript-tslint-plugin
2+
import * as fs from "fs";
23
import * as path from "path";
34

45
import * as mockRequire from "mock-require";
@@ -87,9 +88,33 @@ function convertRemoteToLocalCache(moduleName: string): string {
8788
}
8889
}
8990

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)
9192
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}".`);
9395

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;
95120
}

0 commit comments

Comments
 (0)