← Files Flows by plugin.spaceARCHIVED FILE

scripts/attach-screen.mjs

3.37 KB · Nov 30, 2026 · 00:17 UTC

↓ Download file

#!/usr/bin/env node
import { createLibrary, inferApplication } from '../lib/library.mjs';
import { launcherCommand } from '../lib/node-runtime.mjs';
import { readFile, stat } from 'node:fs/promises';
import { resolve } from 'node:path';
import { isEntrypoint } from '../lib/entrypoint.mjs';
import { readDocument } from '../lib/document.mjs';
import { attachScreenshots, discardScreenshots, imageDimensions, saveScreenshot } from '../lib/screenshots.mjs';

export async function main(args = process.argv.slice(2)) {
  if (args.includes('--help')) { console.log(`Attach a real PNG or JPEG captured in a browser or supplied by the user.\n${launcherCommand('attach-screen')} --file flow.flows.json --node screen-id --profile desktop --image /path/screen.jpg [--method browser|import] [--url https://app.example/screen]\nCapture at the profile viewport with 1× pixel density.`); return; }
  const options = {};
  for (let index = 0; index < args.length; index += 2) {
    if (!['--file', '--node', '--profile', '--image', '--method', '--url', '--library'].includes(args[index]) || !args[index + 1]) throw new Error('Invalid option. Use --help.');
    options[args[index]] = args[index + 1];
  }
  if (!['--file', '--node', '--profile', '--image'].every(key => options[key])) throw new Error('Provide --file, --node, --profile and --image.');
  const file = resolve(options['--file']);
  const library = createLibrary(options['--library']);
  const localId = await library.register(file, await inferApplication(file));
  await library.withWritable(localId, () => {});
  const { document, revision } = await readDocument(file);
  const settings = await library.capturePreferences.effective(document.captureSettings);
  const profile = settings.profiles.find(profile => profile.id === options['--profile']);
  if (!profile) throw new Error('Add this viewport in Screenshot settings first.');
  if (!document.nodes.some(node => node.id === options['--node'] && node.type === 'screen')) throw new Error('Choose an existing screen node.');
  const method = options['--method'] ?? 'import';
  if (!['browser', 'import'].includes(method)) throw new Error('Method must be browser or import.');
  if (options['--url']) { const url = new URL(options['--url']); if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) throw new Error('Use an HTTP(S) source URL without credentials.'); }
  if ((await stat(options['--image'])).size > 8 * 1024 * 1024) throw new Error('Screenshot exceeds 8 MB.');
  const buffer = await readFile(options['--image']);
  const dimensions = imageDimensions(buffer);
  if (dimensions.width !== profile.width || (!settings.fullPage && dimensions.height !== profile.height) || (settings.fullPage && dimensions.height < profile.height)) throw new Error(`Image dimensions do not match ${profile.name} (${profile.width} × ${profile.height}, 1×). Capture at this viewport; do not resize a desktop screen to imitate mobile.`);
  const shot = await saveScreenshot(file, { nodeId: options['--node'], profile, buffer, method, url: options['--url'] });
  try { await library.withWritable(localId, () => attachScreenshots(file, revision, [shot], settings)); }
  catch (error) { await discardScreenshots([shot]); throw error; }
  console.log(`Attached ${profile.name} screenshot to ${options['--node']}.`);
}
if (isEntrypoint(import.meta.url)) main().catch(error => { console.error(error.message); process.exitCode = 1; });

SHA-256: b8c3d0dc740c340d1582d6eb0bf057b948f56b6cb17aa9dfaf17d80870313ba7