@haechi-labs/henesis-cli
Version:
🚀 Command Line Interface tool to Utilize henesis
174 lines • 6.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = tslib_1.__importDefault(require("@oclif/command"));
const os = tslib_1.__importStar(require("os"));
const configstore_1 = tslib_1.__importDefault(require("./configstore"));
const universal_analytics_1 = tslib_1.__importDefault(require("universal-analytics"));
const utils_1 = require("../utils");
const uid_generator_1 = tslib_1.__importDefault(require("uid-generator"));
const Sentry = tslib_1.__importStar(require("@sentry/node"));
// See https://developers.whatismybrowser.com/useragents/
const osVersionMap = {
darwin: {
'1.3.1': '10_0_4',
'1.4.1': '10_1_0',
'5.1': '10_1_1',
'5.2': '10_1_5',
'6.0.1': '10_2',
'6.8': '10_2_8',
'7.0': '10_3_0',
'7.9': '10_3_9',
'8.0': '10_4_0',
'8.11': '10_4_11',
'9.0': '10_5_0',
'9.8': '10_5_8',
'10.0': '10_6_0',
'10.8': '10_6_8',
},
};
/**
* init -> run -> catch -> finally
*/
class default_1 extends command_1.default {
constructor() {
super(...arguments);
this._clientInfo = [];
}
async init() {
const isAgree = configstore_1.default.get('analytics');
const { name } = this.constructor;
if (typeof isAgree === 'undefined' && name === 'Login') {
this.log(`Allow Henesis to collect CLI usage and error reporting information`);
const uidgen = new uid_generator_1.default();
const confirmed = await utils_1.confirmPrompt();
if (confirmed) {
configstore_1.default.set('analytics', (await uidgen.generate()).toString());
}
}
else {
this._clientInfo[1] = this._getOSVersion();
this._clientInfo[2] = this._getNodeVersion();
this._clientInfo[3] = this._getCPUCount();
this._clientInfo[4] = this._getRAM();
this._clientInfo[5] = this._cliVersion();
this._clientInfo[6] = this._getUserId();
const additionals = {};
this._clientInfo.forEach((v, i) => (additionals['cd' + i] = v));
const data = configstore_1.default.get('analytics');
const visitor = universal_analytics_1.default('UA-126138188-2', this._getUserId(), {
uid: data,
strictCidFormat: false,
});
const commandPath = this.id != undefined ? this.id.split(':').join('/') : '';
visitor
.pageview(Object.assign({ dp: `/command/${commandPath}`, dt: 'Henesis CLI' }, additionals))
.send();
}
}
async catch(err) {
const email = this._getUserEmal();
if (!process.env.HENESIS_TEST && !email.includes('@haechi.io')) {
if (typeof err.code === 'undefined') {
await this.reportToSentry(err);
}
await this.reportToGA(err);
}
this.error(err);
}
async reportToSentry(error) {
Sentry.init({
dsn: 'https://27ff620b68bf4da39b40f5d491e16fd8@sentry.io/1779992',
});
Sentry.configureScope(scope => {
scope.setUser({ id: this._getUserId() });
if (this._clientInfo) {
scope.setTags({
OS: String(this._clientInfo[1]),
'Node Version': String(this._clientInfo[2]),
'CPU Count': String(this._clientInfo[3]),
RAM: String(this._clientInfo[4]),
'CLI Version': String(this._clientInfo[5]),
});
}
});
Sentry.captureException(error);
await Sentry.flush();
}
reportToGA(error) {
return new Promise((resolve, rejects) => {
const data = configstore_1.default.get('analytics');
const visitor = universal_analytics_1.default('UA-126138188-2', this._getUserId(), {
uid: data,
strictCidFormat: false,
});
visitor.exception(error.message, true).send((err, req) => {
if (!err) {
resolve();
}
rejects();
});
});
}
async finally(err) {
// called after run and catch regardless of whether or not the command errored
}
_getUser() {
return configstore_1.default.get('user');
}
_getUserId() {
return this._getUser() ? this._getUser().id : 'None';
}
_getUserEmal() {
return this._getUser() ? this._getUser().email : 'None';
}
_getOSVersion() {
switch (os.platform()) {
case 'darwin':
return this._buildUserAgentStringForOsx();
case 'win32':
return this._buildUserAgentStringForWindows();
case 'linux':
return this._buildUserAgentStringForLinux();
default:
return os.platform() + ' ' + os.release();
}
}
_getNodeVersion() {
const p = process.version;
const m = p.match(/\d+\.\d+/);
return (m && m[0] && parseFloat(m[0])) || 0;
}
_getCPUCount() {
const cpus = os.cpus();
// Return "(count)x(average speed)".
return cpus.length;
}
_getRAM() {
return Math.floor((os.totalmem() / 1024) * 1024 * 1024);
}
_cliVersion() {
return require('../../package.json').version;
}
_buildUserAgentStringForOsx() {
let v = osVersionMap.darwin[os.release()];
if (!v) {
// Remove 4 to tie Darwin version to OSX version, add other info.
const x = parseFloat(os.release());
if (x > 10) {
v = `10_${(x - 4).toString().replace('.', '_')}`;
}
}
const cpuModel = os.cpus()[0].model.match(/^[a-z]+/i);
const cpu = cpuModel ? cpuModel[0] : os.cpus()[0].model;
return `(Macintosh; ${cpu} Mac OS X ${v || os.release()})`;
}
_buildUserAgentStringForWindows() {
return `(Windows NT ${os.release()})`;
}
_buildUserAgentStringForLinux() {
return `(X11; Linux i686; ${os.release()}; ${os.cpus()[0].model})`;
}
}
exports.default = default_1;
//# sourceMappingURL=base.js.map