Skip to content

Commit 661b653

Browse files
authored
feat: custom android-studio path to support nixos using new env.NATIVESCRIPT_ANDROID_STUDIO_PATH (#5816)
1 parent 86d9f29 commit 661b653

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

lib/key-commands/index.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ export class ShiftA implements IKeyCommand {
5050
private $projectData: IProjectData
5151
) {}
5252

53+
getAndroidStudioPath(): string | null {
54+
const os = currentPlatform();
55+
56+
if (os === "darwin") {
57+
const possibleStudioPaths = [
58+
"/Applications/Android Studio.app",
59+
`${process.env.HOME}/Applications/Android Studio.app`,
60+
];
61+
62+
return possibleStudioPaths.find((p) => fs.existsSync(p)) || null;
63+
} else if (os === "win32") {
64+
const studioPath = path.join(
65+
"C:",
66+
"Program Files",
67+
"Android",
68+
"Android Studio",
69+
"bin",
70+
"studio64.exe"
71+
);
72+
return fs.existsSync(studioPath) ? studioPath : null;
73+
} else if (os === "linux") {
74+
const studioPath = "/usr/local/android-studio/bin/studio.sh";
75+
return fs.existsSync(studioPath) ? studioPath : null;
76+
}
77+
78+
return null;
79+
}
80+
5381
async execute(): Promise<void> {
5482
this.$liveSyncCommandHelper.validatePlatform(this.platform);
5583
this.$projectData.initializeProjectData();
@@ -65,53 +93,32 @@ export class ShiftA implements IKeyCommand {
6593
}
6694
}
6795

68-
const os = currentPlatform();
96+
let studioPath = null;
6997

70-
if (os === "darwin") {
71-
const possibleStudioPaths = [
72-
"/Applications/Android Studio.app",
73-
`${process.env.HOME}/Applications/Android Studio.app`,
74-
];
98+
studioPath = process.env.NATIVESCRIPT_ANDROID_STUDIO_PATH;
7599

76-
const studioPath = possibleStudioPaths.find((p) => {
77-
this.$logger.trace(`Checking for Android Studio at ${p}`);
78-
return fs.existsSync(p);
79-
});
100+
if (!studioPath) {
101+
studioPath = this.getAndroidStudioPath();
80102

81103
if (!studioPath) {
82104
this.$logger.error(
83-
"Android Studio is not installed, or not in a standard location."
105+
"Android Studio is not installed, or is not in a standard location. Use NATIVESCRIPT_ANDROID_STUDIO_PATH."
84106
);
85107
return;
86108
}
109+
}
110+
111+
const os = currentPlatform();
112+
if (os === "darwin") {
87113
this.$childProcess.exec(`open -a "${studioPath}" ${androidDir}`);
88114
} else if (os === "win32") {
89-
const studioPath = path.join(
90-
"C:",
91-
"Program Files",
92-
"Android",
93-
"Android Studio",
94-
"bin",
95-
"studio64.exe"
96-
);
97-
if (!fs.existsSync(studioPath)) {
98-
this.$logger.error("Android Studio is not installed");
99-
return;
100-
}
101-
102115
const child = this.$childProcess.spawn(studioPath, [androidDir], {
103116
detached: true,
104117
stdio: "ignore",
105118
});
106119
child.unref();
107120
} else if (os === "linux") {
108-
if (!fs.existsSync(`/usr/local/android-studio/bin/studio.sh`)) {
109-
this.$logger.error("Android Studio is not installed");
110-
return;
111-
}
112-
this.$childProcess.exec(
113-
`/usr/local/android-studio/bin/studio.sh ${androidDir}`
114-
);
121+
this.$childProcess.exec(`${studioPath} ${androidDir}`);
115122
}
116123
}
117124
}

0 commit comments

Comments
 (0)