From 050d187e6f0c5461976e008d6215f75f2f139a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Daniel=20Mih=C4=83il=C4=83?= Date: Fri, 6 Jan 2017 18:42:11 +0200 Subject: [PATCH] refactor: extract SelectionStateModel stateless functions (#982) `SelectionStateModel`'s methods `isSystemDrive` and `isDriveLocked` don't depend on application state. They have been extracted in a different AngularJS service: `DriveConstraintsModel`. The new service's actual implementation is in `lib/src`, in order to be reused by the CLI. Miscellaneous changes: - Rename `lib/src` to `lib/shared` - Refactor `drive-constraints` to throw when image is undefined The default behaviour was to pretend that we're all good if the if the image is not specified. We're not using this "feature", and it can be dangerous if we forget to pass in the image. - Make `isSystemDrive` return `false` if `system` property is undefined - Use `drive-constraints` in `store.js` Change-Type: patch --- docs/ARCHITECTURE.md | 2 +- docs/CLI.md | 4 +- lib/cli/cli.js | 2 +- lib/cli/etcher.js | 2 +- lib/gui/app.js | 2 +- .../controllers/drive-selector.js | 19 +- .../drive-selector/drive-selector.js | 1 + .../templates/drive-selector-modal.tpl.html | 12 +- lib/gui/models/drive-constraints.js | 37 +++ lib/gui/models/selection-state.js | 159 ++--------- lib/gui/models/store.js | 25 +- lib/gui/modules/image-writer.js | 2 +- lib/{src => shared}/child-writer/README.md | 0 lib/{src => shared}/child-writer/constants.js | 0 lib/{src => shared}/child-writer/index.js | 0 .../child-writer/renderer-utils.js | 0 lib/{src => shared}/child-writer/utils.js | 0 .../child-writer/writer-proxy.js | 0 lib/shared/drive-constraints.js | 149 ++++++++++ lib/{src => shared}/exit-codes.js | 0 package.json | 2 +- tests/gui/models/drive-constraints.spec.js | 28 ++ tests/gui/models/selection-state.spec.js | 255 ++---------------- tests/shared/drive-constraints.spec.js | 242 +++++++++++++++++ 24 files changed, 529 insertions(+), 414 deletions(-) create mode 100644 lib/gui/models/drive-constraints.js rename lib/{src => shared}/child-writer/README.md (100%) rename lib/{src => shared}/child-writer/constants.js (100%) rename lib/{src => shared}/child-writer/index.js (100%) rename lib/{src => shared}/child-writer/renderer-utils.js (100%) rename lib/{src => shared}/child-writer/utils.js (100%) rename lib/{src => shared}/child-writer/writer-proxy.js (100%) create mode 100644 lib/shared/drive-constraints.js rename lib/{src => shared}/exit-codes.js (100%) create mode 100644 tests/gui/models/drive-constraints.spec.js create mode 100644 tests/shared/drive-constraints.spec.js diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 901684fb..6965d929 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -108,7 +108,7 @@ be documented instead! [lego-blocks]: https://github.com/sindresorhus/ama/issues/10#issuecomment-117766328 [etcher-image-write]: https://github.com/resin-io-modules/etcher-image-write -[exit-codes]: https://github.com/resin-io/etcher/blob/master/lib/src/exit-codes.js +[exit-codes]: https://github.com/resin-io/etcher/blob/master/lib/shared/exit-codes.js [cli-dir]: https://github.com/resin-io/etcher/tree/master/lib/cli [gui-dir]: https://github.com/resin-io/etcher/tree/master/lib/gui [electron]: http://electron.atom.io diff --git a/docs/CLI.md b/docs/CLI.md index 43b74479..2f266661 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -71,7 +71,7 @@ Exit codes ---------- The Etcher CLI uses certain exit codes to signal the result of the operation. -These are documented in [`lib/src/exit-codes.js`][exit-codes] and are also +These are documented in [`lib/shared/exit-codes.js`][exit-codes] and are also printed on the Etcher CLI help page. -[exit-codes]: https://github.com/resin-io/etcher/blob/master/lib/src/exit-codes.js +[exit-codes]: https://github.com/resin-io/etcher/blob/master/lib/shared/exit-codes.js diff --git a/lib/cli/cli.js b/lib/cli/cli.js index 72fa2155..da2c2cff 100644 --- a/lib/cli/cli.js +++ b/lib/cli/cli.js @@ -20,7 +20,7 @@ const _ = require('lodash'); const fs = require('fs'); const yargs = require('yargs'); const utils = require('./utils'); -const EXIT_CODES = require('../src/exit-codes'); +const EXIT_CODES = require('../shared/exit-codes'); const packageJSON = require('../../package.json'); /** diff --git a/lib/cli/etcher.js b/lib/cli/etcher.js index 2e5b6af1..127caa98 100644 --- a/lib/cli/etcher.js +++ b/lib/cli/etcher.js @@ -25,7 +25,7 @@ const drivelist = Bluebird.promisifyAll(require('drivelist')); const writer = require('./writer'); const utils = require('./utils'); const options = require('./cli'); -const EXIT_CODES = require('../src/exit-codes'); +const EXIT_CODES = require('../shared/exit-codes'); isElevated().then((elevated) => { if (!elevated) { diff --git a/lib/gui/app.js b/lib/gui/app.js index 59f29afc..e0cd15f2 100644 --- a/lib/gui/app.js +++ b/lib/gui/app.js @@ -24,7 +24,7 @@ var angular = require('angular'); const electron = require('electron'); -const EXIT_CODES = require('../src/exit-codes'); +const EXIT_CODES = require('../shared/exit-codes'); /* eslint-enable no-var */ diff --git a/lib/gui/components/drive-selector/controllers/drive-selector.js b/lib/gui/components/drive-selector/controllers/drive-selector.js index 16e0812f..604f4fe4 100644 --- a/lib/gui/components/drive-selector/controllers/drive-selector.js +++ b/lib/gui/components/drive-selector/controllers/drive-selector.js @@ -18,7 +18,13 @@ const _ = require('lodash'); -module.exports = function($q, $uibModalInstance, DrivesModel, SelectionStateModel, WarningModalService) { +module.exports = function( + $q, + $uibModalInstance, + DrivesModel, + SelectionStateModel, + WarningModalService, + DriveConstraintsModel) { /** * @summary The drive selector state @@ -27,6 +33,13 @@ module.exports = function($q, $uibModalInstance, DrivesModel, SelectionStateMode */ this.state = SelectionStateModel; + /** + * @summary Static methods to check a drive's properties + * @property + * @type Object + */ + this.constraints = DriveConstraintsModel; + /** * @summary The drives model * @property @@ -55,11 +68,11 @@ module.exports = function($q, $uibModalInstance, DrivesModel, SelectionStateMode * }); */ const shouldChangeDriveSelectionState = (drive) => { - if (!SelectionStateModel.isDriveValid(drive)) { + if (!DriveConstraintsModel.isDriveValid(drive, SelectionStateModel.getImage())) { return $q.resolve(false); } - if (SelectionStateModel.isDriveSizeRecommended(drive)) { + if (DriveConstraintsModel.isDriveSizeRecommended(drive, SelectionStateModel.getImage())) { return $q.resolve(true); } diff --git a/lib/gui/components/drive-selector/drive-selector.js b/lib/gui/components/drive-selector/drive-selector.js index fe233181..14a7c3ad 100644 --- a/lib/gui/components/drive-selector/drive-selector.js +++ b/lib/gui/components/drive-selector/drive-selector.js @@ -27,6 +27,7 @@ const DriveSelector = angular.module(MODULE_NAME, [ require('../warning-modal/warning-modal'), require('../../models/drives'), require('../../models/selection-state'), + require('../../models/drive-constraints'), require('../../utils/byte-size/byte-size') ]); diff --git a/lib/gui/components/drive-selector/templates/drive-selector-modal.tpl.html b/lib/gui/components/drive-selector/templates/drive-selector-modal.tpl.html index 33671fb5..bac07241 100644 --- a/lib/gui/components/drive-selector/templates/drive-selector-modal.tpl.html +++ b/lib/gui/components/drive-selector/templates/drive-selector-modal.tpl.html @@ -6,7 +6,7 @@