Skip to content

Commit dfbb989

Browse files
committed
fix: auto add project to unityhub
1 parent 5871781 commit dfbb989

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

src/modules/unity.ts

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { execa } from 'execa';
2-
import path from 'path';
3-
import process from 'process';
1+
import { execa } from "execa";
2+
import path from "path";
3+
import process from "process";
44

55
export type UnityInstallations = Record<string, string>;
66

@@ -10,62 +10,85 @@ const getUnityHubPath = () => {
1010
const programFiles = process.env.ProgramFiles || "C:/Program Files";
1111
return path.resolve(programFiles, "Unity Hub/Unity Hub.exe");
1212
}
13-
default: return "/Applications/Unity Hub.app/Contents/MacOS/Unity Hub"
13+
default:
14+
return "/Applications/Unity Hub.app/Contents/MacOS/Unity Hub";
1415
}
15-
}
16+
};
1617

1718
const parseUnityPath = (basePath: string) => {
1819
switch (process.platform) {
19-
case "win32": return basePath;
20-
default: return path.join(basePath, "/Contents/MacOS/Unity");
20+
case "win32":
21+
return basePath;
22+
default:
23+
return path.join(basePath, "/Contents/MacOS/Unity");
2124
}
22-
}
25+
};
2326

2427
const unityHubPath = getUnityHubPath();
2528

2629
export const getUnityInstallations = async (): Promise<UnityInstallations> => {
27-
const { stdout } = await execa(unityHubPath, "-- --headless editors -i".split(" "), { reject: false });
30+
const { stdout } = await execa(
31+
unityHubPath,
32+
"-- --headless editors -i".split(" "),
33+
{ reject: false }
34+
);
2835

2936
const isSuccess = stdout.includes(", installed at");
30-
if (!isSuccess) throw `Failed to execute command ${unityHubPath}. Consider create UnityHub env var`;
37+
if (!isSuccess)
38+
throw `Failed to execute command ${unityHubPath}. Consider create UnityHub env var`;
3139

3240
const lines = stdout.split(/\r\n|\n/);
3341
const installations: UnityInstallations = {};
3442

35-
lines.forEach(line => {
43+
lines.forEach((line) => {
3644
const [version, unityPath] = line
3745
.split(", installed at")
38-
.map(entry => entry.trim());
46+
.map((entry) => entry.trim());
3947

4048
if (!!version || !!path) installations[version] = parseUnityPath(unityPath);
4149
});
4250

43-
if (Object.keys(installations).length == 0) throw `No unity installations found at ${unityHubPath}.`;
51+
if (Object.keys(installations).length == 0)
52+
throw `No unity installations found at ${unityHubPath}.`;
4453

4554
return installations;
46-
}
55+
};
4756

4857
export const parseUnityVersion = (raw: string) => {
4958
const result = /^[0-9]+\.[0-9]+/gm.exec(raw);
50-
if (!result || result.length === 0) throw `Failed to parse unity version ${raw}`;
59+
if (!result || result.length === 0)
60+
throw `Failed to parse unity version ${raw}`;
5161

5262
return result[0];
53-
}
54-
55-
export const createUnityProject = async (unityPath: string, projectPath: string) => {
56-
await execa(unityPath, ["-quit", "-batchmode", "-createProject", projectPath]);
57-
}
58-
59-
export const openUnityProject = async (unityPath: string, projectPath: string) => {
60-
const subprocess = execa(unityPath, ["-projectPath", projectPath], {
61-
detached: true,
62-
cleanup: false,
63-
stdio: "ignore",
64-
});
63+
};
64+
65+
export const createUnityProject = async (
66+
unityPath: string,
67+
projectPath: string
68+
) => {
69+
await execa(unityPath, [
70+
"-quit",
71+
"-batchmode",
72+
"-createProject",
73+
projectPath,
74+
]);
75+
};
76+
77+
export const openUnityProject = async (
78+
unityPath: string,
79+
projectPath: string
80+
) => {
81+
const subprocess = execa(
82+
unityPath,
83+
["-projectPath", projectPath, "-useHub", "-hubIPC"],
84+
{
85+
detached: true,
86+
cleanup: false,
87+
stdio: "ignore",
88+
}
89+
);
6590

66-
// Waits for a short period of time before deeming the command successful
6791
await new Promise((resolve) => setTimeout(resolve, 2000));
6892

69-
// Disassociate the Unity process from the script process
7093
subprocess.unref();
71-
}
94+
};

0 commit comments

Comments
 (0)