2017-12-20 21:46:01 +00:00
|
|
|
import * as Capitano from 'capitano';
|
|
|
|
|
|
|
|
import _ = require('lodash');
|
|
|
|
import Mixpanel = require('mixpanel');
|
|
|
|
import Raven = require('raven');
|
|
|
|
import Promise = require('bluebird');
|
2018-10-19 14:38:50 +00:00
|
|
|
import BalenaSdk = require('balena-sdk');
|
2017-12-20 21:46:01 +00:00
|
|
|
import packageJSON = require('../package.json');
|
|
|
|
|
2019-01-12 21:20:19 +00:00
|
|
|
const getBalenaSdk = _.once(() => BalenaSdk.fromSharedOptions());
|
2017-12-20 21:46:01 +00:00
|
|
|
const getMatchCommandAsync = Promise.promisify(Capitano.state.getMatchCommand);
|
2019-02-22 07:59:36 +00:00
|
|
|
const getMixpanel = _.once<any>(() => {
|
2019-02-07 15:36:48 +00:00
|
|
|
const settings = require('balena-settings-client');
|
|
|
|
return Mixpanel.init('00000000000000000000000000000000', {
|
|
|
|
host: `api.${settings.get('balenaUrl')}`,
|
|
|
|
path: '/mixpanel',
|
|
|
|
protocol: 'https',
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
export function trackCommand(capitanoCli: Capitano.Cli) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2017-12-20 21:46:01 +00:00
|
|
|
return Promise.props({
|
2018-10-19 14:38:50 +00:00
|
|
|
balenaUrl: balena.settings.get('balenaUrl'),
|
|
|
|
username: balena.auth.whoami().catchReturn(undefined),
|
2018-01-04 14:07:55 +00:00
|
|
|
mixpanel: getMixpanel(),
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(({ username, balenaUrl, mixpanel }) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
return getMatchCommandAsync(capitanoCli.command).then(command => {
|
|
|
|
Raven.mergeContext({
|
|
|
|
user: {
|
|
|
|
id: username,
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
return mixpanel.track(`[CLI] ${command.signature.toString()}`, {
|
|
|
|
distinct_id: username,
|
|
|
|
argv: process.argv.join(' '),
|
|
|
|
version: packageJSON.version,
|
|
|
|
node: process.version,
|
|
|
|
arch: process.arch,
|
2018-10-19 14:38:50 +00:00
|
|
|
balenaUrl,
|
2018-01-09 15:05:24 +00:00
|
|
|
platform: process.platform,
|
|
|
|
command: capitanoCli,
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.timeout(100)
|
|
|
|
.catchReturn(undefined);
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|