From 5575cb1af8102fff1a47ab6222293de612ca620b Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 4 May 2016 01:57:26 -0400 Subject: [PATCH] Revert "Implement writing by spawning the CLI as a child process (#385)" (#394) This reverts commit fab2d767b0ae6b0401b428910096af236897973d. --- lib/gui/modules/image-writer.js | 83 ++++----------------------------- 1 file changed, 9 insertions(+), 74 deletions(-) diff --git a/lib/gui/modules/image-writer.js b/lib/gui/modules/image-writer.js index f580ae26..324896e5 100644 --- a/lib/gui/modules/image-writer.js +++ b/lib/gui/modules/image-writer.js @@ -21,19 +21,21 @@ */ const angular = require('angular'); -const _ = require('lodash'); const electron = require('electron'); -const childProcess = require('child_process'); -const EXIT_CODES = require('../../src/exit-codes'); + +if (window.mocha) { + var writer = electron.remote.require(require('path').join(__dirname, '..', '..', 'src', 'writer')); +} else { + var writer = electron.remote.require('./src/writer'); +} const MODULE_NAME = 'Etcher.image-writer'; const imageWriter = angular.module(MODULE_NAME, [ require('../models/settings'), - require('../modules/analytics'), require('../utils/notifier/notifier') ]); -imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel, NotifierService, AnalyticsService) { +imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel, NotifierService) { let self = this; let flashing = false; @@ -105,7 +107,6 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel, * @param {Object} drive - drive * @param {Function} onProgress - in progress callback (state) * - * @fulfil {Boolean} - whether the operation succeeded * @returns {Promise} * * @example @@ -116,73 +117,7 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel, * }); */ this.performWrite = function(image, drive, onProgress) { - const argv = _.clone(electron.remote.process.argv); - - argv.push(image); - argv.push('--drive', drive.device); - argv.push('--robot'); - - // Explicitly set the boolen flag in positive - // or negative way in order to be on the safe - // side in case the Etcher CLI changes the - // default value of these options. - - if (SettingsModel.data.unmountOnSuccess) { - argv.push('--unmount'); - } else { - argv.push('--no-unmount'); - } - - if (SettingsModel.data.validateWriteOnSuccess) { - argv.push('--check'); - } else { - argv.push('--no-check'); - } - - return $q(function(resolve, reject) { - AnalyticsService.log(`Forking: ${argv.join(' ')}`); - const child = childProcess.spawn(argv[0], argv.slice(1), { - env: { - - // This environment variable tells Etcher to run - // in CLI mode. See `lib.start.js`. - ELECTRON_RUN_AS_NODE: 1 - - } - }); - - child.stderr.on('data', function(data) { - child.kill(); - reject(new Error(data)); - }); - - child.stdout.on('data', function(data) { - const information = data.toString().split(' '); - - if (information[0] === 'progress') { - return onProgress({ - type: information[1], - percentage: _.parseInt(information[2]), - eta: _.parseInt(information[3]), - speed: _.parseInt(information[4]) - }); - } - }); - - child.on('error', reject); - - child.on('close', function(code) { - if (code === EXIT_CODES.SUCCESS) { - return resolve(true); - } - - if (code === EXIT_CODES.VALIDATION_ERROR) { - return resolve(false); - } - - return reject(new Error(`Child process exitted with error code: ${code}`)); - }); - }); + return $q.when(writer.writeImage(image, drive, SettingsModel.data, onProgress)); }; /** @@ -219,7 +154,7 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel, self.state = { type: state.type, - progress: state.percentage, + progress: Math.floor(state.percentage), // Transform bytes to megabytes preserving only two decimal places speed: Math.floor(state.speed / 1e+6 * 100) / 100 || 0