-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·171 lines (155 loc) · 3.76 KB
/
bin.js
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
#!/usr/bin/env node
import { readFile } from "node:fs/promises";
import inquirer from "inquirer";
import chalk from "chalk";
import semver from "semver";
import sourceMappingURL from "source-map-url";
import { config } from "./lib/config/index.js";
import { getSourceCodeMapUrl } from "./lib/getSourceCodeMapUrl/index.js";
import { loader } from "./lib/loader/index.js";
import { update } from "./lib/update/index.js";
const { satisfies } = semver;
const { bold, red } = chalk;
const { prompt } = inquirer;
start();
async function start() {
const packageJson = await readFile(
new URL("./package.json", import.meta.url),
"utf8",
);
const {
name,
version,
homepage,
dependencies: {},
engines: { node },
bugs: { url: bugUrl },
} = JSON.parse(packageJson);
if (!satisfies(process.version, node)) {
console.error(
new Error(
`This node version (${process.version}) is not supported (${node}).`,
),
);
process.exit(1);
}
try {
const { colombo } = await import("./index.js");
// Lazy call to update. Intentional race condition.
update({ name, version })
.then(
(message) => message && process.on("exit", () => console.log(message)),
)
.catch(() => null);
const { headers, help, showVersion, arg, configured } = await config(
process.argv,
);
if (help) {
console.log(await readFile(new URL("./man", import.meta.url), "utf8"));
}
if (showVersion) {
console.log([name, version].join("@"));
}
if (configured || help || showVersion) {
process.exit(0);
}
const { file } = await prompt([
{
name: "file",
message: "File URL (optional)",
type: "input",
default: arg,
},
]);
const match = file.match(/:(?<line>\d+):(?<column>\d+)$/);
const { groups = { line: undefined, column: undefined, file } } =
match || {};
const clean = file.replace(/:\d+:\d+$/, "").replace(/\?.*/, "");
let url;
if (!headers.has("User-Agent")) {
headers.set(
"User-Agent",
`node (compatible; Colombo/${version}; bot; +${homepage})`,
);
}
if (clean) {
loader.start("Load file");
const response = await fetch(clean, { headers });
if (!response.ok) {
throw new Error(
`Failed to load file ${clean}: ${response.status} ${response.statusText}`,
);
}
const file =
response.headers.get("SourceMap") ||
sourceMappingURL.getFrom(await response.text());
loader.end();
const mapUrl = getSourceCodeMapUrl(file, clean);
if (mapUrl) {
({ url } = await prompt([
{
name: "url",
message: "Source map (found)",
type: "input",
default: mapUrl,
validate: Boolean,
},
]));
} else {
({ url } = await prompt([
{
name: "url",
message: "Source map (assumed)",
type: "input",
default: clean + ".map",
validate: Boolean,
},
]));
}
} else {
({ url } = await prompt([
{
name: "url",
message: "Source map",
type: "input",
validate: Boolean,
},
]));
}
if (!url) {
throw new Error("Source-map is a must");
}
const { column, line } = await prompt([
{
name: "line",
message: "Line number",
type: "number",
default: groups.line || 1,
},
{
name: "column",
message: "Column number",
type: "number",
default: groups.column || 1,
},
]);
loader.start("Load source map");
const result = await colombo({ url, line, column });
loader.end();
console.log(result);
process.exit(0);
} catch (error) {
loader.end();
console.log("\n");
if (typeof error.toJSON === "function") {
console.error(error.toJSON());
process.exit(1);
}
error.message = red(error.message);
console.error(error);
console.log(`
I can see you were not successful. Feel free to ${bold("submit an issue")}
${bugUrl}`);
process.exit(1);
}
}