refactor(@embark/embark-utils): move runCmd utility to utils package

This commit is contained in:
Pascal Precht 2019-04-26 11:13:36 +02:00 committed by Pascal Precht
parent 70a528c296
commit c9e335ea21
7 changed files with 50 additions and 47 deletions

View File

@ -49,7 +49,8 @@
"follow-redirects": "1.5.7",
"merge": "1.2.1",
"multihashes": "0.4.14",
"web3": "1.0.0-beta.37"
"web3": "1.0.0-beta.37",
"shelljs": "0.5.3"
},
"devDependencies": {
"@babel/cli": "7.2.3",

View File

@ -1,5 +1,6 @@
const http = require('follow-redirects').http;
const https = require('follow-redirects').https;
const shelljs = require('shelljs');
const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host');
const {findNextPort} = require('./network');
@ -53,6 +54,43 @@ function sha512(arg) {
return hash.update(arg).digest('hex');
}
function exit(code) {
process.exit(code);
}
function runCmd(cmd, options, callback) {
options = Object.assign({silent: true, exitOnError: true, async: true}, options || {});
const outputToConsole = !options.silent;
options.silent = true;
let result = shelljs.exec(cmd, options, function (code, stdout) {
if(code !== 0) {
if (options.exitOnError) {
return exit();
}
if(typeof callback === 'function') {
callback(`shell returned code ${code}`);
}
} else {
if(typeof callback === 'function') {
return callback(null, stdout);
}
}
});
result.stdout.on('data', function(data) {
if(outputToConsole) {
console.log(data);
}
});
result.stderr.on('data', function(data) {
if (outputToConsole) {
console.log(data);
}
});
}
const Utils = {
joinPath: function() {
const path = require('path');
@ -62,6 +100,7 @@ const Utils = {
defaultCorsHost,
defaultHost,
dockerHostSwap,
exit,
isDocker,
checkIsAvailable,
findNextPort,
@ -70,6 +109,7 @@ const Utils = {
soliditySha3,
recursiveMerge,
sha512,
runCmd,
escapeHtml: logUtils.escapeHtml,
normalizeInput: logUtils.normalizeInput,
LogHandler: require('./logHandler')

View File

@ -1,7 +1,7 @@
/*globals __*/
const env = require("../../core/env");
const utils = require("../../utils/utils");
const {escapeHtml} = require("embark-utils");
const {escapeHtml, exit} = require("embark-utils");
import { Callback } from "embark";
import constants from "../../constants.json";
const stringify = require("json-stringify-safe");
@ -143,7 +143,7 @@ class Console {
__("The web3 object and the interfaces for the deployed contracts and their methods are also available"));
return helpText.join("\n");
} else if (["quit", "exit", "sair", "sortir", __("quit")].indexOf(cmd) >= 0) {
utils.exit();
exit();
}
return false;
}

View File

@ -1,4 +1,4 @@
const utils = require('./../../utils/utils.js');
const { runCmd } = require('embark-utils');
const async = require('async');
class PluginCommand {
@ -41,7 +41,7 @@ class PluginCommand {
self.embark.logger.info(__('Installing npm package %s...', npmPackage));
async.waterfall([
function npmInstallAsync(cb) {
utils.runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => {
runCmd(npmInstall.join(' '), {silent: false, exitOnError: false}, (err) => {
if (err) {
return cb(err);
}

View File

@ -1,7 +1,8 @@
const async = require('async');
const Mocha = require('mocha');
const path = require('path');
const {runCmd, timer} = require('../../utils/utils');
const {timer} = require('../../utils/utils');
const { runCmd } = require('embark-utils');
const assert = require('assert');
const Test = require('./test');
const {EmbarkSpec, EmbarkApiSpec} = require('./reporter');

View File

@ -2,7 +2,7 @@ const findUp = require('find-up');
const fs = require('../core/fs.js');
const hostedGitInfo = require('hosted-git-info');
const utils = require('./utils.js');
import {joinPath} from 'embark-utils';
import {joinPath, runCmd} from 'embark-utils';
const semver = require('semver');
const {promisify} = require('util');
const {execSync} = require('child_process');
@ -224,7 +224,7 @@ class TemplateGenerator {
}
}
utils.runCmd('npm install', {exitOnError: false}, (err) => {
runCmd('npm install', {exitOnError: false}, (err) => {
if (err) {
console.error(__('Could not install dependencies. Try running `npm install` inside the project directory.').red);
}

View File

@ -129,39 +129,6 @@ function pingEndpoint(host, port, type, protocol, origin, callback) {
}
}
function runCmd(cmd, options, callback) {
const shelljs = require('shelljs');
options = Object.assign({silent: true, exitOnError: true, async: true}, options || {});
const outputToConsole = !options.silent;
options.silent = true;
let result = shelljs.exec(cmd, options, function (code, stdout) {
if(code !== 0) {
if (options.exitOnError) {
return exit();
}
if(typeof callback === 'function') {
callback(`shell returned code ${code}`);
}
} else {
if(typeof callback === 'function') {
return callback(null, stdout);
}
}
});
result.stdout.on('data', function(data) {
if(outputToConsole) {
console.log(data);
}
});
result.stderr.on('data', function(data) {
if (outputToConsole) {
console.log(data);
}
});
}
function cd(folder) {
const shelljs = require('shelljs');
shelljs.cd(folder);
@ -172,10 +139,6 @@ function sed(file, pattern, replace) {
shelljs.sed('-i', pattern, replace, file);
}
function exit(code) {
process.exit(code);
}
function downloadFile(url, dest, cb) {
const o_fs = require('fs-extra');
var file = o_fs.createWriteStream(dest);
@ -581,10 +544,8 @@ module.exports = {
isValidDomain,
pingEndpoint,
decodeParams,
runCmd,
cd,
sed,
exit,
downloadFile,
extractTar,
extractZip,