/
cli.ts
213 lines (201 loc) · 5.4 KB
/
cli.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { parse } from "https://deno.land/std@0.145.0/flags/mod.ts";
import { bold, dim } from "https://deno.land/std@0.145.0/fmt/colors.ts";
import { cache } from "./cache.ts";
import { VERSION } from "./version.ts";
type DirEntry = {
type: string;
path: string;
};
const denoPermissionFlags = [
"-A",
"--allow-all",
"--allow-env",
"--allow-ffi",
"--allow-hrtime",
"--allow-net",
"--allow-read",
"--allow-run",
"--allow-write",
];
const supportedModuleExts = [
".ts",
".tsx",
".mts",
".js",
".jsx",
".mjs",
];
async function main() {
const { _: args, ...options } = parse(
Deno.args.filter((a) => !denoPermissionFlags.includes(a)),
);
if (args.length == 0) {
console.log(bold("LAND"), VERSION);
console.log(dim("Homepage: "), "https://deno.land/x/land");
console.log(dim("Repo: "), "https://github.com/ije/land");
return;
}
let [moduleName, version] = args.shift()!.toString().split("@");
const versionMetaUrl =
`https://cdn.deno.land/${moduleName}/meta/versions.json`;
const resp1 = await fetch(versionMetaUrl);
if (resp1.status === 404 || resp1.status === 403) {
console.error(`Module "${moduleName}" not found`);
Deno.exit(1);
}
if (resp1.status !== 200) {
console.error(resp1.statusText + ":", versionMetaUrl);
Deno.exit(1);
}
const { latest, versions } = await resp1.json();
if (!version) {
version = latest;
} else if (!versions.includes(version)) {
const v = version;
if (v.startsWith("v")) {
version = v.slice(1);
} else {
version = "v" + v;
}
if (!versions.includes(version)) {
for (const ver of versions) {
if (ver.startsWith(v)) {
version = ver;
break;
} else if (v.startsWith("v") && ver.startsWith(v.slice(1))) {
version = ver;
break;
} else if (!v.startsWith("v") && ver.startsWith("v" + v)) {
version = ver;
break;
}
}
}
if (!versions.includes(version)) {
console.error(`Version "${v}" not found`);
Deno.exit(1);
}
if (version != v) {
console.log(dim(`Found version ${version}`));
}
}
const { content } = await cache(
`https://cdn.deno.land/${moduleName}/versions/${version}/meta/meta.json`,
);
const { directory_listing } = JSON.parse(new TextDecoder().decode(content));
let command: string | null = null;
let importMap: string | null = null;
for (
const name of ["cli", "main", "mod"].map((name) =>
supportedModuleExts.map((ext) => `${name}${ext}`)
).flat()
) {
if (
directory_listing.some((entry: DirEntry) =>
entry.type === "file" && entry.path === `/${name}`
)
) {
command = name;
break;
}
}
for (
const filename of [
"import_map.json",
"import-map.json",
"importMap.json",
"importmap.json",
]
) {
if (
directory_listing.some((entry: DirEntry) =>
entry.type === "file" && entry.path === `/${filename}`
)
) {
importMap = filename;
break;
}
}
if (command === null) {
console.error(`No command entry file found in ${moduleName}`);
Deno.exit(1);
}
const permissionFlags: string[] = [];
const denoFlags: string[] = [];
const appFlags: string[] = [];
for (const f of Deno.args) {
if (denoPermissionFlags.includes(f)) {
permissionFlags.push(f);
}
}
for (const key of Object.keys(options)) {
const value = options[key];
const flagKey = (key.length === 1 ? "-" : "--") + key;
if (flagKey === "--location" && typeof value === "string") {
try {
const url = new URL(value);
denoFlags.push(`--location=${url.toString()}`);
} catch (_e) {
// ignore
}
}
if (value && value !== true) {
appFlags.push(`${flagKey}=${value}`);
} else {
appFlags.push(flagKey);
}
}
if (permissionFlags.length === 0) {
const permissionsFile = directory_listing.find((entry: DirEntry) =>
entry.type === "file" &&
(entry.path === `/PERMISSIONS` || entry.path === `/PERMISSIONS.txt`)
);
if (permissionsFile) {
const { content } = await cache(
`https://cdn.deno.land/${moduleName}/versions/${version}/raw${permissionsFile.path}`,
);
const text = new TextDecoder().decode(content);
const list = text.split("\n").map((line) => {
const value = line.trim();
return denoPermissionFlags.find((p) =>
value === p || "--" + value === p
) || "";
}).filter(Boolean);
permissionFlags.push(...list);
if (permissionFlags.length > 0) {
console.log(dim(`Land Permissions: ${list.join(" ")}`));
}
}
}
if (permissionFlags.length === 0) {
permissionFlags.push("--prompt");
}
if (!denoFlags.some((f) => f.startsWith("--location="))) {
denoFlags.push(`--location=http://0.0.0.0`);
}
if (importMap !== null) {
denoFlags.push(
`--import-map=https://deno.land/x/${moduleName}@${version}/${importMap}`,
);
}
const cmd = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--unstable",
...denoFlags,
...permissionFlags,
`https://deno.land/x/${moduleName}@${version}/${command}`,
...args.map((a: string) => a.toString()),
...appFlags,
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
await cmd.status();
cmd.close();
}
if (import.meta.main) {
main();
}