-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
731 lines (689 loc) · 19.9 KB
/
index.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
const readline = require("readline");
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const os = require("os");
let tmpdir;
const OSC = "\u001B]";
const SEP = ";";
const BEL = "\u0007";
const link = (text, url) =>
[OSC, "8", SEP, SEP, url, BEL, text, OSC, "8", SEP, SEP, BEL].join("");
const HELP = `justinstall <github-url|file-url|local-file>\n\tv1.0.0 - Just install anything. Supports .tar.gz, .zip, .dmg, .app, .pkg, and .deb files. Binaries will be installed to ~/.local/bin.\n\n\tExample:\n\t\tjustinstall atuinsh/atuin\n\t\tjustinstall https://github.com/junegunn/fzf/\n\t\tjustinstall https://dl.google.com/chrome/mac/universal/stable/GGRO/googlechrome.dmg\n\t\tjustinstall tailscale.pkg\n\n\tMade by ${link(
"Explosion-Scratch",
"https://github.com/explosion-scratch"
)}`;
async function main() {
const INSTALL_SNIPPET_REPLACEMENTS = [
[/^\$ +/, ""],
["npm install", "pnpm i"],
["yarn install", "pnpm i"],
];
const log = {
debug: (message) =>
console.log(`${colors.fg.cyan}${message}${colors.reset}`),
log: (message) =>
console.log(`${colors.fg.white}${message}${colors.reset}`),
error: (message) =>
console.error(`${colors.fg.red}${message}${colors.reset}`),
warn: (message) =>
console.warn(`${colors.fg.yellow}${message}${colors.reset}`),
};
let args = process.argv.slice(2);
if (args.find((i) => ["-h", "--help"].includes(i))) {
return log.log(HELP);
}
if (!args[0]) {
return log.log(HELP);
}
let isFile = fs.existsSync(args[0]);
let isURL =
/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/.test(
args[0]
);
if (!isFile && !isURL && /^[a-z_\-0-9]+\/[a-z_\-0-9]+$/.test(args[0])) {
args[0] = `https://github.com/${args[0]}`;
}
let filepath = isFile ? path.resolve(args[0]) : null;
if (isFile) {
log.debug("Installing " + filepath + " from local files");
}
// Declare some stuff
tmpdir = execSync("mktemp -d").toString().trim();
const saveURL = ({ isURL, isFile, url, path }) => {
const CONFIG_PATH = resolve(
os.homedir(),
".config",
"justinstall",
"installations.json"
);
fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
let existing;
try {
existing = JSON.parse(fs.readFileSync(CONFIG_PATH));
} catch (e) {
existing = [];
}
existing.push({
date: new Date().toString(),
isURL,
isFile,
url,
});
};
if (isURL && !args[0].includes("://github.com")) {
log.debug("Downloading file from URL...");
let origin;
try {
origin = new URL(args[0]).origin;
} catch (e) {}
let response = await fetch(args[0], {
headers: {
...(origin && { origin }),
},
referrer: `https://${origin}` || args[0],
});
const disposition = response.headers.get("Content-Disposition");
const filenameRegex = /filename="?([^;"]+)"/;
const match = disposition && disposition.match(filenameRegex);
let filename =
match?.[1] || args[0].substring(args[0].lastIndexOf("/") + 1);
const dest = path.join(tmpdir, filename);
await response
.arrayBuffer()
.then((ab) => fs.writeFileSync(dest, Buffer.from(ab)));
log.debug("Downloaded " + filename);
isFile = true;
filepath = dest;
}
let selected;
// OS detection stuff:
const arch = process.arch;
const platform = process.platform;
log.debug(`Detected ${arch} on ${platform}`);
let arch_aliases = {
arm64: ["arm64", "arm", "aarch", "aarch64", "aar64", "silicon"],
x64: ["x64", "intel", "x86_64"],
universal: ["universal", "all"],
};
let boosters = {
darwin: ["pkg", "dmg"],
linux: ["AppImage"],
};
let platform_aliases = {
darwin: ["darwin", "osx", "macos", "mac", "apple"],
linux: ["linux"],
freebsd: ["freebsd", "linux"],
openbsd: ["openbsd", "linux"],
win32: ["win32", "win", "windows"],
universal: arch_aliases.universal,
};
const my_arch = [
arch,
...(platform === "darwin" ? ["m1", "m2", "m3"] : []),
...(arch_aliases[arch] || arch_aliases.universal),
];
const my_platform = [
platform,
...(platform_aliases[platform] || platform_aliases.universal),
];
const incl = (a, b) => a.find((i) => b.includes(i));
const ebool = (cmd) => {
try {
return execSync(cmd, { stdio: "ignore" }).length > 0;
} catch (e) {
return false;
}
};
let canInstall = {
// Regular linux
deb: process.platform === "linux" && ebool("which apt").length > 0,
// Mac
dmg: process.platform === "darwin",
pkg: process.platform === "darwin",
app: process.platform === "darwin",
// Arch linux
rpm: ebool("which dnf").length > 0,
"tar.zst": ebool("which pacman").length > 0,
};
const possible = {
platforms: Object.values(platform_aliases).flat(),
arches: Object.values(arch_aliases).flat(),
};
const IGNORE = [
"Applications",
"checksums",
"release_notes",
"readme",
"license",
".txt",
"__MACOSX",
".background",
".keystone_install",
".VolumeIcon.icns",
".DS_Store",
"CHANGELOG",
"LICENSE",
];
const isIgnored = (filename) => {
return IGNORE.find((p) => filename.toLowerCase().includes(p.toLowerCase()));
};
if (!isFile) {
const [owner, repo] = args[0]
.replace(/(?:https?\:\/\/)?github\.com\//, "")
.split("/");
let body;
let assets = await fetch(
`https://api.github.com/repos/${owner}/${repo}/releases/latest`
)
.then((r) => r.json())
.then((a) => ((body = a.body), a))
.then((j) => j.assets)
.then((a) => (log.debug("Found " + a.length + " assets"), a))
.then((assets) => {
return assets.filter((asset) => !isIgnored(asset.name));
});
assets = assets.map((i) => ({
...i,
segments: i.name.split(/[_ \.\-]/).map((j) => j.toLowerCase()),
}));
assets = assets.map((i) => ({
...i,
compatiblePlatforms: i.segments.filter((j) =>
possible.platforms.includes(j)
),
compatibleArches: i.segments.filter((j) => possible.arches.includes(j)),
extension: getExtension(i.name),
}));
let code = getCode(body)?.trim();
const isInstaller = (code) => {
if (!code) {
return false;
}
code = code.toLowerCase().trim();
// Looking for a one-to-three-liner e.g. pnpm i -g thing or sudo apt install package
if (code.split("\n").length > 3) {
return false;
}
if (
code.includes("installing") ||
code.includes("](#") ||
code.includes("](http") ||
code.startsWith("- [")
) {
return false;
}
return (
code.includes("| sh") ||
code.includes("| bash") ||
code.includes("curl ") ||
code.includes("wget ") ||
code.includes("install") ||
code.includes("setup") ||
code.includes("installer.sh") ||
code.includes(".sh") ||
code.includes("sudo emerge")
);
};
if (code && isInstaller(code)) {
log.debug(
"Found installer code in release notes:\n\n\t" +
colors.fg.green +
code +
colors.reset +
"\n"
);
if (
await confirm(
"Run install script (y) or continue to regular installation (n)?"
)
) {
log.debug("Running...");
execSync(code);
return;
}
} else {
// Assuming branch is main - yikes
const readme = await fetch(
`https://raw.githubusercontent.com/${owner}/${repo}/main/README.md`
).then((r) => r.text());
const re =
/(?:(?:```(?<lang>\w*)?\n(?<code>[\s\S]*?)```)|(?:\s+\n(?:\t| +)(?<code2>[\s\S]*?)\n))/;
const codeBlocks = readme
.match(new RegExp(re, "g"))
?.filter(Boolean)
.map(
(code) =>
code?.match(re)?.groups?.code || code?.match(re)?.groups?.code2
)
.map((i) => i?.trim())
.filter(Boolean)
.filter(isInstaller);
if (codeBlocks?.length) {
let b = codeBlocks[0].trim();
for (let [k, v] of INSTALL_SNIPPET_REPLACEMENTS) {
b = b.replace(k, v);
}
log.log(
`Found possible install snippet in README:\n\n\t${colors.fg.green}${b}${colors.reset}\n`
);
if (
await confirm(
"Run script in shell (y) or proceed to normal installation (n)?",
"n"
)
) {
log.debug("Running script...");
execSync(b);
log.debug("Done installing");
return;
} else {
log.debug("Continuing to regular installation.");
}
}
}
if (!assets?.length) {
throw new Error("No assets in releases");
}
let installerScript = assets.find((i) => i.name.includes("installer.sh"));
if (installerScript) {
log.debug(`Found installer script: ${installerScript.name}`);
await fetch(installerScript.browser_download_url)
.then((r) => r.arrayBuffer())
.then((ab) =>
fs.writeFileSync(path.join(tmpdir, "installer.sh"), Buffer.from(ab))
);
execSync(`chmod +x ${JSON.stringify(path.join(tmpdir, "installer.sh"))}`);
if (await confirm(`Run install script? ${tmpdir}/installer.sh`)) {
execSync(`${tmpdir}/installer.sh`);
return;
}
// Continue to regular installation
}
let compat = assets
.map((asset) => {
asset.points = 0;
if (boosters[platform]) {
for (let i of boosters[platform]) {
if (asset.name.includes(i)) {
asset.points += 0.1;
}
}
}
if (asset.compatiblePlatforms.length) {
if (incl(asset.compatiblePlatforms, my_platform)) {
asset.points += 1;
} else {
asset.delete = true;
}
}
if (asset.compatibleArches.length) {
if (incl(asset.compatibleArches, my_arch)) {
asset.points += 1;
} else {
asset.delete = true;
}
}
if (canInstall[asset.extension] === false) {
asset.delete = true;
}
if (asset.segments.includes("pkgbuild") && !canInstall["tar.zst"]) {
asset.delete = true;
}
return asset;
})
.filter((i) => !(i.delete == true))
.sort((a, b) => b.points - a.points);
if (!compat?.length) {
return log.error(
"Couldn't find a binary, you'll have to figure it out yourself 🤷:\n" +
`${colors.fg.green}https://github.com/${owner}/${repo}${colors.reset}`
);
}
selected = compat[0];
} else {
selected = {
name: path.basename(filepath),
size: fs.statSync(filepath).size,
extension: getExtension(path.basename(filepath)),
};
}
if (!selected) {
return log.error("Couldn't find anything to install");
}
const shouldInstall = await confirm(
`Ok to install ${selected.name} (${fileSize(selected.size)})?`
);
if (!shouldInstall) {
return log.error("Aborted install");
}
process.chdir(tmpdir);
log.debug(`Downloading ${selected.name}`);
const checkPath = async (p) => {
if (fs.existsSync(path.resolve(p))) {
if (!(await confirm(`Overwrite ${p}?`))) {
throw new Error("Aborted overwrite of file " + p);
} else {
try {
log.debug("Removing " + p);
fs.rmSync(p, { recursive: true, force: true });
} catch (e) {}
}
}
};
const getPath = (filename) => path.join(tmpdir, filename);
const getName = (sel) =>
sel.name
.replace(sel.extension, "")
.replace(/v?[0-9]+\.[0-9]+\.[0-9]+/i, "")
.replace(
new RegExp(
`(?:${[...possible.platforms, ...possible.arches].join("|")})`,
"gi"
),
""
)
.replace(/[ _\-\.]+$/, "")
.replace(/^[ _\-\.]+/, "");
const mountDMG = (p) => {
log.debug("Mounting DMG", p);
execSync(
`hdiutil attach ${JSON.stringify(
p
)} -nobrowse -mountpoint ${JSON.stringify(OUTPUT_DIR)}`
);
};
const installPkg = (p) => {
if (!canInstall.pkg) {
throw new Error("Can't install .pkg files in this environment");
}
log.debug("Installing pkg...");
execSync(`sudo installer -pkg ${JSON.stringify(p)} -target /`);
log.debug(`Installed pkg ${path.basename(p)} to /`);
};
let newpath;
if (isFile) {
newpath = path.resolve(tmpdir, path.basename(filepath));
if (filepath !== newpath) {
fs.cpSync(filepath, newpath);
}
}
const p = newpath || getPath(selected.name);
if (!isFile) {
await fetch(selected.browser_download_url)
.then((r) => r.arrayBuffer())
.then((a) => (log.debug(`Downloaded, writing to ${p}`), a))
.then((b) => fs.writeFileSync(p, Buffer.from(b)));
}
log.log(`Preinstall: ${selected.name}`);
const OUTPUT_DIR = path.join(tmpdir, "outputs");
fs.mkdirSync(OUTPUT_DIR);
let isBinary = true;
if (selected.extension === "tar.gz") {
log.debug("Unzipping TAR");
execSync(
`tar -xzf ${JSON.stringify(p)} --directory ${JSON.stringify(OUTPUT_DIR)}`
);
} else if (selected.extension === "pkg") {
installPkg(p);
} else if (selected.extension === "zip") {
log.debug("Unzipping ZIP");
execSync(`unzip ${JSON.stringify(p)} -d ${JSON.stringify(OUTPUT_DIR)}`);
} else if (selected.extension === "dmg") {
isBinary = false;
mountDMG(p);
} else if (selected.extension === "deb") {
isBinary = false;
} else if (!selected.extension) {
fs.renameSync(p, path.join(OUTPUT_DIR, selected.name));
}
const getBinaries = (dir) => {
const allFiles = execSync(
`find ${JSON.stringify(path.resolve(dir))} -type f ! -size 0`
)
.toString()
.split("\n")
.filter(Boolean)
.map((i) => i.replace(path.resolve(dir) + path.sep, ""));
let out = [];
const ALLOWED = [
".deb",
".rpm",
".dmg",
".pkg",
".app",
".zip",
".gz",
".gz",
];
out.push(
...allFiles.filter((i) =>
ALLOWED.find((j) => i.toLowerCase().endsWith(j))
)
);
const isExecutable = (f) => {
return execSync(`file ${JSON.stringify(path.resolve(dir, f))}`).includes(
"executable"
);
};
out.push(...allFiles.filter(isExecutable));
return out;
};
let binaries = getBinaries(OUTPUT_DIR);
const _dmg = binaries.find((f) => f.endsWith(".dmg"));
if (_dmg) {
isBinary = false;
mountDMG(path.resolve(OUTPUT_DIR, _dmg));
}
// Do after possible DMG mounted
binaries = getBinaries(OUTPUT_DIR);
const _app = binaries.find((f) => f.endsWith(".app"));
const _pkg = binaries.find((f) => f.endsWith(".pkg"));
if (
!(await confirm(
`Continue installation of ${
binaries.length ? binaries.join(",") : selected.name
}?`
))
) {
return log.error("Aborted");
}
const installApp = async (_app) => {
if (!canInstall.app) {
return log.error("Cannot install .app package");
}
log.debug("Found mac .app package, installing...");
const dest = path.join("/Applications", _app);
await checkPath(dest);
log.debug("Writing files...");
fs.cpSync(path.join(OUTPUT_DIR, _app), dest, {
recursive: true,
});
log.debug("Code signing app...");
try {
execSync(
`codesign --sign - --force --deep ${JSON.stringify(dest)} 2> /dev/null`
);
} catch (e) {
log.warn("Codesigning failed");
}
log.debug("Removing quarantine from app...");
try {
execSync(
`xattr -rd com.apple.quarantine ${JSON.stringify(dest)} 2> /dev/null`
);
} catch (e) {
log.warn("Dequarantining failed");
}
try {
execSync(`hdiutil eject ${JSON.stringify(OUTPUT_DIR)} 2> /dev/null`, {
stdio: false,
});
} catch (e) {}
execSync("sleep 0.5");
if (await confirm("Open app " + path.basename(dest))) {
execSync(`open -n ${JSON.stringify(dest)}`);
}
};
if (_app) {
isBinary = false;
await installApp(_app);
return;
}
if (_pkg) {
isBinary = false;
installPkg(path.resolve(OUTPUT_DIR, _pkg));
return;
}
if (isBinary) {
if (!binaries.length) {
return log.error("No binaries found");
}
log.debug("Got binaries: ", binaries.join("\n"));
log.debug("Renaming binaries...");
for (const bin of binaries) {
fs.renameSync(
path.join(OUTPUT_DIR, bin),
path.join(OUTPUT_DIR, getName(selected))
);
}
}
if (isBinary) {
log.debug("Installing binary");
for (let bin of getBinaries(OUTPUT_DIR)) {
const dest = path.join(os.homedir(), ".local", "bin", path.basename(bin));
await checkPath(dest);
log.debug(`Installing binary: "${bin}" -> "${dest}"`);
execSync(`chmod +x ${JSON.stringify(path.join(OUTPUT_DIR, bin))}`);
fs.cpSync(path.join(OUTPUT_DIR, bin), dest);
}
} else if (selected.extension === "deb") {
log.debug("Installing DEB");
execSync(`dpkg -i ${JSON.stringify(p)} ${JSON.stringify(OUTPUT_DIR)}`);
}
log.log("Done:", binaries.join("\n"));
}
function fileSize(bytes, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = si
? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
: ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let u = -1;
const r = 10 ** dp;
do {
bytes /= thresh;
++u;
} while (
Math.round(Math.abs(bytes) * r) / r >= thresh &&
u < units.length - 1
);
return bytes.toFixed(dp) + " " + units[u];
}
function getExtension(filename) {
const a = () => {
let s = filename.split(".");
if (s.length === 1) {
return "";
}
if (s.length > 2) {
return s.slice(-2).join(".");
}
return s.slice(-1)[0];
};
// Ends of versions
let out = a()
.split(".")
.filter((i) => !parseInt(i, 10))
.join(".");
if (!["tar.gz"].includes(out)) {
return out.split(".").slice(-1)[0];
}
return out;
}
function getCode(markdown) {
// ```(?:\w*\n)?([\s\S]*?)```
const regex = /```(?:\w*\n)?([\s\S]*?)```/g;
let match;
let codeBlocks = "";
while ((match = regex.exec(markdown)) !== null) {
codeBlocks += match[1] + "\n";
}
return codeBlocks;
}
function confirm(question, defaultAnswer = "y") {
return new Promise((resolve) => {
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
interface.question(
`${colors.fg.yellow}${question}${colors.reset} ${colors.fg.blue}(${
defaultAnswer.toLowerCase() == "y" ? "Y" : "y"
}/${defaultAnswer.toLowerCase() == "n" ? "N" : "n"})${colors.reset} `,
(ans) => {
let result =
ans.trim().toLowerCase() === "y" ||
ans.trim().toLowerCase() === "yes";
if (defaultAnswer === "y" && ans.trim() === "") result = true;
interface.close();
resolve(result);
}
);
});
}
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
fg: {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
gray: "\x1b[90m",
crimson: "\x1b[38m", // Scarlet
},
bg: {
black: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m",
gray: "\x1b[100m",
crimson: "\x1b[48m",
},
};
const cleanup = () => {
return;
if (tmpdir) {
console.log("Cleaning up...");
fs.rmSync(tmpdir, { recursive: true });
console.log("Done");
}
};
(async () => {
process.on("SIGINT", () => {
cleanup();
process.exit(0);
});
try {
await main();
} catch (e) {
console.log("Error: " + e.stack);
}
cleanup();
})();