pantry/scripts/ls.ts

69 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-17 15:22:22 +03:00
#!/usr/bin/env -S tea -E
// returns all pantry entries as `[{ name, path }]`
/*---
args:
- deno
- run
- --allow-env
2022-09-08 23:40:35 +03:00
- --allow-read
2022-08-17 15:22:22 +03:00
- --import-map={{ srcroot }}/import-map.json
---*/
2022-09-20 14:53:40 +03:00
import Path from "path"
import { useFlags, useCellar } from "hooks"
2022-08-17 15:22:22 +03:00
2022-09-08 23:40:35 +03:00
const prefix = new Path(`${useCellar().prefix}/tea.xyz/var/pantry/projects`)
2022-08-17 15:22:22 +03:00
2022-09-08 23:40:35 +03:00
interface Entry {
project: string
path: Path
}
2022-08-17 15:22:22 +03:00
2022-09-08 23:40:35 +03:00
//------------------------------------------------------------------------- funcs
2022-08-17 15:22:22 +03:00
export async function* ls(): AsyncGenerator<Entry> {
for await (const path of _ls_pantry(prefix)) {
yield {
2022-09-08 23:40:35 +03:00
project: path.parent().relative({ to: prefix }),
path
2022-08-17 15:22:22 +03:00
}
}
}
async function* _ls_pantry(dir: Path): AsyncGenerator<Path> {
if (!dir.isDirectory()) throw new Error()
for await (const [path, { name, isDirectory }] of dir.ls()) {
if (isDirectory) {
for await (const x of _ls_pantry(path)) {
yield x
}
} else if (name === "package.yml") {
yield path
}
}
}
2022-09-08 23:40:35 +03:00
//-------------------------------------------------------------------------- main
if (import.meta.main) {
const flags = useFlags()
2022-08-17 15:22:22 +03:00
2022-09-08 23:40:35 +03:00
const rv: Entry[] = []
for await (const item of ls()) {
rv.push(item)
}
2022-08-17 15:22:22 +03:00
2022-09-08 23:40:35 +03:00
if (Deno.env.get("GITHUB_ACTIONS")) {
const projects = rv.map(x => x.project).join(":")
console.log(`::set-output name=projects::${projects}`)
} else if (flags.json) {
const obj = rv.map(({ path, project }) => ({ path: path.string, project }))
const out = JSON.stringify(obj, null, 2)
console.log(out)
} else {
console.log(rv.map(x => x.project).join("\n"))
}
2022-08-17 15:22:22 +03:00
}