|
|
|
@@ -1,355 +0,0 @@
|
|
|
|
|
// `raea check` (docs/check.feature): the one check. Every rule each goal of brief.ts holds at one of four moments, on
|
|
|
|
|
// raea's own engine (raea's Rust runs this file with Node's core) or under node on a box. A brief's `goal` is one goal
|
|
|
|
|
// or several in order (a workflow): each is checked alone, and the report says each one's verdict and the whole's.
|
|
|
|
|
//
|
|
|
|
|
// raea check [FILE...] [--at edit|render|publish|done] [--goal NAME] [--only ID,...] [--manifest M] [--json]
|
|
|
|
|
// the project is the folder with brief.ts (from FILE's folder or this one, upwards); its goals are brief.ts's
|
|
|
|
|
// `goal`, and with none, the floor. The files are the ones named, else (publish, done) every deliverable and
|
|
|
|
|
// every video or still made under out/. Each check whose moments include --at (done by default) runs: a file
|
|
|
|
|
// check on each file, a job check once. A file's check after its render is kept beside it (FILE.check.json) and
|
|
|
|
|
// publish and done read it while it's fresh, never running it again. One line a rule a file. Writes
|
|
|
|
|
// review/goal.json and prints each finding. Exit 1 on an error, 2 when it can't run (brief.ts doesn't load). A
|
|
|
|
|
// check that needs what this machine hasn't (a transcriber) waits, and says what for; it holds nothing.
|
|
|
|
|
// raea goal tasks [DIR] [--json] | --apply EDITS.json|- the brief's plan as tasks, and edits written back (tasks.ts)
|
|
|
|
|
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"
|
|
|
|
|
import path from "node:path"
|
|
|
|
|
import { pathToFileURL } from "node:url"
|
|
|
|
|
import { type Check, type Finding, type Moment, MOMENTS, runOnce, type Threw } from "raea/check"
|
|
|
|
|
import { type Contract, type Goals, goalsOf, momentsOf, nameOf, packsOf, unknownPacks } from "./contract.ts"
|
|
|
|
|
import { fileFacts, type FileFacts, type Job, kindOf, runIn } from "./facts.ts"
|
|
|
|
|
import * as media from "./media.ts"
|
|
|
|
|
import { loadProject, type Project } from "./project.ts"
|
|
|
|
|
import { PACK_CHECKS } from "./packs/index.ts"
|
|
|
|
|
import { renderAlone } from "./packs/render.ts"
|
|
|
|
|
import { deliverablesOf } from "./packs/job.ts"
|
|
|
|
|
import { bookkeeping } from "./packs/naming.ts"
|
|
|
|
|
import { placeReport } from "./packs/place.ts"
|
|
|
|
|
import { tasksMain } from "./tasks.ts"
|
|
|
|
|
import { wellFormed } from "./text.ts"
|
|
|
|
|
|
|
|
|
|
/** packs a brief may still name that raea took out, with why: skipped, and said so (a brief never crashes the check) */
|
|
|
|
|
const GONE_PACKS: Record<string, string> = {
|
|
|
|
|
montage: "gone (12-06): a cut's beats are the agent's own plan, and a plan graded against itself catches no fault; the montage guide is how to cut one",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** the checks every project is held to once, whatever its goals: none in the loop (12-04: `registered`, how brief.ts
|
|
|
|
|
* imports files, failed every Dutch cut, and the taste check waits on every box; neither is a fault in the work) */
|
|
|
|
|
const ALWAYS: Check[] = []
|
|
|
|
|
|
|
|
|
|
/** the moments a deliverable's own check, run once after its render, stands for: publish and done read it, never run it again */
|
|
|
|
|
const REUSED: Moment[] = ["publish", "done"]
|
|
|
|
|
/** a deliverable's check kept beside it (FILE.check.json), while it's no older than the file or its manifest: the
|
|
|
|
|
* checks it ran and what they found */
|
|
|
|
|
export function keptCheck(abs: string): { checks: string[]; findings: Said[] } | null {
|
|
|
|
|
const k = `${media.stem(abs)}.check.json`
|
|
|
|
|
const when = (f: string) => (existsSync(f) ? statSync(f).mtimeMs : 0)
|
|
|
|
|
if (!existsSync(k) || when(k) < when(abs) || when(k) < when(`${media.stem(abs)}.render.json`)) return null
|
|
|
|
|
try {
|
|
|
|
|
const j = JSON.parse(readFileSync(k, "utf8")) as { checks?: string[]; findings?: Said[] }
|
|
|
|
|
return Array.isArray(j.checks) ? { checks: j.checks, findings: j.findings ?? [] } : null
|
|
|
|
|
} catch {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** one line a rule a file (12-04, round 26: 34 of one cut's errors were each caption phrase in the same UI zone; 12-05,
|
|
|
|
|
* run 5: an ERROR and a WARN of the same check each had a line, and a cut listed 12): of a check's findings at moments
|
|
|
|
|
* in the file, the most severe first one, and how many more there were; findings with no moment are distinct faults,
|
|
|
|
|
* said once each */
|
|
|
|
|
const RANK: Record<Said["level"], number> = { error: 0, warn: 1, note: 2 }
|
|
|
|
|
export function collapse(xs: Said[]): Said[] {
|
|
|
|
|
const firsts = new Map<string, { x: Said; same: number; lesser: number; to: number | null }>()
|
|
|
|
|
for (const x of xs) {
|
|
|
|
|
// a fault found at many moments (located in time) is one line; distinct faults with no moment each keep theirs
|
|
|
|
|
const key = `${x.goal ?? ""}\u0000${x.file ?? ""}\u0000${x.check}\u0000${x.seconds ? "" : `${x.level}\u0000${x.says}`}`
|
|
|
|
|
const had = firsts.get(key)
|
|
|
|
|
if (!had) firsts.set(key, { x, same: 0, lesser: 0, to: x.seconds?.[1] ?? null })
|
|
|
|
|
else {
|
|
|
|
|
if (RANK[x.level] < RANK[had.x.level]) {
|
|
|
|
|
// a worse one leads the line; what led it is counted under it
|
|
|
|
|
had.lesser += had.same + 1
|
|
|
|
|
had.same = 0
|
|
|
|
|
had.x = x
|
|
|
|
|
} else if (x.level === had.x.level) had.same++
|
|
|
|
|
else had.lesser++
|
|
|
|
|
if (x.seconds && (had.to === null || x.seconds[1] > had.to)) had.to = x.seconds[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [...firsts.values()].map(({ x, same, lesser }) => {
|
|
|
|
|
const more = [same ? `${same} more like it${x.seconds ? ", to the same fix" : ""}` : "", lesser ? `${lesser} lesser` : ""].filter(Boolean).join("; ")
|
|
|
|
|
return more ? { ...x, says: `${x.says} (and ${more})` } : x
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** a finding as `raea check` reports it: its goal, its check, the file it's in, and where in it */
|
|
|
|
|
export type Said = { goal: string | null; check: string; level: Finding["level"]; file: string | null; seconds: [number, number] | null; says: string; fix: string | null; by: string }
|
|
|
|
|
/** one goal's verdict at a moment */
|
|
|
|
|
export type GoalVerdict = { name: string; kind: Contract["kind"] | "floor"; step: string | null; after: string[]; passed: boolean; errors: number; warnings: number; checks: string[]; files: string[] }
|
|
|
|
|
export type Report = {
|
|
|
|
|
object: "goal.check"
|
|
|
|
|
at: Moment
|
|
|
|
|
/** each goal's verdict, in the order written; the floor alone when the brief has none */
|
|
|
|
|
goals: GoalVerdict[]
|
|
|
|
|
root: string
|
|
|
|
|
/** when it ran (s) */
|
|
|
|
|
when: number
|
|
|
|
|
/** every goal passed and no check broken: the work may move past this moment */
|
|
|
|
|
passed: boolean
|
|
|
|
|
errors: number
|
|
|
|
|
warnings: number
|
|
|
|
|
files: string[]
|
|
|
|
|
checks: string[]
|
|
|
|
|
findings: Said[]
|
|
|
|
|
/** checks that couldn't run here, and what they wait for */
|
|
|
|
|
waits: { check: string; file: string | null; says: string }[]
|
|
|
|
|
/** checks that broke: a fault in the check, which fails the run */
|
|
|
|
|
broke: { check: string; file: string | null; says: string }[]
|
|
|
|
|
/** the goals' own problems (doctor), said before anything runs */
|
|
|
|
|
plan: string[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** the folder with brief.ts, from `from` upwards */
|
|
|
|
|
export function rootOf(from: string): string | null {
|
|
|
|
|
let d = path.resolve(from)
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (existsSync(path.join(d, "brief.ts"))) return d
|
|
|
|
|
const up = path.dirname(d)
|
|
|
|
|
if (up === d) return null
|
|
|
|
|
d = up
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** the brief run once (project.ts), and its goals, their checks as the functions they are */
|
|
|
|
|
export async function goalsIn(root: string): Promise<{ goals: Contract[]; brief: string | null; project: Project }> {
|
|
|
|
|
const project = await loadProject(root)
|
|
|
|
|
return { goals: goalsOf(project.value?.goal as Goals | undefined), brief: project.brief, project }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PICTURE = /\.(mp4|mov|m4v|webm|png|jpe?g|webp)$/i
|
|
|
|
|
/** every video and still made under out/ (a bounded walk) */
|
|
|
|
|
function madeUnder(root: string): string[] {
|
|
|
|
|
const out: string[] = []
|
|
|
|
|
const stack = [path.join(root, "out")]
|
|
|
|
|
while (stack.length && out.length < 2000) {
|
|
|
|
|
const d = stack.pop()!
|
|
|
|
|
if (!existsSync(d)) continue
|
|
|
|
|
for (const n of readdirSync(d)) {
|
|
|
|
|
const p = path.join(d, n)
|
|
|
|
|
const st = statSync(p)
|
|
|
|
|
if (st.isDirectory()) stack.push(p)
|
|
|
|
|
else if (PICTURE.test(n)) out.push(path.relative(root, p))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out.filter((r) => !bookkeeping(r)).sort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** the checks that hold a goal at a moment: its packs', then its own */
|
|
|
|
|
export function checksOf(contract: Contract | null, at: Moment, only?: string[]): Check[] {
|
|
|
|
|
const packs = packsOf(contract)
|
|
|
|
|
// the render's own UI-zone lines are the floor pack's to measure when it runs, else the render pack keeps them
|
|
|
|
|
const of = (p: (typeof packs)[number]) => (p === "render" && !packs.includes("floor") ? [renderAlone as Check] : PACK_CHECKS[p])
|
|
|
|
|
const all = [...packs.flatMap(of), ...((contract?.checks ?? []) as Check[])]
|
|
|
|
|
const seen = new Set<string>()
|
|
|
|
|
return all.filter((c) => {
|
|
|
|
|
if (!c?.id || seen.has(c.id) || !c.once) return false
|
|
|
|
|
seen.add(c.id)
|
|
|
|
|
return momentsOf(c).includes(at) && (!only?.length || only.includes(c.id))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** what doctor says of the goals before anything runs */
|
|
|
|
|
function planOf(goals: Contract[]): string[] {
|
|
|
|
|
// core/brief's doctor, from raea's Rust (it keeps the types): RAEA_GOAL_DOCTOR
|
|
|
|
|
let doctor: string[] = []
|
|
|
|
|
try {
|
|
|
|
|
doctor = JSON.parse(process.env.RAEA_GOAL_DOCTOR ?? "[]")
|
|
|
|
|
} catch {}
|
|
|
|
|
const out = [...doctor]
|
|
|
|
|
for (const c of goals) {
|
|
|
|
|
const at = goals.length > 1 ? `${nameOf(c)}: ` : ""
|
|
|
|
|
out.push(...unknownPacks(c).map((k) => GONE_PACKS[k] ? `${at}packs.${k}: ${GONE_PACKS[k]}` : `${at}packs.${k}: raea has no pack of that name (raea check --json lists them)`))
|
|
|
|
|
for (const k of (c.checks ?? []) as unknown[]) if (!k || typeof (k as Check).id !== "string") out.push(`${at}checks: each is a check() imported from the project's checks/ folder`)
|
|
|
|
|
// nothing judges a `says` rule any more (12-04): said, so it isn't taken for checked
|
|
|
|
|
for (const r of c.rules ?? []) if (r.says && !r.run && !r.file) out.push(`${at}rules.${r.id}: a \`says\` rule isn't checked by anything: make it a command (\`run\`) or a file (\`file\`), or leave it to the person's review`)
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function goalCheck(o: { files?: string[]; at?: Moment; goal?: string; only?: string[]; cwd?: string; manifest?: string }): Promise<Report> {
|
|
|
|
|
const at = o.at ?? "done"
|
|
|
|
|
const cwd = o.cwd ?? process.cwd()
|
|
|
|
|
const root = rootOf(o.files?.length ? path.dirname(path.resolve(cwd, o.files[0])) : cwd) ?? cwd
|
|
|
|
|
const { goals: all, brief, project } = await goalsIn(root)
|
|
|
|
|
if (o.goal && !all.some((g) => nameOf(g) === o.goal)) throw new Error(`no goal is named ${o.goal}: ${all.map(nameOf).join(", ") || "the brief has none"}`)
|
|
|
|
|
const goals: (Contract | null)[] = all.length ? all.filter((g) => !o.goal || nameOf(g) === o.goal) : [null]
|
|
|
|
|
const named = (o.files ?? []).map((f) => path.relative(root, path.resolve(cwd, f)))
|
|
|
|
|
const findings: Said[] = []
|
|
|
|
|
const waits: Report["waits"] = []
|
|
|
|
|
const broke: Report["broke"] = []
|
|
|
|
|
const keep = (goal: string | null, file: FileFacts | null, r: { findings: Finding[]; threw: Threw[] }) => {
|
|
|
|
|
const fps = file?.fps ?? 30
|
|
|
|
|
for (const x of r.findings)
|
|
|
|
|
findings.push({ goal, check: x.check, level: x.level, file: file?.path ?? (x.about[0]?.name ?? null), seconds: x.frames ? [x.frames[0] / fps, x.frames[1] / fps] : null, says: x.says, fix: x.fix, by: x.by })
|
|
|
|
|
for (const t of r.threw) (t.waits ? waits : broke).push({ check: t.check, file: file?.path ?? null, says: t.says })
|
|
|
|
|
}
|
|
|
|
|
const job = (contract: Contract | null, deliverables: string[], files: FileFacts[]): Job => ({ root, brief, briefText: null, contract, project, moment: at, deliverables, files, run: runIn(root) })
|
|
|
|
|
// the project's own: once, whatever its goals
|
|
|
|
|
const once = ALWAYS.filter((c) => momentsOf(c).includes(at) && (!o.only?.length || o.only.includes(c.id)))
|
|
|
|
|
for (const c of once) keep(null, null, await runOnce([{ check: c, on: job(null, [], []) }]))
|
|
|
|
|
const verdicts: GoalVerdict[] = []
|
|
|
|
|
const allFiles = new Set<string>()
|
|
|
|
|
for (const contract of goals) {
|
|
|
|
|
const name = contract ? nameOf(contract) : "floor"
|
|
|
|
|
const deliverables = deliverablesOf(contract)
|
|
|
|
|
const wanted = named.length
|
|
|
|
|
? named
|
|
|
|
|
: at === "publish" || at === "done"
|
|
|
|
|
? [...new Set([...deliverables.flatMap((d) => d.split("|")).filter((d) => !d.endsWith("/") && existsSync(path.join(root, d))), ...madeUnder(root)])]
|
|
|
|
|
: []
|
|
|
|
|
const files: FileFacts[] = wanted.filter((f) => existsSync(path.join(root, f)) && statSync(path.join(root, f)).isFile()).map((f) => fileFacts(f, root, at, contract))
|
|
|
|
|
// a render's manifest kept away from its export (`raea render --manifest M`): the one file named reads it
|
|
|
|
|
if (o.manifest && files.length === 1) files[0].manifest = JSON.parse(readFileSync(path.resolve(o.manifest), "utf8")) as FileFacts["manifest"]
|
|
|
|
|
const checks = checksOf(contract, at, o.only)
|
|
|
|
|
const before = findings.length
|
|
|
|
|
const brokeBefore = broke.length
|
|
|
|
|
const j = job(contract, deliverables, files)
|
|
|
|
|
const kept = new Map(files.map((f) => [f.path, REUSED.includes(at) ? keptCheck(f.abs) : null]))
|
|
|
|
|
// a cut's words and its scene asked of the farm together, before its checks run one by one (12-06: a cut's check
|
|
|
|
|
// waited 66 s for its words, then asked for its scene; both are kept on the file's facts, so each check reads them)
|
|
|
|
|
if (contract?.kind === "cut") for (const f of files) if (f.kind === "video" && !kept.get(f.path)) for (const p of [f.words(), f.scene()]) p.catch(() => {})
|
|
|
|
|
for (const c of checks) {
|
|
|
|
|
if (c.over === "job") keep(contract ? name : null, null, await runOnce([{ check: c, on: j }]))
|
|
|
|
|
else
|
|
|
|
|
for (const f of files) {
|
|
|
|
|
const k = kept.get(f.path)
|
|
|
|
|
// run once, after its render: what it found then stands (its own findings, at this goal)
|
|
|
|
|
if (k?.checks.includes(c.id)) findings.push(...k.findings.filter((x) => x.check === c.id).map((x) => ({ ...x, goal: contract ? name : null })))
|
|
|
|
|
else keep(contract ? name : null, f, await runOnce([{ check: c, on: f, fps: f.fps }]))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const mine = findings.slice(before)
|
|
|
|
|
const errors = mine.filter((x) => x.level === "error").length
|
|
|
|
|
for (const f of files) allFiles.add(f.path)
|
|
|
|
|
verdicts.push({ name, kind: contract?.kind ?? "floor", step: contract?.step ?? null, after: contract?.after ?? [], passed: errors === 0 && broke.length === brokeBefore, errors, warnings: mine.filter((x) => x.level === "warn").length, checks: checks.map((c) => c.id), files: files.map((f) => f.path) })
|
|
|
|
|
}
|
|
|
|
|
findings.sort((a, b) => (a.file ?? "").localeCompare(b.file ?? "") || (a.seconds?.[0] ?? -1) - (b.seconds?.[0] ?? -1))
|
|
|
|
|
// printed and saved whole: no finding holds half a character (12-05, Dutch run 9: one broke the check as it printed)
|
|
|
|
|
findings.splice(0, findings.length, ...collapse(findings).map((x) => ({ ...x, says: wellFormed(x.says), fix: x.fix && wellFormed(x.fix) })))
|
|
|
|
|
const errors = findings.filter((x) => x.level === "error").length
|
|
|
|
|
return {
|
|
|
|
|
object: "goal.check",
|
|
|
|
|
at,
|
|
|
|
|
goals: verdicts,
|
|
|
|
|
root,
|
|
|
|
|
when: Math.floor(Date.now() / 1000),
|
|
|
|
|
passed: errors === 0 && broke.length === 0,
|
|
|
|
|
errors,
|
|
|
|
|
warnings: findings.filter((x) => x.level === "warn").length,
|
|
|
|
|
files: [...allFiles].sort(),
|
|
|
|
|
checks: [...new Set([...once.map((c) => c.id), ...verdicts.flatMap((v) => v.checks)])],
|
|
|
|
|
findings,
|
|
|
|
|
waits,
|
|
|
|
|
broke,
|
|
|
|
|
plan: planOf(all),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** the report written where the review, the hook and raea cloud read it: review/goal.json, and FILE.check.json beside
|
|
|
|
|
* each file named */
|
|
|
|
|
export function writeReport(r: Report, named: string[]) {
|
|
|
|
|
const dir = path.join(r.root, "review")
|
|
|
|
|
mkdirSync(dir, { recursive: true })
|
|
|
|
|
writeFileSync(path.join(dir, "goal.json"), `${JSON.stringify(r, null, 1)}\n`)
|
|
|
|
|
for (const f of named) {
|
|
|
|
|
const mine = r.findings.filter((x) => x.file === f)
|
|
|
|
|
writeFileSync(`${media.stem(path.join(r.root, f))}.check.json`, `${JSON.stringify({ object: "goal.check.file", at: r.at, file: f, passed: !mine.some((x) => x.level === "error"), checks: r.checks, findings: mine }, null, 1)}\n`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const secs = (s: [number, number] | null) => (!s ? "" : s[1] - s[0] <= 0.1 ? `${s[0].toFixed(1)} s ` : `${s[0].toFixed(1)}-${s[1].toFixed(1)} s `)
|
|
|
|
|
/** the report as a person or an agent reads it */
|
|
|
|
|
export function text(r: Report): string {
|
|
|
|
|
const t: string[] = []
|
|
|
|
|
for (const p of r.plan) t.push(`PLAN ${p}`)
|
|
|
|
|
for (const x of r.findings) {
|
|
|
|
|
if (x.level === "note" && r.at === "edit") continue
|
|
|
|
|
t.push(`${x.level === "error" ? "ERROR" : x.level === "warn" ? "WARN " : "NOTE "} ${r.goals.length > 1 && x.goal ? `${x.goal}/` : ""}${x.check}${x.file ? ` ${x.file}` : ""} ${secs(x.seconds)}${x.says}${x.fix ? `\n fix: ${x.fix}` : ""}`)
|
|
|
|
|
}
|
|
|
|
|
for (const w of r.waits) t.push(`WAITS ${w.check}${w.file ? ` ${w.file}` : ""} ${w.says}`)
|
|
|
|
|
for (const b of r.broke) t.push(`BROKE ${b.check}${b.file ? ` ${b.file}` : ""} ${b.says}`)
|
|
|
|
|
if (r.goals.length > 1) for (const g of r.goals) t.push(`${g.passed ? "ok " : "open"} goal ${g.name} (${g.kind}${g.step ? `, step ${g.step}` : ""}${g.after.length ? `, after ${g.after.join(", ")}` : ""}): ${g.errors} errors, ${g.warnings} warnings`)
|
|
|
|
|
const kinds = r.goals.map((g) => (r.goals.length > 1 ? g.name : g.kind)).join(", ")
|
|
|
|
|
t.push(`${r.passed ? "PASSED" : "FAILED"} raea check --at ${r.at} (${kinds}): ${r.files.length} file${r.files.length === 1 ? "" : "s"}, ${r.checks.length} checks, ${r.errors} errors, ${r.warnings} warnings${r.waits.length ? `, ${r.waits.length} waiting` : ""}; review/goal.json`)
|
|
|
|
|
return t.join("\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function flag(a: string[], name: string): string | undefined {
|
|
|
|
|
const i = a.indexOf(name)
|
|
|
|
|
return i >= 0 ? a[i + 1] : undefined
|
|
|
|
|
}
|
|
|
|
|
const positional = (a: string[], valued: string[]) => a.filter((x, i) => !x.startsWith("-") && !valued.includes(a[i - 1] ?? ""))
|
|
|
|
|
|
|
|
|
|
const USAGE = `raea check [FILE...] [--at edit|render|publish|done] [--goal NAME] [--only ID,...] [--json]
|
|
|
|
|
raea goal tasks [DIR] [--json] | raea goal tasks [DIR] --apply EDITS.json|-
|
|
|
|
|
raea make place FILE [--at S[,S...]] [--box WxH]`
|
|
|
|
|
|
|
|
|
|
export async function main(argv: string[]): Promise<number> {
|
|
|
|
|
const [verb, ...a] = argv
|
|
|
|
|
if (verb === "check") {
|
|
|
|
|
const at = (flag(a, "--at") ?? "done") as Moment
|
|
|
|
|
if (!MOMENTS.includes(at)) {
|
|
|
|
|
console.error(`raea check: --at is one of ${MOMENTS.join(", ")}`)
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
const files = positional(a, ["--at", "--only", "--manifest", "--goal"])
|
|
|
|
|
let r: Report
|
|
|
|
|
try {
|
|
|
|
|
r = await goalCheck({ files, at, goal: flag(a, "--goal"), only: flag(a, "--only")?.split(",").filter(Boolean), manifest: flag(a, "--manifest") })
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`raea check: ${(e as Error).message}`)
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
writeReport(r, r.files.filter((f) => files.some((n) => path.resolve(n) === path.join(r.root, f))))
|
|
|
|
|
console.log(a.includes("--json") ? JSON.stringify(r, null, 1) : text(r))
|
|
|
|
|
return r.passed ? 0 : 1
|
|
|
|
|
}
|
|
|
|
|
if (verb === "tasks") return tasksMain(a)
|
|
|
|
|
if (verb === "place") {
|
|
|
|
|
const file = positional(a, ["--at", "--box", "-o"])[0]
|
|
|
|
|
if (!file || !existsSync(file)) return usage()
|
|
|
|
|
const box = (flag(a, "--box") ?? "840x110").split("x").map(Number) as [number, number]
|
|
|
|
|
if (box.length !== 2 || !box.every((v) => v > 0)) {
|
|
|
|
|
console.error("raea make place: --box is WxH in a 1080-wide frame's pixels, like 840x110")
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
let at = flag(a, "--at")?.split(",").map(Number).filter(Number.isFinite)
|
|
|
|
|
if (!at?.length && kindOf(file) === "video") {
|
|
|
|
|
const len = media.seconds(file) ?? 1
|
|
|
|
|
const n = Math.min(12, Math.max(1, Math.floor(len)))
|
|
|
|
|
at = Array.from({ length: n }, (_, i) => Math.round(((i + 0.5) * len * 100) / n) / 100)
|
|
|
|
|
}
|
|
|
|
|
const { scenes } = await media.scene(path.resolve(file), at)
|
|
|
|
|
const rep = placeReport(file, scenes, box)
|
|
|
|
|
console.log(`${path.basename(file)}${at?.length ? ` at ${at.join(", ")} s` : ""}: the clearest places for a ${box[0]}x${box[1]} box in a ${rep.frame?.join("x")} frame, best first`)
|
|
|
|
|
for (const p of rep.places) console.log(` left ${p.left} px, top ${p.top} px (${p.at}) cost ${p.cost}${p.covers.length ? ` covers ${p.covers.join(", ")}` : " clear"}`)
|
|
|
|
|
const o = flag(a, "-o")
|
|
|
|
|
if (o) writeFileSync(o, JSON.stringify(rep, null, 1))
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return usage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function usage(): number {
|
|
|
|
|
console.error(USAGE)
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
|
|
|
main(process.argv.slice(2)).then(
|
|
|
|
|
(c) => process.exit(c),
|
|
|
|
|
(e) => {
|
|
|
|
|
console.error(`raea goal: ${e instanceof media.Unmet ? "" : "broke: "}${(e as Error).message}`)
|
|
|
|
|
process.exit(e instanceof media.Unmet ? 1 : 2)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|