Formatting
This commit is contained in:
@@ -14,7 +14,7 @@ import { resolveTemplate } from "../utils.js";
|
||||
*/
|
||||
export const printTemplateHelp = (io: CommandIO): void => {
|
||||
io.out(
|
||||
`
|
||||
`
|
||||
${bold("Usage:")} xo-cli template <sub-command>
|
||||
|
||||
${bold("Sub-commands:")}
|
||||
@@ -23,7 +23,8 @@ ${bold("Sub-commands:")}
|
||||
- list <category> <identifier> ${dim("List all options of the field type in a template")}
|
||||
- inspect <category> <identifier> <field> ${dim("Inspect a field in a template")}
|
||||
- set-default <template-file> <output-identifier> <role-identifier> ${dim("Set the default template")}
|
||||
`);
|
||||
`,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -41,8 +42,11 @@ export const handleTemplateListCommand = async (
|
||||
|
||||
if (!templateCategory) {
|
||||
const templates = await deps.app.engine.listImportedTemplates();
|
||||
const formattedTemplates = templates.map((template: XOTemplate) => `${bold(generateTemplateIdentifier(template))} - ${dim(template.name)} ${dim(template.description)}`);
|
||||
deps.io.out(formattedTemplates.join('\n'));
|
||||
const formattedTemplates = templates.map(
|
||||
(template: XOTemplate) =>
|
||||
`${bold(generateTemplateIdentifier(template))} - ${dim(template.name)} ${dim(template.description)}`,
|
||||
);
|
||||
deps.io.out(formattedTemplates.join("\n"));
|
||||
return { count: templates.length };
|
||||
}
|
||||
|
||||
@@ -51,13 +55,19 @@ export const handleTemplateListCommand = async (
|
||||
|
||||
if (!templateIdentifier) {
|
||||
deps.io.err("No template identifier provided");
|
||||
throw new CommandError("template.list.identifier_missing", "No template identifier provided");
|
||||
throw new CommandError(
|
||||
"template.list.identifier_missing",
|
||||
"No template identifier provided",
|
||||
);
|
||||
}
|
||||
|
||||
const rawTemplate = await deps.app.engine.getTemplate(templateIdentifier);
|
||||
if (!rawTemplate) {
|
||||
deps.io.err(`No template found: ${templateIdentifier}`);
|
||||
throw new CommandError("template.list.not_found", `No template found: ${templateIdentifier}`);
|
||||
throw new CommandError(
|
||||
"template.list.not_found",
|
||||
`No template found: ${templateIdentifier}`,
|
||||
);
|
||||
}
|
||||
|
||||
const template = await resolveTemplateReferences(rawTemplate);
|
||||
@@ -66,47 +76,65 @@ export const handleTemplateListCommand = async (
|
||||
switch (templateCategory) {
|
||||
case "action": {
|
||||
const actions = template.actions;
|
||||
const formattedActions = Object.entries(actions).map(([actionIdentifier, action]) => `${bold(actionIdentifier)} ${dim(action.name)} ${dim(action.description)}`);
|
||||
deps.io.out(formattedActions.join('\n'));
|
||||
const formattedActions = Object.entries(actions).map(
|
||||
([actionIdentifier, action]) =>
|
||||
`${bold(actionIdentifier)} ${dim(action.name)} ${dim(action.description)}`,
|
||||
);
|
||||
deps.io.out(formattedActions.join("\n"));
|
||||
return {};
|
||||
}
|
||||
case "transaction": {
|
||||
const transactions = template.transactions;
|
||||
const formattedTransactions = Object.entries(transactions).map(([transactionIdentifier, transaction]) => `${bold(transactionIdentifier)} ${dim(transaction.name)} ${dim(transaction.description)}`);
|
||||
deps.io.out(formattedTransactions.join('\n'));
|
||||
const formattedTransactions = Object.entries(transactions).map(
|
||||
([transactionIdentifier, transaction]) =>
|
||||
`${bold(transactionIdentifier)} ${dim(transaction.name)} ${dim(transaction.description)}`,
|
||||
);
|
||||
deps.io.out(formattedTransactions.join("\n"));
|
||||
return {};
|
||||
}
|
||||
case "output": {
|
||||
const outputs = template.outputs;
|
||||
const formattedOutputs = Object.entries(outputs).map(([outputIdentifier, output]) => `${bold(outputIdentifier)} ${dim(output.name)} ${dim(output.description)}`);
|
||||
deps.io.out(formattedOutputs.join('\n'));
|
||||
const formattedOutputs = Object.entries(outputs).map(
|
||||
([outputIdentifier, output]) =>
|
||||
`${bold(outputIdentifier)} ${dim(output.name)} ${dim(output.description)}`,
|
||||
);
|
||||
deps.io.out(formattedOutputs.join("\n"));
|
||||
return {};
|
||||
}
|
||||
case "lockingscript": {
|
||||
const lockingscripts = template.lockingScripts;
|
||||
const formattedLockingscripts = Object.entries(lockingscripts).map(([lockingScriptIdentifier, lockingScript]) => `${bold(lockingScriptIdentifier)} ${dim(lockingScript.name)} ${dim(lockingScript.description)}`);
|
||||
deps.io.out(formattedLockingscripts.join('\n'));
|
||||
const formattedLockingscripts = Object.entries(lockingscripts).map(
|
||||
([lockingScriptIdentifier, lockingScript]) =>
|
||||
`${bold(lockingScriptIdentifier)} ${dim(lockingScript.name)} ${dim(lockingScript.description)}`,
|
||||
);
|
||||
deps.io.out(formattedLockingscripts.join("\n"));
|
||||
return {};
|
||||
}
|
||||
case "variable": {
|
||||
const variables = template.variables || {};
|
||||
const formattedVariables = Object.entries(variables).map(([variableIdentifier, variable]) => `${bold(variableIdentifier)} ${dim(variable.name)} ${dim(variable.description)}`);
|
||||
deps.io.out(formattedVariables.join('\n'));
|
||||
const formattedVariables = Object.entries(variables).map(
|
||||
([variableIdentifier, variable]) =>
|
||||
`${bold(variableIdentifier)} ${dim(variable.name)} ${dim(variable.description)}`,
|
||||
);
|
||||
deps.io.out(formattedVariables.join("\n"));
|
||||
return {};
|
||||
}
|
||||
default: {
|
||||
deps.io.verbose(`Unknown template category: ${templateCategory}`);
|
||||
throw new CommandError("template.list.category_unknown", `Unknown template category: ${templateCategory}`);
|
||||
throw new CommandError(
|
||||
"template.list.category_unknown",
|
||||
`Unknown template category: ${templateCategory}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Prints the help message for the template inspect command
|
||||
*/
|
||||
export const printTemplateInspectHelp = (io: CommandIO): void => {
|
||||
io.out(
|
||||
`
|
||||
`
|
||||
${bold("Usage:")} xo-cli template inspect <category> <identifier> <field>
|
||||
|
||||
${bold("Arguments:")}
|
||||
@@ -120,7 +148,8 @@ ${bold("Categories:")}
|
||||
- output <output-identifier> ${dim("Inspect an output")}
|
||||
- lockingscript <lockingscript-identifier> ${dim("Inspect a lockingscript")}
|
||||
- variable <variable-identifier> ${dim("Inspect a variable")}
|
||||
`);
|
||||
`,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -137,12 +166,17 @@ export const handleTemplateInspectCommand = async (
|
||||
const templateQuery = args[1];
|
||||
const templateField = args[2];
|
||||
|
||||
deps.io.verbose(`Template inspect args - category: ${templateCategory}, identifier: ${templateQuery}, field: ${templateField}`);
|
||||
deps.io.verbose(
|
||||
`Template inspect args - category: ${templateCategory}, identifier: ${templateQuery}, field: ${templateField}`,
|
||||
);
|
||||
|
||||
if (!templateCategory || !templateQuery || !templateField) {
|
||||
deps.io.err("No template category, identifier, or field provided");
|
||||
printTemplateInspectHelp(deps.io);
|
||||
throw new CommandError("template.inspect.arguments_missing", "No template category, identifier, or field provided");
|
||||
throw new CommandError(
|
||||
"template.inspect.arguments_missing",
|
||||
"No template category, identifier, or field provided",
|
||||
);
|
||||
}
|
||||
|
||||
const originalTemplate = await resolveTemplate(deps, templateQuery);
|
||||
@@ -156,7 +190,10 @@ export const handleTemplateInspectCommand = async (
|
||||
const action = template.actions[templateField];
|
||||
if (!action) {
|
||||
deps.io.err(`No action found: ${templateField}`);
|
||||
throw new CommandError("template.inspect.action_missing", `No action found: ${templateField}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.action_missing",
|
||||
`No action found: ${templateField}`,
|
||||
);
|
||||
}
|
||||
deps.io.out(formatObject(action));
|
||||
return {};
|
||||
@@ -165,7 +202,10 @@ export const handleTemplateInspectCommand = async (
|
||||
const transaction = template.transactions?.[templateField];
|
||||
if (!transaction) {
|
||||
deps.io.err(`No transaction found: ${templateField}`);
|
||||
throw new CommandError("template.inspect.transaction_missing", `No transaction found: ${templateField}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.transaction_missing",
|
||||
`No transaction found: ${templateField}`,
|
||||
);
|
||||
}
|
||||
deps.io.out(formatObject(transaction));
|
||||
return {};
|
||||
@@ -174,7 +214,10 @@ export const handleTemplateInspectCommand = async (
|
||||
const output = template.outputs[templateField];
|
||||
if (!output) {
|
||||
deps.io.err(`No output found: ${templateField}`);
|
||||
throw new CommandError("template.inspect.output_missing", `No output found: ${templateField}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.output_missing",
|
||||
`No output found: ${templateField}`,
|
||||
);
|
||||
}
|
||||
deps.io.out(formatObject(output));
|
||||
return {};
|
||||
@@ -183,7 +226,10 @@ export const handleTemplateInspectCommand = async (
|
||||
const lockingscript = template.lockingScripts[templateField];
|
||||
if (!lockingscript) {
|
||||
deps.io.err(`No lockingscript found: ${templateField}`);
|
||||
throw new CommandError("template.inspect.lockingscript_missing", `No lockingscript found: ${templateField}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.lockingscript_missing",
|
||||
`No lockingscript found: ${templateField}`,
|
||||
);
|
||||
}
|
||||
deps.io.out(formatObject(lockingscript));
|
||||
return {};
|
||||
@@ -192,17 +238,23 @@ export const handleTemplateInspectCommand = async (
|
||||
const variable = template.variables?.[templateField];
|
||||
if (!variable) {
|
||||
deps.io.err(`No variable found: ${templateField}`);
|
||||
throw new CommandError("template.inspect.variable_missing", `No variable found: ${templateField}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.variable_missing",
|
||||
`No variable found: ${templateField}`,
|
||||
);
|
||||
}
|
||||
deps.io.out(formatObject(variable));
|
||||
return {};
|
||||
}
|
||||
default: {
|
||||
deps.io.verbose(`Unknown template category: ${templateCategory}`);
|
||||
throw new CommandError("template.inspect.category_unknown", `Unknown template category: ${templateCategory}`);
|
||||
throw new CommandError(
|
||||
"template.inspect.category_unknown",
|
||||
`Unknown template category: ${templateCategory}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the template command.
|
||||
@@ -221,7 +273,10 @@ export const handleTemplateCommand = async (
|
||||
if (!subCommand) {
|
||||
deps.io.verbose("No sub-command provided");
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError("template.subcommand.missing", "No sub-command provided");
|
||||
throw new CommandError(
|
||||
"template.subcommand.missing",
|
||||
"No sub-command provided",
|
||||
);
|
||||
}
|
||||
|
||||
switch (subCommand) {
|
||||
@@ -232,7 +287,10 @@ export const handleTemplateCommand = async (
|
||||
if (!templateFile) {
|
||||
deps.io.verbose("No template file provided");
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError("template.import.file_missing", "No template file provided");
|
||||
throw new CommandError(
|
||||
"template.import.file_missing",
|
||||
"No template file provided",
|
||||
);
|
||||
}
|
||||
|
||||
const templatePath = path.resolve(`${process.cwd()}/${templateFile}`);
|
||||
@@ -241,7 +299,10 @@ export const handleTemplateCommand = async (
|
||||
if (!existsSync(templatePath)) {
|
||||
deps.io.err(`Template file does not exist: ${templatePath}`);
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError("template.import.file_not_found", `Template file does not exist: ${templatePath}`);
|
||||
throw new CommandError(
|
||||
"template.import.file_not_found",
|
||||
`Template file does not exist: ${templatePath}`,
|
||||
);
|
||||
}
|
||||
|
||||
const template = await readFileSync(templatePath, "utf8");
|
||||
@@ -262,17 +323,31 @@ export const handleTemplateCommand = async (
|
||||
const outputIdentifier = args[2];
|
||||
const roleIdentifier = args[3];
|
||||
if (!templateFile || !outputIdentifier || !roleIdentifier) {
|
||||
deps.io.verbose("No template file, output identifier, or role identifier provided");
|
||||
deps.io.verbose(
|
||||
"No template file, output identifier, or role identifier provided",
|
||||
);
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError("template.default.arguments_missing", "No template file, output identifier, or role identifier provided");
|
||||
throw new CommandError(
|
||||
"template.default.arguments_missing",
|
||||
"No template file, output identifier, or role identifier provided",
|
||||
);
|
||||
}
|
||||
deps.io.verbose(`Template file: ${templateFile}, output identifier: ${outputIdentifier}, role identifier: ${roleIdentifier}`);
|
||||
await deps.app.engine.setDefaultLockingParameters(templateFile, outputIdentifier, roleIdentifier);
|
||||
deps.io.verbose(
|
||||
`Template file: ${templateFile}, output identifier: ${outputIdentifier}, role identifier: ${roleIdentifier}`,
|
||||
);
|
||||
await deps.app.engine.setDefaultLockingParameters(
|
||||
templateFile,
|
||||
outputIdentifier,
|
||||
roleIdentifier,
|
||||
);
|
||||
return {};
|
||||
}
|
||||
default:
|
||||
deps.io.verbose(`Unknown template sub-command: ${subCommand}`);
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError("template.subcommand.unknown", `Unknown template sub-command: ${subCommand}`);
|
||||
throw new CommandError(
|
||||
"template.subcommand.unknown",
|
||||
`Unknown template sub-command: ${subCommand}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user