Skip to content

Commit

Permalink
cli: visualise use format and output from cli arguments
Browse files Browse the repository at this point in the history
Closes #481
  • Loading branch information
dirkdev98 committed Nov 7, 2020
1 parent d2d8d51 commit d02395f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
65 changes: 59 additions & 6 deletions packages/cli/src/commands/visualise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
]);

Expand All @@ -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,
};
Expand Down Expand Up @@ -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;
}

0 comments on commit d02395f

Please sign in to comment.