-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtelemetry.ts
More file actions
195 lines (179 loc) · 5.57 KB
/
Copy pathtelemetry.ts
File metadata and controls
195 lines (179 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os from 'os';
import { exec } from 'child_process';
import Analytics from 'analytics-node';
import Configstore from 'configstore';
import crypto from 'crypto';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
import gitUrlParse from 'git-url-parse';
import _ from 'lodash';
const sdkPackageJson = require('@stackbit/sdk/package.json');
const cliPackageJson = require('../package.json');
const configDefaults = {
// disable stats from being sent to Stackbit */
telemetryDisabled: false,
// anonymousId
anonymousId: uuidv4(),
identified: false
};
const configOptions = { globalConfigPath: true };
const config = new Configstore('stackbit-cli', configDefaults, configOptions);
let disabled = config.get('telemetryDisabled');
const telemetryOptions = {
host: 'https://api.stackbit.com',
path: '/telemetry/cli',
flushAt: 1
};
const telemetry = new Analytics('temp', telemetryOptions);
// We don't want flushInterval which is passed to setTimeout() to hang the process.
// But we can't use flushInterval: 0 (or other falsy value) in constructor options because it will fallback to 10000.
// So set it directly on constructor
// @ts-ignore
telemetry.flushInterval = 0;
export enum EVENTS {
init = 'Init',
initResult = 'Init Result',
validate = 'Validate',
validateResult = 'Validate Result',
analyzeRepo = 'Analyze Repo',
telemetryDisable = 'Disabled Telemetry',
telemetryEnable = 'Enabled Telemetry'
}
export function identifyIfNeeded() {
if (disabled) {
return;
}
const identified = config.get('identified');
if (identified) {
return;
}
config.set('identified', true);
telemetry.identify({
anonymousId: config.get('anonymousId')
});
}
export function track(event: EVENTS, data?: any) {
if (disabled) {
return;
}
identifyIfNeeded();
computeTelemetryProperties(data).then((properties) => {
telemetry.track({
anonymousId: config.get('anonymousId'),
event: `CLI ${event}`,
properties: properties
});
});
}
export async function trackUncaughtException(data?: any) {
return new Promise((resolve) => {
telemetry.track({
anonymousId: config.get('anonymousId'),
event: 'cli_uncaught_exception',
properties: {
...getDefaultTelemetryProperties(),
...data
}
}, () => {
resolve(null);
});
});
}
export function disable() {
track(EVENTS.telemetryDisable);
config.set('telemetryDisabled', true);
}
export function enable() {
config.set('telemetryDisabled', false);
disabled = false;
track(EVENTS.telemetryEnable);
}
// undefined => wasn't computed yet, no cache
// null => cached null value
// string => cached string value
let folderHash: undefined | null | string;
let repoUrlHash: undefined | null | string;
async function computeTelemetryProperties(data?: any) {
const inputDir = _.get(data, 'inputDir') || _.get(data, 'input_dir');
const folderHash = getFolderHash(inputDir);
const repoUrlHash = await getRepoUrlHash(inputDir);
return _.omitBy(
{
...getDefaultTelemetryProperties(),
folder_hash: folderHash,
repo_hash: repoUrlHash,
..._.omit(data, ['inputDir', 'input_dir'])
},
_.isNil
);
}
function getDefaultTelemetryProperties() {
return {
cli_version: cliPackageJson.version,
sdk_version: sdkPackageJson.version,
node_version: process.version,
os: {
type: os.type(),
platform: os.platform(),
release: os.release(),
version: os.version()
}
};
}
function getFolderHash(inputDir: any): string | null {
if (folderHash !== undefined) {
return folderHash;
}
if (!inputDir || typeof inputDir !== 'string') {
return null;
}
folderHash = md5(path.resolve(inputDir));
return folderHash;
}
async function getRepoUrlHash(inputDir: any): Promise<string | null> {
if (repoUrlHash !== undefined) {
return repoUrlHash;
}
if (!inputDir || typeof inputDir !== 'string') {
return null;
}
return new Promise((resolve: (value: string | null) => void, reject) => {
exec(
'git remote -v',
{
cwd: path.resolve(inputDir)
},
(error, stdout) => {
// stdout:
// origin git@github.com:stackbit/stackbit-cli.git (fetch)
// origin git@github.com:stackbit/stackbit-cli.git (push)
if (stdout) {
// get the remote from the first line
const match = stdout.trim().match(/^(\w+)\s+([^\s]+)/);
if (!match) {
resolve(null);
return;
}
const remoteUrl = match[2]!;
const urlObject = gitUrlParse(remoteUrl);
if (urlObject.source && urlObject.full_name) {
resolve(urlObject.source + '/' + urlObject.full_name);
return;
}
}
resolve(null);
}
);
})
.then((repoUrl) => {
repoUrlHash = repoUrl ? md5(repoUrl) : null;
return repoUrlHash;
})
.catch(() => {
repoUrlHash = null;
return repoUrlHash;
});
}
function md5(data: string) {
return crypto.createHash('md5').update(data).digest('hex');
}