-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno.import-map.generator.js
71 lines (56 loc) · 2.46 KB
/
deno.import-map.generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"use strict";
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const { match } = require("assert");
process.chdir(__dirname);
const TEST_PATH = "test";
const LIB_ESM_PATH = "lib/esm";
const LIB_ALIAS = "#lib";
const LIB_ALIAS_DENO = "#lib/deno";
let imports = {
"chai": "./node_modules/chai/index.mjs",
"moq.ts/internal": "./node_modules/moq.ts/fesm2015/moq.ts.js",
"moq.ts": "./node_modules/moq.ts/fesm2015/moq.ts.js"
};
console.log("Generating import map for Deno tests...");
const platformDirs = glob.globSync(TEST_PATH + "/*/index.ts", { absolute: true })
.map(file => path.dirname(file))
.filter(dir => path.basename(dir) !== "deno");
for (const file of glob.globIterateSync(TEST_PATH + "/**/*.ts", { absolute: true })) {
if (platformDirs.some(dir => file.startsWith(dir))) continue;
const fileContent = fs.readFileSync(file, "utf8");
const regex = /(?:import|from)\s*('|")([.#].*?)\1()/g;
let match;
while ((match = regex.exec(fileContent)) !== null) {
let specifier = match[2], resolvedFilePath;
[specifier, resolvedFilePath] = (specifier.startsWith(".") ? resolveRelativeSpecifier : resolveAliasedSpecifier)(specifier, file);
if (resolvedFilePath == null) {
console.error(`Import specifier '${specifier}' could not be resolved.`);
process.exit(1);
}
imports[specifier] = resolvedFilePath;
}
}
imports = Object.keys(imports).sort().reduce((result, key) => (result[key] = imports[key], result), {});
const importMapJson = JSON.stringify({ imports }, void 0, " ");
fs.writeFileSync("deno.import-map.json", importMapJson, "utf8");
console.log("Done.");
/* Helper functions */
function resolveRelativeSpecifier(specifier, file) {
let resolvedFilePath = path.resolve(path.join(path.dirname(file), specifier));
if (fs.existsSync(resolvedFilePath) && fs.lstatSync(resolvedFilePath).isDirectory()) {
resolvedFilePath += "/index";
}
specifier = "./" + normalizePathSeparator(path.relative(".", resolvedFilePath));
return [specifier, specifier + ".ts"];
}
function resolveAliasedSpecifier(specifier) {
if (specifier === LIB_ALIAS || specifier === LIB_ALIAS_DENO) {
return [specifier, "./" + LIB_ESM_PATH + specifier.substring(LIB_ALIAS.length) + "/index.js"];
}
if (specifier.startsWith(LIB_ALIAS + "/")) {
return [specifier, "./" + LIB_ESM_PATH + "/" + specifier.substring(LIB_ALIAS.length + 1) + ".js"];
}
}
function normalizePathSeparator(path) { return path.replace(/\\/g, "/"); }