mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-22 14:27:18 +00:00

There are a lot of new rules since the last time I revised the ESLint rules documentation. I've updated the main `.eslintrc.yml` to include some newer additions, plus I added another ESLint configuration file inside `tests`, so we can add some stricted rules to the production code while relaxing them for the test suite (due to the fact that Mocha is not very ES6 friendly and Angular tests require a bit of dark magic to setup). This is a summary of the most important changes: - Disallow "magic numbers" These should now be extracted to constants, which forces us to think of a good name for them, and thus make the code more self-documenting (I had to Google up the meaning of some existing magic numbers, so I guess this will be great for readability purposes). - Require consistent `return` statements Some functions relied on JavaScript relaxed casting mechanism to work, which now have explicit return values. This flag also helped me detect some promises that were not being returned, and therefore risked not being caught by the exception handlers in case of errors. - Disallow redefining function arguments Immutability makes functions easier to reason about. - Enforce JavaScript string templates instead of string concatenation We were heavily mixing boths across the codebase. There are some extra rules that I tweaked, however most of codebase changes in this commit are related to the rules mentioned above. Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
/*
|
|
* Copyright 2016 resin.io
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
|
|
/**
|
|
* @summary Get the explicit boolean form of an argument
|
|
* @function
|
|
* @private
|
|
*
|
|
* @description
|
|
* We refer as "explicit boolean form of an argument" to a boolean
|
|
* argument in either normal or negated form.
|
|
*
|
|
* For example: `--check` and `--no-check`;
|
|
*
|
|
* @param {String} argumentName - argument name
|
|
* @param {Boolean} value - argument value
|
|
* @returns {String} argument
|
|
*
|
|
* @example
|
|
* console.log(cli.getBooleanArgumentForm('check', true));
|
|
* > '--check'
|
|
*
|
|
* @example
|
|
* console.log(cli.getBooleanArgumentForm('check', false));
|
|
* > '--no-check'
|
|
*/
|
|
exports.getBooleanArgumentForm = (argumentName, value) => {
|
|
const prefix = _.attempt(() => {
|
|
if (!value) {
|
|
return '--no-';
|
|
}
|
|
|
|
const SHORT_OPTION_LENGTH = 1;
|
|
if (_.size(argumentName) === SHORT_OPTION_LENGTH) {
|
|
return '-';
|
|
}
|
|
|
|
return '--';
|
|
});
|
|
|
|
return prefix + argumentName;
|
|
};
|
|
|
|
/**
|
|
* @summary Get CLI writer arguments
|
|
* @function
|
|
* @public
|
|
*
|
|
* @param {Object} options - options
|
|
* @param {String} options.image - image
|
|
* @param {String} options.device - device
|
|
* @param {String} options.entryPoint - entry point
|
|
* @param {Boolean} [options.validateWriteOnSuccess] - validate write on success
|
|
* @param {Boolean} [options.unmountOnSuccess] - unmount on success
|
|
* @returns {String[]} arguments
|
|
*
|
|
* @example
|
|
* const argv = cli.getArguments({
|
|
* image: 'path/to/rpi.img',
|
|
* device: '/dev/disk2'
|
|
* entryPoint: 'path/to/app.asar',
|
|
* validateWriteOnSuccess: true,
|
|
* unmountOnSuccess: true
|
|
* });
|
|
*/
|
|
exports.getArguments = (options) => {
|
|
const argv = [
|
|
options.entryPoint,
|
|
options.image,
|
|
'--drive',
|
|
options.device,
|
|
|
|
// Explicitly set the boolean 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.
|
|
exports.getBooleanArgumentForm('unmount', options.unmountOnSuccess),
|
|
exports.getBooleanArgumentForm('check', options.validateWriteOnSuccess)
|
|
|
|
];
|
|
|
|
return argv;
|
|
};
|