From 122f136ff819019bf27b26630053cd5c98ec25a7 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 22 Jan 2016 11:29:26 -0400 Subject: [PATCH] Refactor Etcher using ES6 features --- .jshintrc | 2 +- gulpfile.js | 20 ++++----- lib/browser/app.js | 30 ++++++------- lib/browser/modules/drive-scanner.js | 24 +++++----- lib/browser/modules/image-writer.js | 16 +++---- lib/browser/modules/path.js | 9 ++-- lib/browser/modules/selection-state.js | 26 +++++------ lib/components/hero-badge.html | 2 +- lib/components/hero-button.html | 6 ++- lib/components/hero-caption.html | 2 +- lib/components/hero-icon.html | 14 +++--- lib/components/hero-progress-button.html | 8 +++- lib/components/hero-tick.html | 8 ++-- lib/elevate.js | 18 ++++---- lib/etcher.js | 13 +++--- lib/src/dialog.js | 17 +++---- lib/src/drives.js | 8 ++-- lib/src/writer.js | 24 +++++----- tests/browser/modules/drive-scanner.spec.js | 35 ++++++++------- tests/browser/modules/image-writer.spec.js | 17 +++---- tests/browser/modules/path.spec.js | 15 ++++--- tests/browser/modules/selection-state.spec.js | 45 ++++++++++--------- tests/src/dialog.spec.js | 11 ++--- tests/src/drives.spec.js | 19 ++++---- tests/src/writer.spec.js | 17 +++---- 25 files changed, 203 insertions(+), 203 deletions(-) diff --git a/.jshintrc b/.jshintrc index cf86db2c..d730076b 100644 --- a/.jshintrc +++ b/.jshintrc @@ -59,7 +59,7 @@ "evil" : false, // Tolerate use of `eval`. "expr" : true, // Tolerate `ExpressionStatement` as Programs. "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside. - "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). + "globalstrict" : true, // Allow global "use strict" (also enables 'strict'). "iterator" : false, // Allow usage of __iterator__ property. "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block. "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. diff --git a/gulpfile.js b/gulpfile.js index 9ed51bbd..871c033c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,13 +14,15 @@ * limitations under the License. */ -var gulp = require('gulp'); -var jscs = require('gulp-jscs'); -var jshint = require('gulp-jshint'); -var jshintStylish = require('jshint-stylish'); -var sass = require('gulp-sass'); +'use strict'; -var paths = { +const gulp = require('gulp'); +const jscs = require('gulp-jscs'); +const jshint = require('gulp-jshint'); +const jshintStylish = require('jshint-stylish'); +const sass = require('gulp-sass'); + +const paths = { scripts: [ './tests/**/*.spec.js', './lib/**/*.js', @@ -32,16 +34,12 @@ var paths = { }; gulp.task('sass', function() { - 'use strict'; - return gulp.src(paths.sass) .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest('./build/css')); }); gulp.task('lint', function() { - 'use strict'; - return gulp.src(paths.scripts) .pipe(jshint()) .pipe(jshint.reporter(jshintStylish)) @@ -50,8 +48,6 @@ gulp.task('lint', function() { }); gulp.task('watch', [ 'lint', 'sass' ], function() { - 'use strict'; - gulp.watch(paths.scripts, [ 'lint' ]); gulp.watch(paths.sass, [ 'sass' ]); }); diff --git a/lib/browser/app.js b/lib/browser/app.js index be9e4c51..bd66d95f 100644 --- a/lib/browser/app.js +++ b/lib/browser/app.js @@ -18,11 +18,13 @@ * @module ResinEtcher */ +'use strict'; + var angular = require('angular'); -var _ = require('lodash'); -var electron = require('electron'); -var shell = electron.remote.require('shell'); -var dialog = electron.remote.require('./src/dialog'); +const _ = require('lodash'); +const electron = require('electron'); +const shell = electron.remote.require('shell'); +const dialog = electron.remote.require('./src/dialog'); require('angular-ui-bootstrap'); require('./browser/modules/selection-state'); @@ -30,7 +32,7 @@ require('./browser/modules/drive-scanner'); require('./browser/modules/image-writer'); require('./browser/modules/path'); -var app = angular.module('ResinEtcher', [ +const app = angular.module('ResinEtcher', [ 'ui.bootstrap', // Resin Etcher modules @@ -43,8 +45,6 @@ var app = angular.module('ResinEtcher', [ // TrackJS integration // http://docs.trackjs.com/tracker/framework-integrations app.config(function($provide) { - 'use strict'; - $provide.decorator('$exceptionHandler', function($delegate, $window) { return function(exception, cause) { $window.trackJs.track(exception); @@ -55,7 +55,7 @@ app.config(function($provide) { $provide.decorator('$log', function($delegate, $window) { // Save the original $log.debug() - var debugFn = $delegate.debug; + let debugFn = $delegate.debug; $delegate.debug = function(message) { message = new Date() + ' ' + message; @@ -68,9 +68,7 @@ app.config(function($provide) { }); app.controller('AppController', function($q, $log, DriveScannerService, SelectionStateService, ImageWriterService) { - 'use strict'; - - var self = this; + let self = this; this.selection = SelectionStateService; this.writer = ImageWriterService; this.scanner = DriveScannerService; @@ -86,14 +84,14 @@ app.controller('AppController', function($q, $log, DriveScannerService, Selectio // otherwise the drive is selected while the drive step is disabled // which looks very weird. if (drives.length === 1 && self.selection.hasImage()) { - var drive = _.first(drives); + const drive = _.first(drives); // Do not autoselect the same drive over and over again // and fill the logs unnecessary. // `angular.equals` is used instead of `_.isEqual` to // cope with `$$hashKey`. if (!angular.equals(self.selection.getDrive(), drive)) { - $log.debug('Autoselecting drive: ' + drive.device); + $log.debug(`Autoselecting drive: ${drive.device}`); self.selectDrive(drive); } @@ -117,13 +115,13 @@ app.controller('AppController', function($q, $log, DriveScannerService, Selectio this.selectImage = function() { return $q.when(dialog.selectImage()).then(function(image) { self.selection.setImage(image); - $log.debug('Image selected: ' + image); + $log.debug(`Image selected: ${image}`); }); }; this.selectDrive = function(drive) { self.selection.setDrive(drive); - $log.debug('Drive selected: ' + drive.device); + $log.debug(`Drive selected: ${drive.device}`); }; this.reselectImage = function() { @@ -155,7 +153,7 @@ app.controller('AppController', function($q, $log, DriveScannerService, Selectio // otherwise Windows throws EPERM self.scanner.stop(); - $log.debug('Burning ' + image + ' to ' + drive.device); + $log.debug(`Burning ${image} to ${drive.device}`); return self.writer.burn(image, drive).then(function() { $log.debug('Done!'); }).catch(dialog.showError); diff --git a/lib/browser/modules/drive-scanner.js b/lib/browser/modules/drive-scanner.js index dca53c09..b2d11337 100644 --- a/lib/browser/modules/drive-scanner.js +++ b/lib/browser/modules/drive-scanner.js @@ -14,14 +14,16 @@ * limitations under the License. */ +'use strict'; + /** * @module ResinEtcher.drive-scanner */ -var angular = require('angular'); -var _ = require('lodash'); -var EventEmitter = require('events').EventEmitter; -var electron = require('electron'); +const angular = require('angular'); +const _ = require('lodash'); +const EventEmitter = require('events').EventEmitter; +const electron = require('electron'); if (window.mocha) { var path = require('path'); @@ -33,12 +35,10 @@ if (window.mocha) { var dialog = electron.remote.require('./src/dialog'); } -var driveScanner = angular.module('ResinEtcher.drive-scanner', []); +const driveScanner = angular.module('ResinEtcher.drive-scanner', []); driveScanner.service('DriveScannerRefreshService', function($interval, $timeout) { - 'use strict'; - - var interval = null; + let interval = null; /** * @summary Run a function every certain milliseconds @@ -80,9 +80,7 @@ driveScanner.service('DriveScannerRefreshService', function($interval, $timeout) }); driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshService) { - 'use strict'; - - var self = this; + let self = this; /** * @summary List of available drives @@ -160,14 +158,14 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ * @returns {EventEmitter} event emitter instance * * @example - * var emitter = DriveScannerService.start(2000); + * const emitter = DriveScannerService.start(2000); * * emitter.on('scan', function(drives) { * console.log(drives); * }); */ this.start = function(ms) { - var emitter = new EventEmitter(); + let emitter = new EventEmitter(); DriveScannerRefreshService.every(function() { return self.scan().then(function(drives) { diff --git a/lib/browser/modules/image-writer.js b/lib/browser/modules/image-writer.js index 5a6c7197..9b6f9363 100644 --- a/lib/browser/modules/image-writer.js +++ b/lib/browser/modules/image-writer.js @@ -14,12 +14,14 @@ * limitations under the License. */ +'use strict'; + /** * @module ResinEtcher.image-writer */ -var angular = require('angular'); -var electron = require('electron'); +const angular = require('angular'); +const electron = require('electron'); if (window.mocha) { var writer = electron.remote.require(require('path').join(__dirname, '..', '..', 'src', 'writer')); @@ -27,13 +29,11 @@ if (window.mocha) { var writer = electron.remote.require('./src/writer'); } -var imageWriter = angular.module('ResinEtcher.image-writer', []); +const imageWriter = angular.module('ResinEtcher.image-writer', []); imageWriter.service('ImageWriterService', function($q, $timeout, $log) { - 'use strict'; - - var self = this; - var burning = false; + let self = this; + let burning = false; /* * @summary Progress state @@ -80,7 +80,7 @@ imageWriter.service('ImageWriterService', function($q, $timeout, $log) { // Transform bytes to megabytes preserving only two decimal places self.state.speed = Math.floor(state.speed / 1e+6 * 100) / 100 || 0; - $log.debug('Progress: ' + self.state.progress + '% at ' + self.state.speed + ' MB/s'); + $log.debug(`Progress: ${self.state.progress}% at ${self.state.speed} MB/s`); }); }; diff --git a/lib/browser/modules/path.js b/lib/browser/modules/path.js index 05fe64d7..2450e92e 100644 --- a/lib/browser/modules/path.js +++ b/lib/browser/modules/path.js @@ -14,17 +14,18 @@ * limitations under the License. */ +'use strict'; + /** * @module ResinEtcher.path */ -var angular = require('angular'); -var path = require('path'); +const angular = require('angular'); +const path = require('path'); -var pathModule = angular.module('ResinEtcher.path', []); +const pathModule = angular.module('ResinEtcher.path', []); pathModule.filter('basename', function() { - 'use strict'; /** * @summary Get the basename of a path diff --git a/lib/browser/modules/selection-state.js b/lib/browser/modules/selection-state.js index 9f287500..80c5b524 100644 --- a/lib/browser/modules/selection-state.js +++ b/lib/browser/modules/selection-state.js @@ -14,25 +14,25 @@ * limitations under the License. */ +'use strict'; + /** * @module ResinEtcher.selection-state */ -var _ = require('lodash'); -var angular = require('angular'); -var selectionState = angular.module('ResinEtcher.selection-state', []); +const _ = require('lodash'); +const angular = require('angular'); +const selectionState = angular.module('ResinEtcher.selection-state', []); selectionState.service('SelectionStateService', function() { - 'use strict'; - - var self = this; + let self = this; /** * @summary Selection state * @type Object * @private */ - var selection = {}; + let selection = {}; /** * @summary Set a drive @@ -72,7 +72,7 @@ selectionState.service('SelectionStateService', function() { * @returns {Object} drive * * @example - * var drive = SelectionStateService.getDrive(); + * const drive = SelectionStateService.getDrive(); */ this.getDrive = function() { return selection.drive; @@ -86,7 +86,7 @@ selectionState.service('SelectionStateService', function() { * @returns {String} image * * @example - * var image = SelectionStateService.getImage(); + * const image = SelectionStateService.getImage(); */ this.getImage = function() { return selection.image; @@ -132,9 +132,7 @@ selectionState.service('SelectionStateService', function() { * @example * SelectionStateService.removeDrive(); */ - this.removeDrive = function() { - self.setDrive(undefined); - }; + this.removeDrive = _.partial(self.setDrive, undefined); /** * @summary Remove image @@ -144,9 +142,7 @@ selectionState.service('SelectionStateService', function() { * @example * SelectionStateService.removeImage(); */ - this.removeImage = function() { - self.setImage(undefined); - }; + this.removeImage = _.partial(self.setImage, undefined); /** * @summary Clear selections diff --git a/lib/components/hero-badge.html b/lib/components/hero-badge.html index 6020ec8d..017a68fb 100644 --- a/lib/components/hero-badge.html +++ b/lib/components/hero-badge.html @@ -19,6 +19,6 @@ diff --git a/lib/components/hero-button.html b/lib/components/hero-button.html index 541c9d33..002c66ae 100644 --- a/lib/components/hero-button.html +++ b/lib/components/hero-button.html @@ -40,7 +40,7 @@ diff --git a/lib/components/hero-caption.html b/lib/components/hero-caption.html index c22d25ec..082f259c 100644 --- a/lib/components/hero-caption.html +++ b/lib/components/hero-caption.html @@ -15,6 +15,6 @@ diff --git a/lib/components/hero-icon.html b/lib/components/hero-icon.html index a2956846..49a7fd61 100644 --- a/lib/components/hero-icon.html +++ b/lib/components/hero-icon.html @@ -20,11 +20,11 @@