@platformos/pos-cli
Version:
Manage your platformOS application
59 lines (49 loc) • 1.74 kB
JavaScript
import fs from 'fs';
import { program } from '../lib/program.js';
import Gateway from '../lib/proxy.js';
import { fetchSettings } from '../lib/settings.js';
import logger from '../lib/logger.js';
import { isProductionEnvironment, confirmProductionExecution } from '../lib/productionEnvironment.js';
program
.name('pos-cli exec liquid')
.argument('<environment>', 'name of environment. Example: staging')
.argument('[code]', 'liquid code to execute as string')
.option('-f, --file <path>', 'path to liquid file to execute')
.action(async (environment, code, options) => {
let liquidCode = code;
if (options.file) {
if (!fs.existsSync(options.file)) {
logger.Error(`File not found: ${options.file}`);
process.exit(1);
}
liquidCode = fs.readFileSync(options.file, 'utf8');
}
if (!liquidCode) {
logger.Error("error: missing required argument 'code'");
process.exit(1);
}
const authData = await fetchSettings(environment, program);
const gateway = new Gateway(authData);
if (isProductionEnvironment(environment)) {
const confirmed = await confirmProductionExecution(environment);
if (!confirmed) {
logger.Info('Execution cancelled.');
process.exit(0);
}
}
try {
const response = await gateway.liquid({ content: liquidCode });
if (response.error) {
logger.Error(`Liquid execution error: ${response.error}`);
process.exit(1);
}
if (response.result) {
logger.Print(response.result);
}
} catch (error) {
logger.Error(`Failed to execute liquid: ${error.message}`);
process.exit(1);
}
});
program.parse(process.argv);