-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
ci.ts
164 lines (141 loc) Β· 4.2 KB
/
ci.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { encode } from "./deps/base64.ts";
import { posix, toFileUrl } from "./deps/path.ts";
import { parse } from "./deps/flags.ts";
import { brightGreen, red } from "./deps/colors.ts";
import { Exception } from "./core/errors.ts";
const { join } = posix;
const baseUrl = new URL(".", import.meta.url).href;
type SpecifierMap = Record<string, string>;
interface ImportMap {
imports: SpecifierMap;
scopes?: Record<string, SpecifierMap>;
}
export function checkDenoVersion(): void {
const minDenoVersion = "1.16.1";
if (Deno.version.deno < minDenoVersion) {
console.log();
console.error(red("Error installing or running Lume"));
console.log("You have an old version of Deno");
console.log(`Lume needs Deno ${brightGreen(minDenoVersion)} or greater`);
console.log(`Your current version is ${red(Deno.version.deno)}`);
console.log();
console.log(
`Run ${brightGreen("deno upgrade")} and try again`,
);
console.log();
Deno.exit(1);
}
}
async function ensureUrl(maybeUrl: string) {
try {
return new URL(maybeUrl);
} catch {
return toFileUrl(await Deno.realPath(maybeUrl));
}
}
/**
* If a given specifier map has relative paths,
* resolve them with a given base URL.
*/
function resolveSpecifierMap(specifierMap: SpecifierMap, baseUrl: URL) {
return Object.fromEntries(
Object.entries(specifierMap).map((
[key, value],
) => [key, new URL(value, baseUrl).href]),
);
}
/**
* If any specifier maps in a given import map have relative paths,
* resolve it with a given base URL.
*/
function resolveImportMap(importMap: ImportMap, baseUrl: URL) {
const imports = resolveSpecifierMap(importMap.imports, baseUrl);
const scopes = Object.fromEntries(
Object.entries(importMap.scopes ?? []).map((
[scopeSpecifier, specifierMap],
) => [scopeSpecifier, resolveSpecifierMap(specifierMap, baseUrl)]),
);
return { imports, scopes };
}
/**
* Return a data url with the import map of Lume
* Optionally merge it with a custom import map from the user
*/
export async function getImportMap(mapFile?: string) {
const map: ImportMap = {
imports: {
"lume": join(baseUrl, "/mod.ts"),
"lume/": join(baseUrl, "/"),
"https://deno.land/x/lume/": join(baseUrl, "/"),
},
};
if (mapFile) {
try {
const url = await ensureUrl(mapFile);
const file = await (await fetch(url)).text();
const parsedMap = JSON.parse(file) as ImportMap;
const resolvedMap = resolveImportMap(parsedMap, url);
map.imports = { ...map.imports, ...resolvedMap.imports };
map.scopes = parsedMap.scopes;
} catch (cause) {
throw new Exception("Unable to load the import map file", {
cause,
mapFile,
});
}
}
return `data:application/json;base64,${encode(JSON.stringify(map))}`;
}
/** Returns the Lume & Deno arguments */
export async function getArgs(args: string[]): Promise<[string[], string[]]> {
const sep = args.indexOf("--");
const lumeArgs = sep === -1 ? args : args.slice(0, sep);
const denoArgs = [
"--unstable",
"-A",
`--no-check`,
];
if (lumeArgs.includes("--quiet")) {
denoArgs.push("--quiet");
}
// Deno flags
const parsedArgs = parse(sep === -1 ? [] : args.slice(sep + 1));
// Regular flags
for (const [name, value] of Object.entries(parsedArgs)) {
switch (name) {
case "_":
case "import-map":
break;
default: {
const flagName = name.length === 1 ? `-${name}` : `--${name}`;
denoArgs.push(value === true ? flagName : `${flagName}=${value}`);
}
}
}
// Merge the import map
denoArgs.push(`--import-map=${await getImportMap(parsedArgs["import-map"])}`);
return [lumeArgs, denoArgs];
}
export default async function main(args: string[]) {
checkDenoVersion();
const [lumeArgs, denoArgs] = await getArgs(args);
const cli = join(baseUrl, "./cli.ts");
const process = Deno.run({
cmd: [
Deno.execPath(),
"run",
...denoArgs,
cli,
...lumeArgs,
],
});
const status = await process.status();
process.close();
if (!status.success) {
addEventListener("unload", () => Deno.exit(1));
}
}
// Run the current command
if (import.meta.main) {
main(Deno.args);
}