1+ import { execa } from 'execa' ;
2+ import path from 'path' ;
3+ import process from 'process' ;
4+
5+ export type UnityInstallations = Record < string , string > ;
6+
7+ const programFiles = process . env . ProgramFiles || path . resolve ( "C:/Program Files/" ) ;
8+ const unityHubPath = process . env . UnityHub || path . join ( programFiles , "Unity Hub" , "Unity Hub.exe" )
9+
10+ export const getUnityInstallations = async ( ) : Promise < UnityInstallations > => {
11+ const { stdout } = await execa ( unityHubPath , "-- --headless editors -i" . split ( " " ) , { reject : false } ) ;
12+
13+ const isSuccess = stdout . includes ( ", installed at" ) ;
14+ if ( ! isSuccess ) throw `Failed to execute command ${ unityHubPath } . Consider create UnityHub env var` ;
15+
16+ const lines = stdout . split ( / \r \n | \n / ) ;
17+ const installations : UnityInstallations = { } ;
18+
19+ lines . forEach ( line => {
20+ const [ version , path ] = line
21+ . split ( ", installed at" )
22+ . map ( entry => entry . trim ( ) ) ;
23+
24+ if ( ! ! version || ! ! path ) installations [ version ] = path ;
25+ } ) ;
26+
27+ if ( Object . keys ( installations ) . length == 0 ) throw `No unity installations found at ${ unityHubPath } .` ;
28+
29+ return installations ;
30+ }
31+
32+ export const createUnityProject = async ( unityPath : string , projectPath : string ) => {
33+ await execa ( unityPath , [ "-quit" , "-batchmode" , "-createProject" , projectPath ] ) ;
34+ }
35+
36+ export const openUnityProject = async ( unityPath : string , projectPath : string ) => {
37+ await execa ( unityPath , [ "-projectPath" , projectPath ] , { stdio : "ignore" } ) ;
38+ }
0 commit comments