All files / cli/src/commands view.ts

10% Statements 2/20
0% Branches 0/10
20% Functions 1/5
10.52% Lines 2/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39            5x       1x                                                        
import { spawn } from 'node:child_process';
import { parseLDT } from '@ies-tools/core';
import { defineCommand } from 'yargs-file-commands';
import { servePreview } from '../preview.js';
import { inputOptions, readInput, requireLampSetSelection } from '../options.js';
 
export const command = defineCommand({
  command: 'view <file>',
  describe: 'Serve a local interactive light preview on 127.0.0.1; press Ctrl+C to stop',
  builder: (yargs) =>
    inputOptions(yargs)
      .option('port', { type: 'number', default: 0, describe: 'Port to listen on; 0 picks a free port' })
      .option('open', { type: 'boolean', describe: 'Open the preview in the default browser' }),
  handler: async (argv) => {
    const { text, format, lampSetIndex } = await readInput(argv);
    if (format === 'ldt') requireLampSetSelection(parseLDT(text).lampSets.length, lampSetIndex);
    const preview = await servePreview(text, argv.port, { format, lampSetIndex });
    console.log(`Photometric preview: ${preview.url}\nPress Ctrl+C to stop.`);
    const stop = () => {
      preview.server.closeAllConnections();
      preview.server.close();
    };
    process.once('SIGINT', stop);
    process.once('SIGTERM', stop);
    preview.server.once('close', () => {
      process.off('SIGINT', stop);
      process.off('SIGTERM', stop);
    });
    if (argv.open) {
      const executable =
        process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'rundll32' : 'xdg-open';
      const args = process.platform === 'win32' ? ['url.dll,FileProtocolHandler', preview.url] : [preview.url];
      spawn(executable, args, { stdio: 'ignore' }).on('error', (error) =>
        console.error(`Could not open browser: ${error.message}. Open ${preview.url} manually.`),
      );
    }
  },
});