/
telemetry.ts
54 lines (44 loc) · 2.05 KB
/
telemetry.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { join } from 'vs/base/common/path';
import { Promises } from 'vs/base/node/pfs';
export async function buildTelemetryMessage(appRoot: string, extensionsPath?: string): Promise<string> {
const mergedTelemetry = Object.create(null);
// Simple function to merge the telemetry into one json object
const mergeTelemetry = (contents: string, dirName: string) => {
const telemetryData = JSON.parse(contents);
mergedTelemetry[dirName] = telemetryData;
};
if (extensionsPath) {
const dirs: string[] = [];
const files = await Promises.readdir(extensionsPath);
for (const file of files) {
try {
const fileStat = await Promises.stat(join(extensionsPath, file));
if (fileStat.isDirectory()) {
dirs.push(file);
}
} catch {
// This handles case where broken symbolic links can cause statSync to throw and error
}
}
const telemetryJsonFolders: string[] = [];
for (const dir of dirs) {
const files = (await Promises.readdir(join(extensionsPath, dir))).filter(file => file === 'telemetry.json');
if (files.length === 1) {
telemetryJsonFolders.push(dir); // // We know it contains a telemetry.json file so we add it to the list of folders which have one
}
}
for (const folder of telemetryJsonFolders) {
const contents = (await Promises.readFile(join(extensionsPath, folder, 'telemetry.json'))).toString();
mergeTelemetry(contents, folder);
}
}
let contents = (await Promises.readFile(join(appRoot, 'telemetry-core.json'))).toString();
mergeTelemetry(contents, 'vscode-core');
contents = (await Promises.readFile(join(appRoot, 'telemetry-extensions.json'))).toString();
mergeTelemetry(contents, 'vscode-extensions');
return JSON.stringify(mergedTelemetry, null, 4);
}