From c9e335ea2144c25c410c786b2d10e521935418df Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Fri, 26 Apr 2019 11:13:36 +0200 Subject: [PATCH] refactor(@embark/embark-utils): move runCmd utility to utils package --- packages/embark-utils/package.json | 3 +- packages/embark-utils/src/index.js | 40 +++++++++++++++++++ .../embark/src/lib/modules/console/index.ts | 4 +- .../src/lib/modules/plugin_cmd/index.js | 4 +- .../embark/src/lib/modules/tests/index.js | 3 +- .../src/lib/utils/template_generator.js | 4 +- packages/embark/src/lib/utils/utils.js | 39 ------------------ 7 files changed, 50 insertions(+), 47 deletions(-) diff --git a/packages/embark-utils/package.json b/packages/embark-utils/package.json index 9b50baeb0..71efcdbe0 100644 --- a/packages/embark-utils/package.json +++ b/packages/embark-utils/package.json @@ -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", diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index dda88ae3a..0565606b5 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -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') diff --git a/packages/embark/src/lib/modules/console/index.ts b/packages/embark/src/lib/modules/console/index.ts index 0774f465f..abb05437e 100644 --- a/packages/embark/src/lib/modules/console/index.ts +++ b/packages/embark/src/lib/modules/console/index.ts @@ -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; } diff --git a/packages/embark/src/lib/modules/plugin_cmd/index.js b/packages/embark/src/lib/modules/plugin_cmd/index.js index e6668aa2e..196e35b6b 100644 --- a/packages/embark/src/lib/modules/plugin_cmd/index.js +++ b/packages/embark/src/lib/modules/plugin_cmd/index.js @@ -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); } diff --git a/packages/embark/src/lib/modules/tests/index.js b/packages/embark/src/lib/modules/tests/index.js index 0387955a0..c698f766b 100644 --- a/packages/embark/src/lib/modules/tests/index.js +++ b/packages/embark/src/lib/modules/tests/index.js @@ -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'); diff --git a/packages/embark/src/lib/utils/template_generator.js b/packages/embark/src/lib/utils/template_generator.js index 8905f8568..119d5acba 100644 --- a/packages/embark/src/lib/utils/template_generator.js +++ b/packages/embark/src/lib/utils/template_generator.js @@ -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); } diff --git a/packages/embark/src/lib/utils/utils.js b/packages/embark/src/lib/utils/utils.js index dc88dd569..e70a9c3c8 100644 --- a/packages/embark/src/lib/utils/utils.js +++ b/packages/embark/src/lib/utils/utils.js @@ -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,