|
1 | | -#!/usr/bin/env -S pkgx deno run -A |
| 1 | +#!/usr/bin/env -S pkgx deno^1 run -A |
2 | 2 |
|
3 | | -import { Path } from "pkgx" |
4 | | -import undent from "outdent" |
| 3 | +import { Path } from "pkgx"; |
| 4 | +import undent from "outdent"; |
5 | 5 |
|
6 | 6 | const has_shebang = (() => { |
7 | | - const encoder = new TextDecoder() |
| 7 | + const encoder = new TextDecoder(); |
8 | 8 | return (buf: Uint8Array) => { |
9 | | - return encoder.decode(buf) == '#!' |
10 | | - } |
11 | | -})() |
| 9 | + return encoder.decode(buf) == "#!"; |
| 10 | + }; |
| 11 | +})(); |
12 | 12 |
|
13 | 13 | for (const path of Deno.args) { |
14 | | - if (!Path.cwd().join(path).isFile()) continue |
| 14 | + if (!Path.cwd().join(path).isFile()) continue; |
15 | 15 |
|
16 | | - console.debug({ path }) |
| 16 | + console.debug({ path }); |
17 | 17 |
|
18 | | - const rid = await Deno.open(path, { read: true }) |
| 18 | + const rid = await Deno.open(path, { read: true }); |
19 | 19 | try { |
20 | | - const buf = new Uint8Array(2) |
21 | | - await rid.read(buf) |
22 | | - if (!has_shebang(buf)) continue |
| 20 | + const buf = new Uint8Array(2); |
| 21 | + await rid.read(buf); |
| 22 | + if (!has_shebang(buf)) continue; |
23 | 23 | } finally { |
24 | | - rid.close() |
| 24 | + rid.close(); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | //FIXME this could be pretty damn efficient if we can find the time |
28 | 28 | //NOTE as it stands this is HIDEOUSLY inefficient |
29 | 29 |
|
30 | | - const contents = await Deno.readFile(path) |
31 | | - const txt = new TextDecoder().decode(contents) |
32 | | - const [line0, ...lines] = txt.split("\n") //lol |
| 30 | + const contents = await Deno.readFile(path); |
| 31 | + const txt = new TextDecoder().decode(contents); |
| 32 | + const [line0, ...lines] = txt.split("\n"); //lol |
33 | 33 |
|
34 | | - const match = line0.match(/^#!\s*(\/[^\s]+)/) |
35 | | - if (!match) throw new Error() |
36 | | - const interpreter = match[1] |
| 34 | + const match = line0.match(/^#!\s*(\/[^\s]+)/); |
| 35 | + if (!match) throw new Error(); |
| 36 | + const interpreter = match[1]; |
37 | 37 |
|
38 | 38 | switch (interpreter) { |
39 | | - case "/usr/bin/env": |
40 | | - case "/bin/sh": |
41 | | - console.log({ line0, path }) |
42 | | - console.log("^^ skipped acceptable shebang") |
43 | | - continue |
| 39 | + case "/usr/bin/env": |
| 40 | + case "/bin/sh": |
| 41 | + console.log({ line0, path }); |
| 42 | + console.log("^^ skipped acceptable shebang"); |
| 43 | + continue; |
44 | 44 | } |
45 | 45 |
|
46 | | - const shebang = `#!/usr/bin/env ${new Path(interpreter).basename()}` |
| 46 | + const shebang = `#!/usr/bin/env ${new Path(interpreter).basename()}`; |
47 | 47 |
|
48 | 48 | const rewrite = undent` |
49 | 49 | ${shebang} |
50 | 50 | ${lines.join("\n")} |
51 | | - ` |
| 51 | + `; |
52 | 52 |
|
53 | | - console.log({rewrote: path, to: `#!/usr/bin/env ${interpreter}`}) |
| 53 | + console.log({ rewrote: path, to: `#!/usr/bin/env ${interpreter}` }); |
54 | 54 |
|
55 | | - const stat = Deno.lstatSync(path) |
56 | | - const needs_chmod = stat.mode && !(stat.mode & 0o200) |
57 | | - if (needs_chmod) Deno.chmodSync(path, 0o666) |
58 | | - await Deno.writeFile(path, new TextEncoder().encode(rewrite)) |
59 | | - if (needs_chmod) Deno.chmodSync(path, stat.mode!) |
| 55 | + const stat = Deno.lstatSync(path); |
| 56 | + const needs_chmod = stat.mode && !(stat.mode & 0o200); |
| 57 | + if (needs_chmod) Deno.chmodSync(path, 0o666); |
| 58 | + await Deno.writeFile(path, new TextEncoder().encode(rewrite)); |
| 59 | + if (needs_chmod) Deno.chmodSync(path, stat.mode!); |
60 | 60 | } |
0 commit comments