|
1 | 1 | #!/usr/bin/env -S eval-wsl deno run --quiet --no-config --no-lock --no-npm --no-remote --cached-only |
2 | 2 | // deno-lint-ignore-file no-explicit-any |
3 | 3 |
|
4 | | -// parse stdin |
5 | | -const json = await new Response(Deno.stdin.readable).json() |
6 | | - |
7 | | -// prepare result |
8 | | -const names = new Set<string>() |
9 | | - |
10 | | -// type |
11 | | -const type: 'cask' | 'formula' | 'tap' | '' = Deno.args.includes('--type=cask') |
12 | | - ? 'cask' |
13 | | - : Deno.args.includes('--type=formula') |
14 | | - ? 'formula' |
15 | | - : Deno.args.includes('--type=tap') |
16 | | - ? 'tap' |
17 | | - : '' |
18 | | - |
19 | | -// add taps if desired |
20 | | -if (['tap'].includes(type)) { |
21 | | - // all taps are by request |
22 | | - const taps = json || [] |
23 | | - for (const tap of taps.filter((i: any) => i.installed)) { |
24 | | - names.add(tap.name) |
25 | | - } |
| 4 | +import { heredoc, HelpError, exitWithError, wantsHelp } from '../sources/ts.ts' |
| 5 | + |
| 6 | +class UsageError extends HelpError { |
| 7 | + override help = heredoc` |
| 8 | + USAGE: |
| 9 | + \`brew-installed.ts [--type=<cask|formula|tap>] [--requested] <brew.json\` |
| 10 | + ` |
26 | 11 | } |
27 | 12 |
|
28 | | -// add casks if desired |
29 | | -if (['', 'cask'].includes(type)) { |
30 | | - // all casks are by request |
31 | | - const casks = json.casks || [] |
32 | | - for (const cask of casks.filter((i: any) => i.installed)) { |
33 | | - names.add(cask.full_token) |
| 13 | +async function main(...args: string[]) { |
| 14 | + // parse stdin |
| 15 | + const json = await new Response(Deno.stdin.readable).json() |
| 16 | + |
| 17 | + // prepare result |
| 18 | + const names = new Set<string>() |
| 19 | + |
| 20 | + // type |
| 21 | + const type: 'cask' | 'formula' | 'tap' | '' = args.includes('--type=cask') |
| 22 | + ? 'cask' |
| 23 | + : args.includes('--type=formula') |
| 24 | + ? 'formula' |
| 25 | + : args.includes('--type=tap') |
| 26 | + ? 'tap' |
| 27 | + : '' |
| 28 | + |
| 29 | + // add taps if desired |
| 30 | + if (['tap'].includes(type)) { |
| 31 | + // all taps are by request |
| 32 | + const taps = json || [] |
| 33 | + for (const tap of taps.filter((i: any) => i.installed)) { |
| 34 | + names.add(tap.name) |
| 35 | + } |
34 | 36 | } |
35 | | -} |
36 | 37 |
|
37 | | -// add formulas if desired |
38 | | -if (['', 'formula'].includes(type)) { |
39 | | - // formulas could be by request, or by dependency |
40 | | - const formulae = json.formulae || [] |
41 | | - if (Deno.args.includes('--requested')) { |
42 | | - // only requested formula |
43 | | - for (const formula of formulae) { |
44 | | - if (formula.installed.find((i: any) => i.installed_on_request)) { |
| 38 | + // add casks if desired |
| 39 | + if (['', 'cask'].includes(type)) { |
| 40 | + // all casks are by request |
| 41 | + const casks = json.casks || [] |
| 42 | + for (const cask of casks.filter((i: any) => i.installed)) { |
| 43 | + names.add(cask.full_token) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // add formulas if desired |
| 48 | + if (['', 'formula'].includes(type)) { |
| 49 | + // formulas could be by request, or by dependency |
| 50 | + const formulae = json.formulae || [] |
| 51 | + if (args.includes('--requested')) { |
| 52 | + // only requested formula |
| 53 | + for (const formula of formulae) { |
| 54 | + if (formula.installed.find((i: any) => i.installed_on_request)) { |
| 55 | + names.add(formula.full_name) |
| 56 | + } |
| 57 | + } |
| 58 | + } else { |
| 59 | + // all formula |
| 60 | + for (const formula of formulae.filter((i: any) => i.installed)) { |
45 | 61 | names.add(formula.full_name) |
46 | 62 | } |
47 | 63 | } |
48 | | - } else { |
49 | | - // all formula |
50 | | - for (const formula of formulae.filter((i: any) => i.installed)) { |
51 | | - names.add(formula.full_name) |
52 | | - } |
| 64 | + } |
| 65 | + |
| 66 | + // output result |
| 67 | + if (names.size) { |
| 68 | + await Deno.stdout.write( |
| 69 | + new TextEncoder().encode(Array.from(names).sort().concat('').join('\n')), |
| 70 | + ) |
53 | 71 | } |
54 | 72 | } |
55 | 73 |
|
56 | | -// output result |
57 | | -if (names.size) { |
58 | | - await Deno.stdout.write( |
59 | | - new TextEncoder().encode(Array.from(names).sort().concat('').join('\n')), |
60 | | - ) |
| 74 | +try { |
| 75 | + if (wantsHelp(Deno.args)) { |
| 76 | + throw new UsageError() |
| 77 | + } |
| 78 | + await main(...Deno.args) |
| 79 | +} catch (error) { |
| 80 | + await exitWithError(error) |
61 | 81 | } |
0 commit comments