import fs from 'node:fs/promises'; import { basename, dirname, join, resolve } from 'node:path'; import { merge } from './edit.js'; import { ensureHome, home, userHome } from './home.js'; import { absolute, bundle, codeRefs, fileRefs } from './styles.js'; /** * A Look is the part of an Edit that does not depend on one Take: everything except camera targets and scrolls, * the address bar's url and its changes, where popups sit, the frame rate, the Shortcuts, the fast-forwards, the masks and the Narration's Lines. A * Look sets only what it names, and is not validated. */ export const mergeLook = merge; /** The Look an Edit carries. */ export function lookFromEdit(edit) { const { schemaVersion, narration, shortcuts, fastForward, masks, output: { fps, ...output } = {}, frame, camera: { targets, scrolls, ...camera } = {}, tabs: { placement, ...tabs } = {}, ...rest } = edit; const { url, urls, ...styling } = frame ?? {}; return { ...rest, output, frame: styling, camera, tabs }; } /** * The Edit with a Look adopted. Camera targets, the frame rate, the Shortcuts, the fast-forwards, the * masks, the Lines and where popups sit stay; the address bar keeps its url and changes, else shows `host`. */ export function applyLook(edit, look, host) { const next = merge(edit, look); next.frame = { ...next.frame, url: edit.frame?.url ?? host, ...(edit.frame?.urls && { urls: edit.frame.urls }) }; if (edit.tabs?.placement) next.tabs = { ...next.tabs, placement: edit.tabs.placement }; return next; } const label = item => item && typeof item === 'object' ? basename(String(item.shader ?? item.draw ?? item.image ?? '')) : item === false ? 'none' : null; /** One line for listings, such as "browser-dark · fluted · 3840×2160". */ export function summarize(look) { const { frame, output } = look; const parts = [label(frame?.chrome), ...(Array.isArray(frame?.background) ? frame.background.map(label) : []), output?.width && output?.height ? `${output.width}×${output.height}` : null]; return parts.filter(Boolean).join(' · ') || Object.keys(look).join(', ') || 'empty'; } /** Looks live in the Home; a project Home's Looks come before the user Home's, which serve every project. */ const inProject = () => home() !== userHome(); const scopes = () => ({ ...(inProject() && { project: join(home(), 'looks') }), user: join(userHome(), 'looks') }); /** Where `look save` and `look rm` act: the user Home with --global, else the Home. */ const scopeFor = global => !global && inProject() ? 'project' : 'user'; const NAME = /^[a-z0-9][a-z0-9-]{0,63}$/; function checkName(name) { if (!NAME.test(name)) throw new Error('Use a short lowercase Look name, such as brand'); } const readIfPresent = path => fs.readFile(path, 'utf8').catch(error => { if (['ENOENT', 'ENOTDIR'].includes(error.code)) return null; throw error; }); const parse = (text, path) => { try { return JSON.parse(text); } catch (error) { throw new Error(`${path} is not JSON: ${error.message}`); } }; /** A saved Look is `.json`, or a `/` folder holding `look.json` and the files it names. */ async function readSaved(dir, name) { for (const path of [join(dir, `${name}.json`), join(dir, name, 'look.json')]) { const text = await readIfPresent(path); if (text !== null) return { path, dir: dirname(path), look: parse(text, path) }; } return null; } /** * A Look by name, the project's before the user's, or from a JSON file labeled by its `name` or file name. `dir` is * where its file refs resolve. */ export async function loadLook(ref, cwd = process.cwd()) { if (ref.endsWith('.json')) { const path = resolve(cwd, ref); const { name = basename(ref, '.json'), ...look } = parse(await fs.readFile(path, 'utf8'), path); return { name: String(name), dir: dirname(path), look }; } checkName(ref); for (const [scope, root] of Object.entries(scopes())) { const saved = await readSaved(root, ref); if (saved) return { name: ref, scope, dir: saved.dir, look: saved.look }; } throw new Error(`No Look named '${ref}' in ${Object.values(scopes()).join(' or ')}`); } /** A Look's styling with its file refs made absolute, ready to merge with styling from elsewhere. */ export const resolvedLook = ({ look, dir }) => absolute(look, dir); /** Every saved Look, with the code files it runs; a user Look hidden by a project one with the same name is marked shadowed. */ export async function listLooks() { const found = []; for (const [scope, root] of Object.entries(scopes())) { const entries = await fs.readdir(root, { withFileTypes: true }).catch(error => { if (error.code === 'ENOENT') return []; throw error; }); const names = entries.flatMap(e => e.isDirectory() ? [e.name] : e.name.endsWith('.json') ? [basename(e.name, '.json')] : []).sort(); for (const name of new Set(names)) { const saved = await readSaved(root, name).catch(error => ({ error })); if (!saved) continue; const entry = { name, scope, shadowed: scope === 'user' && found.some(l => l.scope === 'project' && l.name === name), path: saved.path ?? join(root, name) }; if (saved.error) found.push({ ...entry, summary: `invalid: ${saved.error.message}` }); else found.push({ ...entry, summary: summarize(saved.look), code: codeRefs(absolute(saved.look, saved.dir)) }); } } return found; } /** * Saves a Look in the Home, or the user Home when `global`, with its file refs resolved against `from`. A * Look that names files becomes a folder holding copies of them. An existing Look with the name is kept * unless `replace`. */ export async function saveLook(name, look, { global = false, replace = false, from = process.cwd() } = {}) { checkName(name); const scope = scopeFor(global), root = scopes()[scope]; if (scope === 'project') await ensureHome(); const existing = await readSaved(root, name); if (existing && !replace) throw new Error(`A ${scope} Look named '${name}' already exists at ${existing.path}; pass --replace to overwrite it`); await fs.rm(join(root, `${name}.json`), { force: true }); await fs.rm(join(root, name), { recursive: true, force: true }); const hasFiles = fileRefs(look).length > 0, dir = hasFiles ? join(root, name) : root; const saved = hasFiles ? await bundle(look, from, dir) : look; const path = hasFiles ? join(dir, 'look.json') : join(root, `${name}.json`); await fs.mkdir(dirname(path), { recursive: true }); await fs.writeFile(path, JSON.stringify(saved, null, 2) + '\n'); return { name, scope, path, summary: summarize(saved), code: codeRefs(absolute(saved, dir)) }; } /** The file or folder of every Look in the user Home; other files in the folder are not CueTake's. */ export async function userLookFiles() { const dir = scopes().user, paths = []; const entries = await fs.readdir(dir, { withFileTypes: true }).catch(error => { if (error.code === 'ENOENT') return []; throw error; }); for (const entry of entries) { const path = join(dir, entry.name); if (entry.isFile() && entry.name.endsWith('.json') && NAME.test(basename(entry.name, '.json'))) paths.push(path); else if (entry.isDirectory() && NAME.test(entry.name) && await readIfPresent(join(path, 'look.json')) !== null) paths.push(path); } return { dir, paths: paths.sort() }; } export async function removeLook(name, { global = false } = {}) { checkName(name); const scope = scopeFor(global), root = scopes()[scope]; const saved = await readSaved(root, name); if (!saved) throw new Error(`No ${scope} Look named '${name}' in ${root}`); const removed = saved.path === join(root, `${name}.json`) ? saved.path : saved.dir; await fs.rm(removed, { recursive: true, force: true }); return { name, scope, removed }; }