|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('node:fs/promises') |
| 4 | +const process = require('node:process') |
| 5 | + |
| 6 | +const help =`Usage: lbf-list-modules-typescript <package-name>=<directory>... |
| 7 | +
|
| 8 | +Description: |
| 9 | + Given a sequence of \`pkg-name1=dir1\`, ..., \`pkg-nameN=dirN\`, outputs |
| 10 | + a JSON object on stdout of the form |
| 11 | + \`\`\` |
| 12 | + { |
| 13 | + "pkg-name1": lbfs1, |
| 14 | + ... |
| 15 | + "pkg-nameN": lbfsN |
| 16 | + } |
| 17 | + \`\`\` |
| 18 | + where for every \`i\`, each \`lbfi\` is a list of all paths ending with \`*.lbf\` in \`diri\` |
| 19 | + except the \`*.lbf\` suffix is removed and all \`/\`s are replaced with \`.\`. |
| 20 | +
|
| 21 | + When there are no arguments, this returns the empty JSON object. |
| 22 | +
|
| 23 | +Examples: |
| 24 | + Suppose there is a directory as follows. |
| 25 | + |-A.lbf |
| 26 | + \`-Directory |
| 27 | + |-B.lbf |
| 28 | + \`-C.lbf |
| 29 | + Then, \`lbf-list-modules-typescript .\` returns |
| 30 | + \`\`\` |
| 31 | + A |
| 32 | + Directory.B |
| 33 | + Directory.C |
| 34 | + \`\`\`` |
| 35 | + |
| 36 | +async function main() { |
| 37 | + const argv = process.argv.slice(2); |
| 38 | + |
| 39 | + const result = { } |
| 40 | + |
| 41 | + for (const pkgDir of argv) { |
| 42 | + const pkgDirMatches = pkgDir.match(/^(?<pkgName>[^=]*)=(?<dir>[^=]+)$/) |
| 43 | + if (pkgDirMatches === null) |
| 44 | + throw new Error("CLI argument not of the form \`<pkg-name>=<directory>\`\n" + help) |
| 45 | + |
| 46 | + const { pkgName, dir } = pkgDirMatches.groups |
| 47 | + const listings = await fs.readdir(dir, { recursive: true }) |
| 48 | + |
| 49 | + const filtered = [] |
| 50 | + for (const listing of listings) { |
| 51 | + const listingMatch = listing.match(/^(?<moduleName>.*)\.lbf$/) |
| 52 | + if (listingMatch === null) |
| 53 | + continue |
| 54 | + const { moduleName } = listingMatch.groups |
| 55 | + filtered.push(moduleName.replace(/\//g, '.')) |
| 56 | + } |
| 57 | + |
| 58 | + if (result[pkgName] !== undefined) |
| 59 | + console.error(`lbf-list-modules-typescript: warning: package name ${pkgName} is defined multiple times. Overwriting the first definition.`) |
| 60 | + |
| 61 | + result[pkgName] = filtered |
| 62 | + } |
| 63 | + |
| 64 | + process.stdout.write(JSON.stringify(result)); |
| 65 | +} |
| 66 | + |
| 67 | +main() |
0 commit comments