/
ci.ts
182 lines (158 loc) Β· 5.07 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { encode } from "./deps/base64.ts";
import { parse } from "./deps/flags.ts";
import { cyan, dim, green, red } from "./deps/colors.ts";
import {
checkDenoVersion,
getDenoConfig,
getImportMap,
getLumeVersion,
loadImportMap,
mustNotifyUpgrade,
toUrl,
} from "./core/utils.ts";
import { log } from "./cli/utils.ts";
/**
* This file works as a proxy to the actual Lume CLI to fix the following issues:
* - Add defaults flags to Deno (--unstable, -A, --no-check)
* - Adds user provided flags to Deno (for example --compact)
* - Detect and set the lume --quiet flag in Deno.
* - Detect and use the deno.json file automatically.
* - Add the import-map option to Deno if it's missing
* - Check whether the import_map.json file has the Lume imports.
* - Check whether the Lume version is the latest.
*/
/** Returns the Lume & Deno arguments */
export async function getArgs(
args: string[],
quiet: boolean,
): Promise<[string[], string[]]> {
const sep = args.indexOf("--");
const lumeArgs = sep === -1 ? args : args.slice(0, sep);
const denoArgs = [
"--unstable",
"-A",
`--no-check`,
];
if (quiet) {
denoArgs.push("--quiet");
}
// Flags passed to Deno
const parsedArgs = parse(sep === -1 ? [] : args.slice(sep + 1));
for (const [name, value] of Object.entries(parsedArgs)) {
if (name === "_") {
continue;
}
const flagName = name.length === 1 ? `-${name}` : `--${name}`;
denoArgs.push(value === true ? flagName : `${flagName}=${value}`);
}
// Detect and use the deno.json file automatically
const options = await getDenoConfig();
if (options) {
// To-do: For some reason, this is required in some cases. Needs research.
denoArgs.push(`--config=${options.file}`);
}
// Add the import-map option to Deno if it's missing
const importMapArg = parsedArgs["import-map"] || options?.config.importMap;
const shouldWarn = !quiet &&
!["import-map", "upgrade", "init"].includes(lumeArgs[0]);
// There's a import map file
if (importMapArg) {
const importMapUrl = await toUrl(importMapArg);
const importMap = await loadImportMap(importMapUrl);
if (!importMap.imports["lume/"]) {
// The import map doesn't include Lume imports
shouldWarn && warn(
red("Error:"),
`The import map file ${
dim(importMapArg)
} does not include Lume imports.`,
(importMapArg === "import_map.json")
? `Run ${cyan("lume import-map")} to update import_map.json.`
: "",
);
} else {
// Check whether the import_map.json file has the same lume version as the installed version.
const cliVersion = getLumeVersion();
const mapValue = importMap.imports["lume/"];
const mapVersion = getLumeVersion(
mapValue.startsWith("./")
? new URL(mapValue, importMapUrl)
: new URL(mapValue),
);
if (cliVersion !== mapVersion) {
shouldWarn && warn(
red("Different lume versions mixed:"),
`The import map file ${dim(importMapArg)} imports Lume ${
dim(mapVersion)
}`,
`but CLI version is ${dim(cliVersion)}.`,
(importMapArg === "import_map.json")
? `Run ${
cyan("lume import-map")
} to update import_map.json with your CLI version.`
: undefined,
);
}
}
} else {
// There's no import map file, so we generate one automatically
const importMap = `data:application/json;base64,${
encode(JSON.stringify(await getImportMap()))
}`;
denoArgs.push(`--import-map=${importMap}`);
}
return [lumeArgs, denoArgs];
}
/** Runs the Lume CLI */
export default async function main(args: string[]) {
const denoInfo = checkDenoVersion();
const quiet = args.includes("--quiet");
if (denoInfo) {
warn(
red("Error running Lume"),
`Lume needs Deno ${green(denoInfo.minimum)} or greater`,
`Your current version is ${red(denoInfo.current)}`,
`Run ${cyan(denoInfo.command)} and try again`,
);
Deno.exit(1);
}
const command = args[0];
if (!quiet && command !== "upgrade" && command !== "import-map") {
const info = await mustNotifyUpgrade();
if (info) {
warn(
`Update available ${dim(info.current)} β ${green(info.latest)}`,
`Run ${cyan(info.command)} to update`,
);
}
}
const [lumeArgs, denoArgs] = await getArgs(args, quiet);
const process = Deno.run({
cmd: [
Deno.execPath(),
"run",
...denoArgs,
new URL("./cli.ts", import.meta.url).href,
...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);
}
function warn(...lines: (string | undefined)[]) {
const { args } = Deno;
const syncWarn = args.includes("--serve") || args.includes("-s") ||
args.includes("--watch") || args.includes("-w");
if (syncWarn) {
log(...lines);
} else {
addEventListener("unload", () => log(...lines));
}
}