Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 0c977e4

Browse files
committed
use function to replace duplicated logic
1 parent 3d761bc commit 0c977e4

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/arduino/arduinoSettings.ts

+4-20
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ export class ArduinoSettings implements IArduinoSettings {
5656

5757
public get defaultExamplePath(): string {
5858
if (os.platform() === "darwin") {
59-
if (this._arduinoPath.match(/Arduino.*\.app/)){
60-
return path.join(this._arduinoPath, "/Contents/Java/examples");
61-
}else{
62-
return path.join(this._arduinoPath, "Arduino.app/Contents/Java/examples");
63-
}
59+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/examples");
6460
} else {
6561
return path.join(this._arduinoPath, "examples");
6662
}
@@ -72,23 +68,15 @@ export class ArduinoSettings implements IArduinoSettings {
7268

7369
public get defaultPackagePath(): string {
7470
if (os.platform() === "darwin") {
75-
if (this._arduinoPath.match(/Arduino.*\.app/)){
76-
return path.join(this._arduinoPath, "/Contents/Java/hardware");
77-
}else{
78-
return path.join(this._arduinoPath, "Arduino.app/Contents/Java/hardware");
79-
}
71+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/hardware");
8072
} else { // linux and win32.
8173
return path.join(this._arduinoPath, "hardware");
8274
}
8375
}
8476

8577
public get defaultLibPath(): string {
8678
if (os.platform() === "darwin") {
87-
if (this._arduinoPath.match(/Arduino.*\.app/)){
88-
return path.join(this._arduinoPath, "/Contents/Java/libraries");
89-
}else{
90-
return path.join(this._arduinoPath, "Arduino.app/Contents/Java/libraries");
91-
}
79+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), "/Contents/Java/libraries");
9280
} else { // linux and win32
9381
return path.join(this._arduinoPath, "libraries");
9482
}
@@ -97,11 +85,7 @@ export class ArduinoSettings implements IArduinoSettings {
9785
public get commandPath(): string {
9886
const platform = os.platform();
9987
if (platform === "darwin") {
100-
if (this._arduinoPath.match(/Arduino.*\.app/)){
101-
return path.join(this._arduinoPath, path.normalize("/Contents/MacOS/Arduino"));
102-
}else{
103-
return path.join(this._arduinoPath, path.normalize("Arduino.app/Contents/MacOS/Arduino"));
104-
}
88+
return path.join(util.resolveMacArduinoAppPath(this._arduinoPath), path.normalize("/Contents/MacOS/Arduino"));
10589
} else if (platform === "linux") {
10690
return path.join(this._arduinoPath, "arduino");
10791
} else if (platform === "win32") {

src/common/sys/darwin.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as childProcess from "child_process";
55
import * as path from "path";
6-
import { directoryExistsSync, fileExistsSync } from "../util";
6+
import { directoryExistsSync, fileExistsSync, resolveMacArduinoAppPath } from "../util";
77

88
export function resolveArduinoPath(): string {
99
let result;
@@ -19,11 +19,7 @@ export function resolveArduinoPath(): string {
1919
}
2020

2121
export function validateArduinoPath(arduinoPath: string): boolean {
22-
if (arduinoPath.match(/Arduino.*\.app/)){
23-
return fileExistsSync(path.join(arduinoPath, "/Contents/MacOS/Arduino"));
24-
}else{
25-
return fileExistsSync(path.join(arduinoPath, "Arduino.app/Contents/MacOS/Arduino"));
26-
}
22+
return fileExistsSync(path.join(resolveMacArduinoAppPath(arduinoPath), "/Contents/MacOS/Arduino"));
2723
}
2824

2925
export function findFile(fileName: string, cwd: string): string {

src/common/util.ts

+13
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,16 @@ export function getRegistryValues(hive: string, key: string, name: string): Prom
388388
export function convertToHex(number, width = 0) {
389389
return padStart(number.toString(16), width, "0");
390390
}
391+
392+
/**
393+
* This will accept any Arduino*.app on Mac OS,
394+
* in case you named Arduino with a version number
395+
* @argument {string} arduinoPath
396+
*/
397+
export function resolveMacArduinoAppPath(arduinoPath: string): string {
398+
if (/Arduino.*\.app/.test(arduinoPath)) {
399+
return arduinoPath;
400+
} else {
401+
return path.join(arduinoPath, "Arduino.app");
402+
}
403+
}

0 commit comments

Comments
 (0)