-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadImportMapFile.mjs
53 lines (45 loc) · 1.24 KB
/
readImportMapFile.mjs
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
// @ts-check
import assertImportMap from "./assertImportMap.mjs";
/**
* Reads an import map JSON file.
* @see https://github.com/WICG/import-maps
* @param {URL} importMapFileUrl Import map file URL.
* @returns {Promise<import("./assertImportMap.mjs").ImportMap>} Resolves the
* import map.
*/
export default async function readImportMapFile(importMapFileUrl) {
if (!(importMapFileUrl instanceof URL)) {
throw new TypeError(
"Argument 1 `importMapFileUrl` must be a `URL` instance.",
);
}
/** @type {string} */
let importMapJson;
try {
importMapJson = await Deno.readTextFile(importMapFileUrl);
} catch (cause) {
throw new Error(
`Error reading import map file \`${importMapFileUrl.href}\`.`,
{ cause },
);
}
/** @type {import("./assertImportMap.mjs").ImportMap} */
let importMapData;
try {
importMapData = JSON.parse(importMapJson);
} catch (cause) {
throw new Error(
`Invalid JSON in import map file \`${importMapFileUrl.href}\`.`,
{ cause },
);
}
try {
assertImportMap(importMapData);
} catch (cause) {
throw new TypeError(
`Invalid content in import map file \`${importMapFileUrl.href}\`.`,
{ cause },
);
}
return importMapData;
}