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');
|
|
|
|
import ResinSdk = require('resin-sdk');
|
|
|
|
import packageJSON = require('../package.json');
|
|
|
|
|
|
|
|
const resin = ResinSdk.fromSharedOptions();
|
|
|
|
const getMatchCommandAsync = Promise.promisify(Capitano.state.getMatchCommand);
|
2018-01-09 15:05:24 +00:00
|
|
|
const getMixpanel = _.memoize<any>(() =>
|
|
|
|
resin.models.config
|
|
|
|
.getAll()
|
|
|
|
.get('mixpanelToken')
|
|
|
|
.then(Mixpanel.init),
|
|
|
|
);
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
export function trackCommand(capitanoCli: Capitano.Cli) {
|
|
|
|
return Promise.props({
|
|
|
|
resinUrl: resin.settings.get('resinUrl'),
|
|
|
|
username: resin.auth.whoami().catchReturn(undefined),
|
2018-01-04 14:07:55 +00:00
|
|
|
mixpanel: getMixpanel(),
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.then(({ username, resinUrl, mixpanel }) => {
|
|
|
|
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,
|
|
|
|
resinUrl,
|
|
|
|
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
|
|
|
}
|