|
| 1 | +#!/usr/bin/env -S pkgx deno run --allow-run=bash --allow-read=. |
| 2 | + |
| 3 | +import { join, basename, dirname } from "https://deno.land/std@0.206.0/path/mod.ts"; |
| 4 | +import { walk, exists } from "https://deno.land/std@0.206.0/fs/mod.ts"; |
| 5 | +import * as flags from "https://deno.land/std@0.206.0/flags/mod.ts"; |
| 6 | + |
| 7 | +if (import.meta.main) { |
| 8 | + const args = flags.parse(Deno.args); |
| 9 | + const inputdir = args['input'] |
| 10 | + |
| 11 | + if (!inputdir) { |
| 12 | + console.error(`usage: index.ts --input <path>`); |
| 13 | + Deno.exit(1); |
| 14 | + } |
| 15 | + |
| 16 | + Deno.chdir(inputdir); |
| 17 | + |
| 18 | + const scripts: Script[] = [] |
| 19 | + for await (const slug of iterateGitRepos('.')) { |
| 20 | + console.error(`iterating: ${slug}`); |
| 21 | + scripts.push(...await get_metadata(slug)); |
| 22 | + } |
| 23 | + |
| 24 | + scripts.sort((a, b) => b.birthtime.getTime() - a.birthtime.getTime()); |
| 25 | + |
| 26 | + const categories = (() => { |
| 27 | + const categories: Record<string, number> = {} |
| 28 | + for (const script of scripts) { |
| 29 | + if (script.category && script.description) { |
| 30 | + categories[script.category] ??= 0; |
| 31 | + categories[script.category]++; |
| 32 | + } |
| 33 | + } |
| 34 | + return Object.keys(categories) |
| 35 | + })(); |
| 36 | + |
| 37 | + |
| 38 | + console.log(JSON.stringify({ scripts, categories }, null, 2)); |
| 39 | +} |
| 40 | + |
| 41 | +////////////////////////////////////////////////////////////////////// lib |
| 42 | +async function extractMarkdownSection(filePath: string, sectionTitle: string): Promise<string | undefined> { |
| 43 | + const data = await Deno.readTextFile(filePath); |
| 44 | + const lines = data.split('\n'); |
| 45 | + let capturing = false; |
| 46 | + let sectionContent = ''; |
| 47 | + |
| 48 | + for (const line of lines) { |
| 49 | + if (line.startsWith('## ')) { |
| 50 | + if (capturing) { |
| 51 | + break; // stop if we reach another ## section |
| 52 | + } else if (normalize_title(line.slice(3)) == normalize_title(sectionTitle)) { |
| 53 | + capturing = true; |
| 54 | + } else if (line.slice(3).trim() == mash_title(sectionTitle)) { |
| 55 | + capturing = true; |
| 56 | + } |
| 57 | + } else if (capturing) { |
| 58 | + sectionContent += line + '\n'; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return chuzzle(sectionContent); |
| 63 | + |
| 64 | + function normalize_title(input: string) { |
| 65 | + return input.toLowerCase().replace(/[^a-z0-9]/g, '').trim(); |
| 66 | + } |
| 67 | + |
| 68 | + function mash_title(input: string) { |
| 69 | + const [category, ...name] = input.trim().split('-') |
| 70 | + return `\`mash ${category} ${name.join('-')}\`` |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export interface Script { |
| 75 | + fullname: string |
| 76 | + birthtime: Date |
| 77 | + description?: string |
| 78 | + avatar: string |
| 79 | + url: string |
| 80 | + category?: string |
| 81 | + README?: string |
| 82 | + cmd: string |
| 83 | +} |
| 84 | + |
| 85 | +async function* iterateGitRepos(basePath: string): AsyncIterableIterator<string> { |
| 86 | + for await (const entry of walk(basePath, { maxDepth: 2 })) { |
| 87 | + if (entry.isDirectory && await exists(join(entry.path, '.git'))) { |
| 88 | + yield entry.path; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function chuzzle(ln: string): string | undefined { |
| 94 | + const out = ln.trim() |
| 95 | + return out || undefined; |
| 96 | +} |
| 97 | + |
| 98 | +async function get_metadata(slug: string) { |
| 99 | + |
| 100 | + const cmdString = `git -C '${slug}' log --pretty=format:'%H %aI' --name-only --diff-filter=AR -- scripts`; |
| 101 | + |
| 102 | + const process = Deno.run({ |
| 103 | + cmd: ["bash", "-c", cmdString], |
| 104 | + stdout: "piped" |
| 105 | + }); |
| 106 | + |
| 107 | + const output = new TextDecoder().decode(await process.output()); |
| 108 | + await process.status(); |
| 109 | + process.close(); |
| 110 | + |
| 111 | + const lines = chuzzle(output)?.split('\n') ?? []; |
| 112 | + const rv: Script[] = [] |
| 113 | + let currentCommitDate: string | undefined; |
| 114 | + |
| 115 | + for (let line of lines) { |
| 116 | + line = line.trim() |
| 117 | + |
| 118 | + if (line.includes(' ')) { // Detect lines with commit hash and date |
| 119 | + currentCommitDate = line.split(' ')[1]; |
| 120 | + } else if (line && currentCommitDate) { |
| 121 | + const filename = join(slug, line) |
| 122 | + if (!await exists(filename)) { |
| 123 | + // the file used to exist but has been deleted |
| 124 | + console.warn("skipping deleted: ", filename, line) |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + console.error(line) |
| 129 | + |
| 130 | + const repo_metadata = JSON.parse(await Deno.readTextFile(join(slug, 'metadata.json'))) |
| 131 | + |
| 132 | + const README = await extractMarkdownSection(join(slug, 'README.md'), basename(filename)); |
| 133 | + const birthtime = new Date(currentCommitDate!); |
| 134 | + const avatar = repo_metadata.avatar |
| 135 | + const fullname = join(dirname(slug), ...stem(filename)) |
| 136 | + const url = repo_metadata.url +'/scripts/' + basename(filename) |
| 137 | + const category = (([x, y]) => x?.length > 0 && y ? x : undefined)(basename(filename).split("-")) |
| 138 | + const description = README ? extract_description(README) : undefined |
| 139 | + const cmd = category ? `mash ${category} ${basename(filename).split('-').slice(1).join('-')}` : `mash ${fullname}` |
| 140 | + |
| 141 | + rv.push({ fullname, birthtime, description, avatar, url, category, README, cmd }) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return rv; |
| 146 | + |
| 147 | + function stem(filename: string): string[] { |
| 148 | + const base = basename(filename) |
| 149 | + const parts = base.split('.') |
| 150 | + if (parts.length == 1) { |
| 151 | + return parts.slice(0, 1) |
| 152 | + } else { |
| 153 | + return parts.slice(0, -1) // no extension, but allow eg. foo.bar.js to be foo.bar |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +function extract_description(input: string) { |
| 159 | + const regex = /^(.*?)\n#|^.*$/ms; |
| 160 | + const match = regex.exec(input); |
| 161 | + return match?.[1]?.trim(); |
| 162 | +} |
0 commit comments