UNPKG

beam-cli

Version:

A beautifully simple CLI for running Lighthouse audits on a statically generated (SSG) website

61 lines (60 loc) 1.92 kB
import owDefault from 'ow'; import { errorMessage, warningMessage } from '../console/print.js'; // @ts-expect-error Workaround fix for loading Ow with ESM imports const ow = owDefault.default; const validateConfig = (config) => { ow(config, ow.string .minLength(2) .message('Option flag `config` should be string with a minimum length of 2.')); }; const validateDist = (dist) => { ow(dist, ow.string .minLength(2) .message('Option flag `dist` should be string with a minimum length of 2.')); }; const validateUrls = (urls) => { ow(urls, ow.string .minLength(2) .message('Option flag `urls` should be string with a minimum length of 2.')); }; const validatePort = (port) => { ow(port, ow.number .inRange(0, 49151) .message('Options flag `port` should be a number between 0 and 49151.')); }; const validateGui = (gui) => { if (typeof gui !== 'boolean') throw new Error('Options flag `gui` to be of type `boolean`'); }; const validateSetup = (setup) => { if (typeof setup !== 'boolean') throw new Error('Options flag `setup` to be of type `boolean`'); }; const validations = { config: validateConfig, dist: validateDist, urls: validateUrls, port: validatePort, gui: validateGui, setup: validateSetup, }; export const validateFlags = async function (input, flags) { if (input.length > 0) { warningMessage('Unexpected input provided, please only use the option flags listed via `beam --help`'); } try { const keys = Object.keys(validations); for (const name of keys) { if (flags[name] !== undefined) validations[name](flags[name]); } } catch (error) { if (error instanceof Error) { errorMessage(error.message); return false; } throw error; } return true; };