@constructor-io/constructorio-connect-cli
Version:
CLI tool to enable users to interface with the Constructor Connect Ecosystem
51 lines (50 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateCommandInput = generateCommandInput;
/**
* Generates a string representing a command that can be directly pasted
* into the terminal from the provided inputs describing the command.
* This is useful for providing in-command suggestions to the user for what they may
* want to run next.
*
* @param commandName Name of the command that should be printed (e.g. "execute" or "init").
* @param args The positional arguments to be passed to the command. Must be in order.
* @param inputFlags A map of flag names to the values that should be printed in the command.
* @returns A string representing a ready to execute command.
*/
function generateCommandInput({ commandName, args, inputFlags }) {
const flags = inputFlags ? renderInputFlags(inputFlags) : "";
return [
"npm run ", //
commandName,
renderDoubleDashes(commandName, !!flags),
renderArgs(args),
flags,
"\n",
].join("");
}
/**
* Sometimes, when executing a command via npm it needs to have double dashes (--) in front of the flags.
* This function will add the double dashes, if needed, to the command.
*/
function renderDoubleDashes(commandName, hasFlags) {
// If no flags are provided, we shouldn't add double dashes because there are no options to render.
if (!hasFlags) {
return "";
}
switch (commandName) {
case "execute":
return " --";
default:
return "";
}
}
function renderArgs(args) {
return args?.length ? ` ${args.join(" ")}` : "";
}
function renderInputFlags(input) {
return Object.keys(input).reduce((command, flag) => {
command += ` --${flag}=${input[flag]}`;
return command;
}, "");
}