From d02395f8af4a50091615af9fa5120845e4290032 Mon Sep 17 00:00:00 2001 From: Dirk de Visser Date: Sat, 7 Nov 2020 13:30:34 +0100 Subject: [PATCH] cli: visualise use format and output from cli arguments Closes #481 --- packages/cli/src/commands/help.js | 2 +- packages/cli/src/commands/visualise.js | 65 +++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/help.js b/packages/cli/src/commands/help.js index ab1f03121b..fde623e1cb 100644 --- a/packages/cli/src/commands/help.js +++ b/packages/cli/src/commands/help.js @@ -29,7 +29,7 @@ Usage: - bench : lbu bench [--watch] [--verbose] [--node-arg] - coverage : lbu coverage [--watch] [--verbose] [--any-node-arg] [-- --c8-arg] - lint : lbu lint [--watch] [--verbose] [--any-node-arg] -- visualise : lbu visualise [sql,router] {path/to/generated/index.js} +- visualise : lbu visualise [sql,router] {path/to/generated/index.js} [--format png|svg|webp|pdf] [--output ./path/to/output.ext] Available script names: diff --git a/packages/cli/src/commands/visualise.js b/packages/cli/src/commands/visualise.js index 7ab50dd727..4ff8f973ba 100644 --- a/packages/cli/src/commands/visualise.js +++ b/packages/cli/src/commands/visualise.js @@ -24,7 +24,7 @@ const codeGenImportPath = pathJoin( * @returns {Promise<{ exitCode?: number }>} */ export async function visualiseCommand(logger, command) { - const [subCommand, structureFile] = command.arguments; + const [subCommand, structureFile, ...args] = command.arguments; // All pre-checks @@ -59,6 +59,12 @@ export async function visualiseCommand(logger, command) { return { exitCode: 1 }; } + const { format, output } = parseFormatAndOutputArguments( + logger, + subCommand, + args, + ); + // Get the structure const { structure, trie } = await getStructure( @@ -89,16 +95,15 @@ export async function visualiseCommand(logger, command) { } const tmpPathDot = `/tmp/${uuid()}.gv`; - const tmpOutputPath = `/tmp/${environment.APP_NAME.toLowerCase()}_${subCommand}.svg`; writeFileSync(tmpPathDot, graph, "utf8"); logger.info(`Dot file written to temporary directory. Spawning 'dot'.`); try { const { exitCode } = await spawn(`dot`, [ - "-Tsvg", + `-T${format}`, `-o`, - tmpOutputPath, + output, tmpPathDot, ]); @@ -108,14 +113,14 @@ export async function visualiseCommand(logger, command) { ); return { exitCode }; } - } catch (e) { + } catch { logger.error( `'Dot' could not be found. Please install 'graphviz' via your package manager and try again.`, ); return { exitCode: 1 }; } - logger.info(`Image of '${subCommand}' is available at ${tmpOutputPath}`); + logger.info(`Graph of '${subCommand}' is available at ${output}`); return { exitCode: 0, }; @@ -198,3 +203,51 @@ async function structureFileExists(structureFile) { return false; } } + +/** + * Get format and output path from arguments or supply defaults + * + * @param {Logger} logger + * @param {string} subCommand + * @param {string[]} args + * @returns {{ format: string, outputL: string }} + */ +function parseFormatAndOutputArguments(logger, subCommand, args) { + const supportedFormats = ["png", "svg", "pdf", "webp"]; + const result = { + format: "svg", + output: undefined, + }; + + const formatIdx = args.indexOf("--format"); + if (formatIdx !== -1) { + const formatValue = args[formatIdx + 1]; + if (supportedFormats.indexOf(formatValue) === -1) { + logger.error( + `Supplied format '${formatValue}' is invalid. Please use one of '${supportedFormats.join( + `', '`, + )}'.\nDefaulting to '${result.format}'.`, + ); + } else { + result.format = formatValue; + } + } + + result.output = `/tmp/${environment.APP_NAME.toLowerCase()}_${subCommand}.${ + result.format + }`; + + const outputIdx = args.indexOf("--output"); + if (outputIdx !== -1) { + const outputValue = args[outputIdx + 1]; + if (isNil(outputValue)) { + logger.error( + `No value given to '--output' option. Defaulting to '${result.output}'`, + ); + } else { + result.output = outputValue; + } + } + + return result; +}