2023-03-17 04:18:51 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const execFile = require('child_process').execFile;
|
|
|
|
const { Liquid } = require('liquidjs');
|
|
|
|
const logger = require('../logger').global;
|
2024-01-18 02:26:55 +00:00
|
|
|
const error = require('./error');
|
2020-02-19 04:55:06 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
2024-01-18 02:26:55 +00:00
|
|
|
exec: async function(cmd, options = {}) {
|
|
|
|
logger.debug('CMD:', cmd);
|
|
|
|
|
|
|
|
const { stdout, stderr } = await new Promise((resolve, reject) => {
|
|
|
|
const child = exec(cmd, options, (isError, stdout, stderr) => {
|
|
|
|
if (isError) {
|
|
|
|
reject(new error.CommandError(stderr, isError));
|
2020-02-19 04:55:06 +00:00
|
|
|
} else {
|
2024-01-18 02:26:55 +00:00
|
|
|
resolve({ stdout, stderr });
|
2020-02-19 04:55:06 +00:00
|
|
|
}
|
|
|
|
});
|
2024-01-18 02:26:55 +00:00
|
|
|
|
|
|
|
child.on('error', (e) => {
|
|
|
|
reject(new error.CommandError(stderr, 1, e));
|
|
|
|
});
|
2020-02-19 04:55:06 +00:00
|
|
|
});
|
2024-01-18 02:26:55 +00:00
|
|
|
return stdout;
|
2023-02-26 19:10:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-03-16 22:49:18 +00:00
|
|
|
* @param {String} cmd
|
|
|
|
* @param {Array} args
|
2023-02-26 19:10:25 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2023-03-16 22:49:18 +00:00
|
|
|
execFile: function (cmd, args) {
|
2024-01-18 02:26:55 +00:00
|
|
|
// logger.debug('CMD: ' + cmd + ' ' + (args ? args.join(' ') : ''));
|
|
|
|
|
2023-02-26 19:10:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-03-16 22:49:18 +00:00
|
|
|
execFile(cmd, args, function (err, stdout, /*stderr*/) {
|
2023-02-26 19:10:25 +00:00
|
|
|
if (err && typeof err === 'object') {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(stdout.trim());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-03-17 04:18:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in objection query builder
|
|
|
|
*
|
|
|
|
* @param {Array} omissions
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
omitRow: function (omissions) {
|
|
|
|
/**
|
|
|
|
* @param {Object} row
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
return (row) => {
|
|
|
|
return _.omit(row, omissions);
|
2023-03-17 04:23:12 +00:00
|
|
|
};
|
2023-03-17 04:18:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in objection query builder
|
|
|
|
*
|
|
|
|
* @param {Array} omissions
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
omitRows: function (omissions) {
|
|
|
|
/**
|
|
|
|
* @param {Array} rows
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
return (rows) => {
|
|
|
|
rows.forEach((row, idx) => {
|
|
|
|
rows[idx] = _.omit(row, omissions);
|
|
|
|
});
|
|
|
|
return rows;
|
2023-03-17 04:23:12 +00:00
|
|
|
};
|
2023-03-17 04:18:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Object} Liquid render engine
|
|
|
|
*/
|
|
|
|
getRenderEngine: function () {
|
|
|
|
const renderEngine = new Liquid({
|
|
|
|
root: __dirname + '/../templates/'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nginxAccessRule expects the object given to have 2 properties:
|
|
|
|
*
|
|
|
|
* directive string
|
|
|
|
* address string
|
|
|
|
*/
|
|
|
|
renderEngine.registerFilter('nginxAccessRule', (v) => {
|
|
|
|
if (typeof v.directive !== 'undefined' && typeof v.address !== 'undefined' && v.directive && v.address) {
|
|
|
|
return `${v.directive} ${v.address};`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
|
|
|
return renderEngine;
|
2020-02-19 04:55:06 +00:00
|
|
|
}
|
|
|
|
};
|