This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathwin32.ts
47 lines (41 loc) · 1.79 KB
/
win32.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as childProcess from "child_process";
import * as path from "path";
import * as WinReg from "winreg";
import { directoryExistsSync, fileExistsSync, getRegistryValues } from "../util";
export async function resolveArduinoPath() {
// eslint-disable-next-line no-prototype-builtins
const isWin64 = process.arch === "x64" || process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432");
let pathString = await getRegistryValues(WinReg.HKLM,
isWin64 ? "\\SOFTWARE\\WOW6432Node\\Arduino" : "\\SOFTWARE\\Arduino",
"Install_Dir");
if (directoryExistsSync(pathString)) {
return pathString;
}
try {
pathString = childProcess.execSync("where arduino", { encoding: "utf8" });
pathString = path.resolve(pathString).trim();
if (fileExistsSync(pathString)) {
pathString = path.dirname(path.resolve(pathString));
}
} catch (error) {
// when "where arduino"" execution fails, the childProcess.execSync will throw error, just ignore it
}
return pathString;
}
export function validateArduinoPath(arduinoPath: string, useArduinoCli = false): boolean {
return fileExistsSync(path.join(arduinoPath, useArduinoCli ? "arduino-cli.exe" : "arduino_debug.exe"));
}
export function findFile(fileName: string, cwd: string): string {
let result;
try {
const pathString = childProcess.execSync(`dir ${fileName} /S /B`, { encoding: "utf8", cwd }).split("\n");
if (pathString && pathString[0] && fileExistsSync(pathString[0].trim())) {
result = path.normalize(pathString[0].trim());
}
} catch (ex) {
// Ignore the errors.
}
return result;
}