-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hotfix(local-debug): add error message for npm install tasks telemetry
- Loading branch information
Showing
3 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters