-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ts
95 lines (80 loc) · 2.79 KB
/
install.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
/**
* strawman - A Deno-based service virtualization solution
* Copyright (C) 2022 Open Formation GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { red } from "https://deno.land/[email protected]/fmt/colors.ts";
const VERSION_META_URL = "https://cdn.deno.land/strawman/meta/versions.json";
export async function checkVersion(desiredVersion: string): Promise<string> {
console.log("Looking up latest version...");
const { latest: latestVersion, versions: availableVersions } =
await (await fetch(VERSION_META_URL)).json();
if (desiredVersion === "latest") {
desiredVersion = latestVersion;
} else if (!availableVersions.includes(desiredVersion)) {
desiredVersion = "v" + desiredVersion;
if (!availableVersions.includes(desiredVersion)) {
console.log(`${red("error")}: version(${desiredVersion}) not found!`);
const shouldListAvailableVersions = prompt(
"List available? [y/n (y = yes, n = no)] ",
);
if (
["y", "yes"].includes(shouldListAvailableVersions?.toLowerCase() ?? "")
) {
for (const release of availableVersions) {
console.log(release, latestVersion === release ? "(latest)" : "");
}
}
Deno.exit(1);
}
}
return desiredVersion;
}
export async function install(
version: string,
): Promise<number | undefined> {
const denoExecPath = Deno.execPath();
const p = Deno.run({
cmd: [
denoExecPath,
"install",
"-A",
"--unstable",
"--no-check",
"--location",
"http://localhost/",
"-n",
"strawman",
"-f",
`https://deno.land/x/strawman@${version}/modules/strawman-cli/cli.ts`,
],
stdout: "inherit",
stderr: "inherit",
});
const status = await p.status();
if (status.success) {
console.log("Strawman was installed successfully");
console.log(`Run 'strawman -h' to get started`);
}
return Deno.exit(status.code);
}
if (import.meta.main) {
const { _: args, ...options } = parse(Deno.args);
const version = await checkVersion(
options.v || options.version || args[0] || "latest",
);
await install(version);
}