From 3528e8e146587506aa0e6597d7bc14b14eae9c18 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 11 Feb 2022 01:20:53 +0100 Subject: [PATCH] moved command execution to own .js file --- libs/commands.js | 26 ++++++++++++++++++++++++++ libs/modep.js | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 libs/commands.js diff --git a/libs/commands.js b/libs/commands.js new file mode 100644 index 0000000..a759739 --- /dev/null +++ b/libs/commands.js @@ -0,0 +1,26 @@ +const logger = require('./logger.js'); +const { spawn } = require('child_process'); + +function execute(cmd, args) { + return new Promise(function (resolve, reject) { + var spawned = spawn(cmd, args); + spawned.stdout.on('data', function (data) { + logger.debug(data); + }); + spawned.stderr.on('data', function (data) { + logger.error(data); + }); + spawned.on('close', function (code) { + logger.debug('command \'' + cmd + '\' with args \'' + args + '\' finished with exit code ' + code); + resolve(); + }); + spawned.on('error', function (err) { + logger.error('command \'' + cmd + '\' with args \'' + args + '\' encountered an error >>> ' + err); + reject(err); + }); + }); +} + +module.exports = { + execute +} \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index aa074e2..3f43b09 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -3,9 +3,9 @@ const util = require('./util.js'); const constants = require('./constants.js'); const logger = require('./logger.js'); const cache = require('./cache.js'); +const commands = require('./commands.js'); const path = require('path'); const fs = require('fs'); -const { spawn } = require('child_process'); const ttl2jsonld = require('@frogcat/ttl2jsonld').parse; function reset() {