mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-24 23:37: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>
193 lines
4.8 KiB
JavaScript
193 lines
4.8 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';
|
|
|
|
/**
|
|
* @module Etcher.Modules.Analytics
|
|
*/
|
|
|
|
const _ = require('lodash');
|
|
const angular = require('angular');
|
|
const username = require('username');
|
|
const isRunningInAsar = require('electron-is-running-in-asar');
|
|
const app = require('electron').remote.app;
|
|
const packageJSON = require('../../../package.json');
|
|
|
|
// Force Mixpanel snippet to load Mixpanel locally
|
|
// instead of using a CDN for performance reasons
|
|
window.MIXPANEL_CUSTOM_LIB_URL = '../../bower_components/mixpanel/mixpanel.js';
|
|
|
|
require('../../../bower_components/mixpanel/mixpanel-jslib-snippet.js');
|
|
require('../../../bower_components/angular-mixpanel/src/angular-mixpanel');
|
|
const MODULE_NAME = 'Etcher.Modules.Analytics';
|
|
const analytics = angular.module(MODULE_NAME, [
|
|
'analytics.mixpanel',
|
|
require('../models/settings')
|
|
]);
|
|
|
|
// Mixpanel integration
|
|
// https://github.com/kuhnza/angular-mixpanel
|
|
|
|
analytics.config(($mixpanelProvider) => {
|
|
$mixpanelProvider.apiKey('63e5fc4563e00928da67d1226364dd4c');
|
|
|
|
$mixpanelProvider.superProperties({
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
distinct_id: username.sync(),
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
electron: app.getVersion(),
|
|
node: process.version,
|
|
arch: process.arch,
|
|
version: packageJSON.version
|
|
});
|
|
});
|
|
|
|
// TrackJS integration
|
|
// http://docs.trackjs.com/tracker/framework-integrations
|
|
|
|
analytics.run(($window) => {
|
|
|
|
// Don't configure TrackJS when
|
|
// running inside the test suite
|
|
if (window.mocha) {
|
|
return;
|
|
}
|
|
|
|
$window.trackJs.configure({
|
|
userId: username.sync(),
|
|
version: packageJSON.version
|
|
});
|
|
});
|
|
|
|
analytics.service('AnalyticsService', function($log, $window, $mixpanel, SettingsModel) {
|
|
|
|
/**
|
|
* @summary Log a debug message
|
|
* @function
|
|
* @public
|
|
*
|
|
* @description
|
|
* This function sends the debug message to TrackJS only.
|
|
*
|
|
* @param {String} message - message
|
|
*
|
|
* @example
|
|
* AnalyticsService.log('Hello World');
|
|
*/
|
|
this.logDebug = (message) => {
|
|
const debugMessage = `${new Date()} ${message}`;
|
|
|
|
if (SettingsModel.get('errorReporting') && isRunningInAsar()) {
|
|
$window.trackJs.console.debug(debugMessage);
|
|
}
|
|
|
|
$log.debug(debugMessage);
|
|
};
|
|
|
|
/**
|
|
* @summary Log an event
|
|
* @function
|
|
* @public
|
|
*
|
|
* @description
|
|
* This function sends the debug message to TrackJS and Mixpanel.
|
|
*
|
|
* @param {String} message - message
|
|
* @param {Object} [data] - event data
|
|
*
|
|
* @example
|
|
* AnalyticsService.logEvent('Select image', {
|
|
* image: '/dev/disk2'
|
|
* });
|
|
*/
|
|
this.logEvent = (message, data) => {
|
|
if (SettingsModel.get('errorReporting') && isRunningInAsar()) {
|
|
|
|
// Clone data before passing it to `mixpanel.track`
|
|
// since this function mutates the object adding
|
|
// some custom private Mixpanel properties.
|
|
$mixpanel.track(message, _.clone(data));
|
|
|
|
}
|
|
|
|
const debugMessage = _.attempt(() => {
|
|
if (data) {
|
|
return `${message} (${JSON.stringify(data)})`;
|
|
}
|
|
|
|
return message;
|
|
});
|
|
|
|
this.logDebug(debugMessage);
|
|
};
|
|
|
|
/**
|
|
* @summary Check whether an error should be reported to TrackJS
|
|
* @function
|
|
* @private
|
|
*
|
|
* @description
|
|
* In order to determine whether the error should be reported, we
|
|
* check a property called `report`. For backwards compatibility, and
|
|
* to properly handle errors that we don't control, an error without
|
|
* this property is reported automatically.
|
|
*
|
|
* @param {Error} error - error
|
|
* @returns {Boolean} whether the error should be reported
|
|
*
|
|
* @example
|
|
* if (AnalyticsService.shouldReportError(new Error('foo'))) {
|
|
* console.log('We should report this error');
|
|
* }
|
|
*/
|
|
this.shouldReportError = (error) => {
|
|
return !_.has(error, 'report') || Boolean(error.report);
|
|
};
|
|
|
|
/**
|
|
* @summary Log an exception
|
|
* @function
|
|
* @public
|
|
*
|
|
* @description
|
|
* This function logs an exception in TrackJS.
|
|
*
|
|
* @param {Error} exception - exception
|
|
*
|
|
* @example
|
|
* AnalyticsService.logException(new Error('Something happened'));
|
|
*/
|
|
this.logException = (exception) => {
|
|
if (_.every([
|
|
SettingsModel.get('errorReporting'),
|
|
isRunningInAsar(),
|
|
this.shouldReportError(exception)
|
|
])) {
|
|
$window.trackJs.track(exception);
|
|
}
|
|
|
|
$log.error(exception);
|
|
};
|
|
|
|
});
|
|
|
|
module.exports = MODULE_NAME;
|