telemetry - avoid sync fs APIs for building CLI telemetry message

This commit is contained in:
Benjamin Pasero 2021-06-07 15:32:26 +02:00
parent 3d377570fa
commit 666ce447cb
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
3 changed files with 30 additions and 20 deletions

View file

@ -220,7 +220,7 @@ class CliMain extends Disposable {
// Telemetry
else if (this.argv['telemetry']) {
console.log(buildTelemetryMessage(environmentService.appRoot, environmentService.extensionsPath));
console.log(await buildTelemetryMessage(environmentService.appRoot, environmentService.extensionsPath));
}
}

View file

@ -3,43 +3,52 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { readdirSync } from 'vs/base/node/pfs';
import { statSync, readFileSync } from 'fs';
import { Promises } from 'vs/base/node/pfs';
import { join } from 'vs/base/common/path';
export function buildTelemetryMessage(appRoot: string, extensionsPath?: string): string {
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) {
// Gets all the directories inside the extension directory
const dirs = readdirSync(extensionsPath).filter(files => {
// This handles case where broken symbolic links can cause statSync to throw and error
const dirs: string[] = [];
const files = await Promises.readdir(extensionsPath);
for (const file of files) {
try {
return statSync(join(extensionsPath, files)).isDirectory();
const fileStat = await Promises.stat(join(extensionsPath, file));
if (fileStat.isDirectory()) {
dirs.push(file);
}
} catch {
return false;
// This handles case where broken symbolic links can cause statSync to throw and error
}
});
}
const telemetryJsonFolders: string[] = [];
dirs.forEach((dir) => {
const files = readdirSync(join(extensionsPath, dir)).filter(file => file === 'telemetry.json');
// We know it contains a telemetry.json file so we add it to the list of folders which have one
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);
telemetryJsonFolders.push(dir); // // We know it contains a telemetry.json file so we add it to the list of folders which have one
}
});
telemetryJsonFolders.forEach((folder) => {
const contents = readFileSync(join(extensionsPath, folder, 'telemetry.json')).toString();
}
for (const folder of telemetryJsonFolders) {
const contents = (await Promises.readFile(join(extensionsPath, folder, 'telemetry.json'))).toString();
mergeTelemetry(contents, folder);
});
}
}
let contents = readFileSync(join(appRoot, 'telemetry-core.json')).toString();
let contents = (await Promises.readFile(join(appRoot, 'telemetry-core.json'))).toString();
mergeTelemetry(contents, 'vscode-core');
contents = readFileSync(join(appRoot, 'telemetry-extensions.json')).toString();
contents = (await Promises.readFile(join(appRoot, 'telemetry-extensions.json'))).toString();
mergeTelemetry(contents, 'vscode-extensions');
return JSON.stringify(mergedTelemetry, null, 4);
}

View file

@ -856,6 +856,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
if (isFileToOpen(openable)) {
options = { ...options, forceOpenWorkspaceAsFile: true };
}
return this.doResolveFilePath(uri.fsPath, options);
}