UNPKG

@commercelayer/cli

Version:
52 lines (51 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const lodash_kebabcase_1 = tslib_1.__importDefault(require("lodash.kebabcase")); const config_1 = require("../../config"); const hook = async function (opts) { // Do not save fake commands output if (opts.Command.id?.toLowerCase().endsWith('noc')) return; if (opts.Command.name?.toLowerCase().endsWith('noc')) return; // Save last results if (opts.result) { const saveDir = (0, node_path_1.join)(opts.config.dataDir, 'results'); if (!(0, node_fs_1.existsSync)(saveDir)) (0, node_fs_1.mkdirSync)(saveDir, { recursive: true }); const filePath = (0, node_path_1.join)(saveDir, (0, lodash_kebabcase_1.default)(opts.Command.name) + '.last.json'); try { const data = JSON.stringify(opts.result, null, 4); (0, node_fs_1.writeFileSync)(filePath, data); } catch (error) { this.warn(`Error saving ouput for command ${opts.Command.name}`); } } // Save last command if (opts.argv && (opts.argv.length > 0)) { const commDir = (0, node_path_1.join)(opts.config.dataDir, 'commands'); if (!(0, node_fs_1.existsSync)(commDir)) (0, node_fs_1.mkdirSync)(commDir, { recursive: true }); const tstamp = new Date().toISOString(); const date = tstamp.substring(0, tstamp.indexOf('T')); const filePath = (0, node_path_1.join)(commDir, date + '_command.list'); const data = ['>', opts.Command.id, ...opts.argv]; (0, node_fs_1.appendFileSync)(filePath, `${data.join(' ')}\n`); deleteOldFiles(commDir); } }; exports.default = hook; const deleteOldFiles = (dir) => { const files = (0, node_fs_1.readdirSync)(dir).filter((f) => { const today = new Date(); const fday = new Date(f.substr(0, 10)); const diffday = Math.floor((today.getTime() - fday.getTime()) / 1000 / 60 / 60 / 24); return (diffday > Number((0, config_1.configParam)(config_1.ConfigParams.commandRetention))); }); if (files && (files.length > 0)) files.forEach(f => { (0, node_fs_1.unlinkSync)((0, node_path_1.join)(dir, f)); }); };