@vertesia/memory-cli
Version:
Vertesia memory builder CLI
101 lines • 3.98 kB
JavaScript
import { loadMemoryPack } from '@vertesia/memory';
import { build } from '@vertesia/memory-commands';
import { Command } from 'commander';
import { readFile } from 'fs/promises';
import { dirname } from 'path';
import url from 'url';
export function setupMemoCommand(command, publish) {
const buildCmd = new Command("build").description("Build a memory pack from a recipe script.");
buildCmd.allowUnknownOption()
.option('-i, --indent <spaces>', 'The number of spaces to indent the JSON result. A indentation of 2 is used by default.')
.option('-q, --quiet', 'Do not log anything to the console.')
.option('-z, --gzip', 'Compress the output file using gzip.')
.option('-o, --out <file>', 'The output file. Defaults to "memory.tar".')
.option('-t, --test', 'Test the memory script without building it.')
.argument('<recipe>', 'The recipe script to build the memory from.')
.action((_arg, options, command) => {
memoAction(command, { ...options, publish }).catch((err) => {
console.error("Failed to run command: ", err);
process.exit(1);
});
});
const exportCmd = new Command("export").description("Export a JSON object from the memory pack given a mapping.");
exportCmd.option('--map <mapping>', 'The mapping to use. An inline JSON object or a path to a JSOn file prefixed with @')
.option('-i, --indent <spaces>', 'The number of spaces to indent the JSON result. No indentation is done by default.')
.argument('<pack>', 'The uncompressed memory pack to use (i.e. a .tar file).')
.action((arg, options, command) => {
exportAction(command, arg, options).catch((err) => {
console.error("Failed to run command: ", err);
process.exit(1);
});
});
command.addCommand(buildCmd);
command.addCommand(exportCmd);
return command;
}
function memoAction(command, options) {
const { script, vars } = parseArgs(command.args);
if (options.indent) {
options.indent = parseInt(options.indent);
}
if (!options.transpileDir) {
options.transpileDir = dirname(url.fileURLToPath(import.meta.url));
}
return build(script, { ...options, vars });
}
/**
* We take all --var-xxx options and return them as an object to be passed as the `vars` variable to the script
* @param args
*/
function parseArgs(args) {
if (!args.length) {
console.error("No recipe script was provided.");
process.exit(1);
}
let script;
const vars = {};
let lastKey;
let lastCommittedOption;
for (const arg of args) {
if (arg.startsWith('--var-')) {
if (lastKey) {
vars[lastKey] = true;
}
lastKey = arg.substring(6);
}
else if (lastKey) {
vars[lastKey] = arg;
lastCommittedOption = lastKey;
lastKey = undefined;
}
else if (script) {
console.error(`Ambiguous command line arguments. Multiple recipe scripts found: ${script}, ${arg}`);
process.exit(1);
}
else {
script = arg;
}
}
if (!script) {
if (!lastCommittedOption) {
console.error("Ambiguous command line arguments. No recipe script was found.");
process.exit(1);
}
else {
script = vars[lastCommittedOption];
vars[lastCommittedOption] = true;
}
}
return { script, vars };
}
async function exportAction(_command, packFile, options) {
let mapParam = options.map;
if (mapParam.startsWith('@')) {
mapParam = await readFile(mapParam.substring(1), "utf-8");
}
const mapping = JSON.parse(mapParam);
const pack = await loadMemoryPack(packFile);
const obj = await pack.exportObject(mapping);
console.log(JSON.stringify(obj, null, options.indent || 2));
}
//# sourceMappingURL=command.js.map