Skip to content

Commit

Permalink
hotfix(local-debug): add error message for npm install tasks telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
kuojianlu committed Jun 24, 2021
1 parent ce1ed3f commit c8d119e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
54 changes: 54 additions & 0 deletions packages/vscode-extension/src/debug/npmLogHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { spawn } from "child_process";
import * as fs from "fs-extra";
import * as path from "path";

function getNpmCachePath(): Promise<string> {
const command = spawn("npm config get cache", {
shell: true,
});
return new Promise((resolve, reject) => {
command.stdout.on("data", (data) => {
resolve(`${data.toString().trim()}`);
})
});
}

async function getLatestNpmLogFile(npmLogPath: string): Promise<string | undefined> {
const files = await fs.readdir(npmLogPath);
if (files.length === 0) {
return undefined;
}
const latestNpmLogFile = files.reduce((previous, current, index, array) => {
return previous > current ? previous : current;
});
return path.join(npmLogPath, latestNpmLogFile);
}

export async function getNpmInstallErrorLog(cwd: string): Promise<Array<string> | undefined> {
const npmCachePath = await getNpmCachePath();
if (!fs.pathExists(npmCachePath)) {
return undefined;
}
const latestNpmLogFile = await getLatestNpmLogFile(path.join(npmCachePath, "_logs"));
if (latestNpmLogFile === undefined) {
return undefined;
}
const log = (await fs.readFile(latestNpmLogFile)).toString();
const cwdPattern = /\d+ verbose cwd (.*)/;
const cwdResult = log.match(cwdPattern);
// TODO: handle case sensitive path
if (!cwdResult || cwdResult[1].trim().toLowerCase() !== cwd.toLowerCase()) {
return undefined;
}
const errorPattern = /\d+ error .*/g;
const errorResults = log.match(errorPattern);
if (!errorResults) {
return undefined;
}
return errorResults.map((value, index, array) => {
return value.trim();
});
}
31 changes: 26 additions & 5 deletions packages/vscode-extension/src/debug/teamsfxTaskHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { ExtTelemetry } from "../telemetry/extTelemetry";
import { TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents";
import { getTeamsAppId } from "../utils/commonUtils";
import { isValidProject } from "@microsoft/teamsfx-core";
import { getNpmInstallErrorLog } from "./npmLogHandler";
import * as path from "path";

interface IRunningTeamsfxTask {
source: string;
Expand Down Expand Up @@ -90,18 +92,37 @@ function onDidStartTaskProcessHandler(event: vscode.TaskProcessStartEvent): void
}
}

function onDidEndTaskProcessHandler(event: vscode.TaskProcessEndEvent): void {
async function onDidEndTaskProcessHandler(event: vscode.TaskProcessEndEvent): Promise<void> {
const task = event.execution.task;
const activeTerminal = vscode.window.activeTerminal;

if (task.scope !== undefined && isTeamsfxTask(task)) {
allRunningTeamsfxTasks.delete({ source: task.source, name: task.name, scope: task.scope });
} else if (isNpmInstallTask(task)) {
try {
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.DebugNpmInstall, {
[TelemetryProperty.DebugNpmInstallName]: task.name,
[TelemetryProperty.DebugNpmInstallExitCode]: event.exitCode + "",
});
let npmInstallErrorMessage: string | undefined;
if (event.exitCode) {
const cwdOption = (task.execution as vscode.ShellExecution).options?.cwd;
if (cwdOption !== undefined) {
const cwd = path.join(ext.workspaceUri.fsPath, cwdOption?.replace("${workspaceFolder}/", ""));
const npmInstallErrorLog = await getNpmInstallErrorLog(cwd);
if (npmInstallErrorLog !== undefined) {
npmInstallErrorMessage = npmInstallErrorLog.join("\n");
}
}
}
if (npmInstallErrorMessage === undefined) {
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.DebugNpmInstall, {
[TelemetryProperty.DebugNpmInstallName]: task.name,
[TelemetryProperty.DebugNpmInstallExitCode]: event.exitCode + "",
});
} else {
ExtTelemetry.sendTelemetryEvent(TelemetryEvent.DebugNpmInstall, {
[TelemetryProperty.DebugNpmInstallName]: task.name,
[TelemetryProperty.DebugNpmInstallExitCode]: event.exitCode + "",
[TelemetryProperty.DebugNpmInstallErrorMessage]: npmInstallErrorMessage,
});
}
} catch {
// ignore telemetry error
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export enum TelemetryProperty {
DebugAppId = "debug-appid",
DebugNpmInstallName = "debug-npm-install-name",
DebugNpmInstallExitCode = "debug-npm-install-exit-code",
DebugNpmInstallErrorMessage = "debug-npm-install-error-message",
Internal = "internal",
InternalAlias = "internal-alias",
OSArch = "os-arch",
Expand Down

0 comments on commit c8d119e

Please sign in to comment.