Breaking-Change: Extremely rough update to work with Kioks wallet
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
import path from "path";
|
||||
import { generateTemplateIdentifier } from "@xo-cash/engine";
|
||||
import type { XOTemplate } from "@xo-cash/types";
|
||||
@@ -23,6 +23,10 @@ ${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")}
|
||||
- export <template-identifier> [output-file] ${dim("Export a template to stdout or a file")}
|
||||
|
||||
${bold("Options:")}
|
||||
-o --output <output-filename> ${dim("Output filename for the exported template")}
|
||||
`,
|
||||
);
|
||||
};
|
||||
@@ -338,6 +342,71 @@ export const handleTemplateInspectCommand = async (
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the template export command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
* @param deps - The command dependencies.
|
||||
* @param args - Positional args after "export", e.g. ["template-id"] or ["template-id", "template.json"].
|
||||
* @param options - Parsed option flags.
|
||||
*/
|
||||
export const handleTemplateExportCommand = async (
|
||||
deps: CommandDependencies,
|
||||
args: string[],
|
||||
options: Record<string, string>,
|
||||
): Promise<{ outputFile?: string }> => {
|
||||
// Get the template identifier from the arguments
|
||||
const templateIdentifier = args[0];
|
||||
|
||||
// If no template identifier is provided, print a message and throw an error
|
||||
if (!templateIdentifier) {
|
||||
deps.io.err("No template identifier provided");
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError(
|
||||
"template.export.identifier_missing",
|
||||
"No template identifier provided",
|
||||
);
|
||||
}
|
||||
|
||||
// Get the raw template from the engine.
|
||||
// Do not resolve references or pretty-print the template.
|
||||
const rawTemplate = await deps.app.engine.getTemplate(templateIdentifier);
|
||||
|
||||
// If the raw template is not found, print a message and throw an error
|
||||
if (!rawTemplate) {
|
||||
deps.io.err(`No template found: ${templateIdentifier}`);
|
||||
throw new CommandError(
|
||||
"template.export.not_found",
|
||||
`No template found: ${templateIdentifier}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Serialize the template without indentation to preserve the engine output shape.
|
||||
const serializedTemplate = JSON.stringify(rawTemplate);
|
||||
|
||||
// Resolve output file from --output (or -o), then fallback to optional positional output file
|
||||
const outputFile = options["output"] ?? args[1];
|
||||
|
||||
// If no output file is provided, print the template to stdout
|
||||
if (!outputFile) {
|
||||
deps.io.out(serializedTemplate);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Resolve output file path and write the template to disk
|
||||
const outputPath = path.resolve(process.cwd(), outputFile);
|
||||
try {
|
||||
writeFileSync(outputPath, serializedTemplate);
|
||||
} catch (error) {
|
||||
throw new CommandError(
|
||||
"template.export.write_failed",
|
||||
`Failed to export template to file: ${outputPath} (${error instanceof Error ? error.message : "unknown error"})`,
|
||||
);
|
||||
}
|
||||
|
||||
deps.io.out(`Template exported to: ${outputPath}`);
|
||||
return { outputFile: outputPath };
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the template command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
@@ -348,8 +417,8 @@ export const handleTemplateInspectCommand = async (
|
||||
export const handleTemplateCommand = async (
|
||||
deps: CommandDependencies,
|
||||
args: string[],
|
||||
_options: Record<string, string>,
|
||||
): Promise<{ templateFile?: string; count?: number }> => {
|
||||
options: Record<string, string>,
|
||||
): Promise<{ templateFile?: string; count?: number; outputFile?: string }> => {
|
||||
// Get the sub-command from the arguments
|
||||
const subCommand = args[0];
|
||||
|
||||
@@ -414,6 +483,10 @@ export const handleTemplateCommand = async (
|
||||
// Handle the template inspect command, We offload here as it has lots of arguments and is quite long
|
||||
return handleTemplateInspectCommand(deps, args.slice(1));
|
||||
}
|
||||
case "export": {
|
||||
// Handle the template export command
|
||||
return handleTemplateExportCommand(deps, args.slice(1), options);
|
||||
}
|
||||
case "set-default": {
|
||||
// Get the template file, output identifier, and role identifier from the arguments
|
||||
const templateFile = args[1];
|
||||
|
||||
Reference in New Issue
Block a user