Skip to content

Commit 9a75bfa

Browse files
explicit process exit codes for cli
updated some logging and private function names
1 parent a8557ad commit 9a75bfa

4 files changed

Lines changed: 57 additions & 27 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ npm install -g @rage-against-the-pixel/unity-cli
2222

2323
## Usage
2424

25+
In general, the command structure is:
26+
2527
```bash
26-
unity-cli [command] [options]
28+
unity-cli [command] [options] <args...>
2729
```
2830

31+
With options always using double dashes (`--option`) and arguments passed directly to Unity or Unity Hub commands as they normally would with single dashes (`-arg`). Each option typically has a short alias using a single dash (`-o`), except for commands where we pass through arguments, as those get confused by the command parser.
32+
2933
### Common Commands
3034

3135
#### Auth

src/cli.ts

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ program.command('license-version')
3232
.action(async () => {
3333
const client = new LicensingClient();
3434
await client.Version();
35+
process.exit(0);
3536
});
3637

3738
program.command('activate-license')
@@ -53,13 +54,15 @@ program.command('activate-license')
5354
const licenseStr: string = options.license?.toString()?.trim();
5455

5556
if (!licenseStr || licenseStr.length === 0) {
56-
throw new Error('License type is required. Use -l or --license to specify it.');
57+
Logger.instance.error('License type is required. Use -l or --license to specify it.');
58+
process.exit(1);
5759
}
5860

5961
const licenseType: LicenseType = options.license.toLowerCase() as LicenseType;
6062

6163
if (![LicenseType.personal, LicenseType.professional, LicenseType.floating].includes(licenseType)) {
62-
throw new Error(`Invalid license type: ${licenseType}`);
64+
Logger.instance.error(`Invalid license type: ${licenseType}`);
65+
process.exit(1);
6366
}
6467

6568
if (licenseType !== LicenseType.floating) {
@@ -83,6 +86,7 @@ program.command('activate-license')
8386
username: options.email,
8487
password: options.password
8588
});
89+
process.exit(0);
8690
});
8791

8892
program.command('return-license')
@@ -100,16 +104,19 @@ program.command('return-license')
100104
const licenseStr: string = options.license?.toString()?.trim();
101105

102106
if (!licenseStr || licenseStr.length === 0) {
103-
throw new Error('License type is required. Use -l or --license to specify it.');
107+
Logger.instance.error('License type is required. Use -l or --license to specify it.');
108+
process.exit(1);
104109
}
105110

106111
const licenseType: LicenseType = licenseStr.toLowerCase() as LicenseType;
107112

108113
if (![LicenseType.personal, LicenseType.professional, LicenseType.floating].includes(licenseType)) {
109-
throw new Error(`Invalid license type: ${licenseType}`);
114+
Logger.instance.error(`Invalid license type: ${licenseType}`);
115+
process.exit(1);
110116
}
111117

112118
await client.Deactivate(licenseType);
119+
process.exit(0);
113120
});
114121

115122
program.commandsGroup('Unity Hub:');
@@ -123,6 +130,8 @@ program.command('hub-version')
123130
process.stdout.write(`${version}\n`);
124131
} catch (error) {
125132
process.stdout.write(`${error}\n`);
133+
} finally {
134+
process.exit(0);
126135
}
127136
});
128137

@@ -148,6 +157,8 @@ program.command('hub-install')
148157
} else {
149158
process.stdout.write(`${hubPath}\n`);
150159
}
160+
161+
process.exit(0);
151162
});
152163

153164
program.command('hub-path')
@@ -163,13 +174,15 @@ program.command('hub-path')
163174
} else {
164175
process.stdout.write(`${hub.executable}\n`);
165176
}
177+
178+
process.exit(0);
166179
});
167180

168181
program.command('hub')
169182
.description('Run commands directly to the Unity Hub. (You need not to pass --headless or -- to this command).')
183+
.allowUnknownOption(true)
170184
.argument('<args...>', 'Arguments to pass to the Unity Hub executable.')
171185
.option('--verbose', 'Enable verbose logging.')
172-
.allowUnknownOption(true)
173186
.action(async (args: string[], options) => {
174187
if (options.verbose) {
175188
Logger.instance.logLevel = LogLevel.DEBUG;
@@ -179,6 +192,7 @@ program.command('hub')
179192

180193
const unityHub = new UnityHub();
181194
await unityHub.Exec(args, { silent: false, showCommand: Logger.instance.logLevel === LogLevel.DEBUG });
195+
process.exit(0);
182196
});
183197

184198
program.command('setup-unity')
@@ -206,7 +220,8 @@ program.command('setup-unity')
206220
}
207221

208222
if (!options.unityVersion && !unityProject) {
209-
throw new Error('You must specify a Unity version or project path with -u, --unity-version, -p, --unity-project.');
223+
Logger.instance.error('You must specify a Unity version or project path with -u, --unity-version, -p, --unity-project.');
224+
process.exit(1);
210225
}
211226

212227
const unityVersion = unityProject?.version ?? new UnityVersion(options.unityVersion, options.changeset);
@@ -266,6 +281,8 @@ program.command('setup-unity')
266281
}
267282
}
268283
}
284+
285+
process.exit(0);
269286
});
270287

271288
program.command('uninstall-unity')
@@ -289,6 +306,7 @@ program.command('uninstall-unity')
289306
const unityVersion = new UnityVersion(unityVersionStr, options.changeset, options.arch);
290307
const unityHub = new UnityHub();
291308
const installedEditors = await unityHub.ListInstalledEditors();
309+
292310
if (unityVersion.isLegacy()) {
293311
const installPath = await unityHub.GetInstallPath();
294312
unityEditor = new UnityEditor(path.join(installPath, `Unity ${unityVersion.toString()}`, 'Unity.exe'));
@@ -299,7 +317,8 @@ program.command('uninstall-unity')
299317
const editorPath = options.unityEditor?.toString()?.trim() || process.env.UNITY_EDITOR_PATH || undefined;
300318

301319
if (!editorPath || editorPath.length === 0) {
302-
throw new Error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
320+
Logger.instance.error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
321+
process.exit(1);
303322
}
304323

305324
try {
@@ -336,7 +355,8 @@ program.command('open-project')
336355
const unityProject = await UnityProject.GetProject(projectPath);
337356

338357
if (!unityProject) {
339-
throw new Error(`The specified path is not a valid Unity project: ${projectPath}`);
358+
Logger.instance.error(`The specified path is not a valid Unity project: ${projectPath}`);
359+
process.exit(1);
340360
}
341361

342362
const unityVersion = unityProject?.version ?? new UnityVersion(options.unityVersion, options.changeset);
@@ -380,7 +400,8 @@ program.command('run')
380400
const unityProject = await UnityProject.GetProject(projectPath);
381401

382402
if (!unityProject) {
383-
throw new Error(`The specified path is not a valid Unity project: ${projectPath}`);
403+
Logger.instance.error(`The specified path is not a valid Unity project: ${projectPath}`);
404+
process.exit(1);
384405
}
385406

386407
if (!unityEditor) {
@@ -389,7 +410,8 @@ program.command('run')
389410
}
390411

391412
if (!unityEditor) {
392-
throw new Error('The Unity Editor path was not specified. Use --unity-editor to specify it or set the UNITY_EDITOR_PATH environment variable.');
413+
Logger.instance.error('The Unity Editor path was not specified. Use --unity-editor to specify it or set the UNITY_EDITOR_PATH environment variable.');
414+
process.exit(1);
393415
}
394416

395417
if (!args.includes('-logFile')) {
@@ -400,6 +422,7 @@ program.command('run')
400422
await unityEditor.Run({
401423
args: [...args]
402424
});
425+
process.exit(0);
403426
});
404427

405428
program.command('list-project-templates')
@@ -418,7 +441,8 @@ program.command('list-project-templates')
418441
const unityVersionStr = options.unityVersion?.toString()?.trim();
419442

420443
if (!unityVersionStr && !options.unityEditor) {
421-
throw new Error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
444+
Logger.instance.error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
445+
process.exit(1);
422446
}
423447

424448
let unityEditor: UnityEditor;
@@ -442,14 +466,13 @@ program.command('list-project-templates')
442466
if (options.json) {
443467
process.stdout.write(`\n${JSON.stringify({ templates })}\n`);
444468
} else {
445-
process.stdout.write(`Available project templates:\n`);
446-
for (const template of templates) {
447-
process.stdout.write(` - ${path.basename(template)}\n`);
448-
}
469+
process.stdout.write(`Available project templates:\n${templates.map(t => ` - ${path.basename(t)}`).join('\n')}\n`);
449470
}
450471
} else {
451472
process.stdout.write('No project templates found for this Unity Editor.\n');
452473
}
474+
475+
process.exit(0);
453476
});
454477

455478
program.command('create-project')
@@ -471,7 +494,8 @@ program.command('create-project')
471494
const unityVersionStr = options.unityVersion?.toString()?.trim();
472495

473496
if (!unityVersionStr && !options.unityEditor) {
474-
throw new Error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
497+
Logger.instance.error('You must specify a Unity version or editor path with -u, --unity-version, -e, --unity-editor.');
498+
process.exit(1);
475499
}
476500

477501
let unityEditor: UnityEditor;
@@ -483,7 +507,8 @@ program.command('create-project')
483507
const editorPath = options.unityEditor?.toString()?.trim() || process.env.UNITY_EDITOR_PATH;
484508

485509
if (!editorPath || editorPath.length === 0) {
486-
throw new Error('The Unity Editor path was not specified. Use -e or --unity-editor to specify it, or set the UNITY_EDITOR_PATH environment variable.');
510+
Logger.instance.error('The Unity Editor path was not specified. Use -e or --unity-editor to specify it, or set the UNITY_EDITOR_PATH environment variable.');
511+
process.exit(1);
487512
}
488513

489514
unityEditor = new UnityEditor(editorPath);
@@ -521,6 +546,8 @@ program.command('create-project')
521546
} else {
522547
process.stdout.write(`Unity project created at: ${projectPath}\n`);
523548
}
549+
550+
process.exit(0);
524551
});
525552

526553
program.parse(process.argv);

src/unity-editor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class UnityEditor {
131131
templateDir = path.join(editorRoot, 'Data', 'Resources', 'PackageManager', 'ProjectTemplates');
132132
}
133133

134-
this.logger.ci(`Looking for templates in: ${templateDir}`);
134+
this.logger.debug(`Looking for templates in: ${templateDir}`);
135135

136136
// Check if the template directory exists
137137
if (!fs.existsSync(templateDir) ||
@@ -144,8 +144,7 @@ export class UnityEditor {
144144
.filter(f => f.endsWith('.tgz'))
145145
.map(f => path.join(templateDir, f));
146146
templates.push(...packages);
147-
this.logger.ci(`Found ${templates.length} templates:`);
148-
templates.forEach(t => this.logger.ci(` - ${t}`));
147+
this.logger.debug(`Found ${templates.length} templates:\n${templates.map(t => ` - ${t}`).join('\n')}`);
149148
return templates;
150149
}
151150

src/unity-version.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ export class UnityVersion {
7878
return new UnityVersion(exactMatch.version, this.changeset ?? null, this.architecture);
7979
}
8080

81-
if (UnityVersion.needsFallbackSearch(this.version)) {
82-
const candidates = UnityVersion.resolveFallbackCandidates(this.version, releaseInfos);
81+
if (UnityVersion.needsGlobSearch(this.version)) {
82+
const candidates = UnityVersion.resolveVersionCandidates(this.version, releaseInfos);
8383

84-
this.logger.debug(`Searching for fallback match for ${this.version}:`);
84+
this.logger.debug(`Searching for match for ${this.version}:`);
8585
candidates.forEach(release => {
8686
this.logger.debug(` > ${release.version}`);
8787
});
8888

8989
const latest = candidates[0];
9090

9191
if (latest) {
92-
this.logger.debug(`Found fallback Unity ${latest.version}`);
92+
this.logger.debug(`Found Unity ${latest.version}`);
9393
return new UnityVersion(latest.version, null, this.architecture);
9494
}
9595
}
@@ -215,11 +215,11 @@ export class UnityVersion {
215215
};
216216
}
217217

218-
private static needsFallbackSearch(version: string): boolean {
218+
private static needsGlobSearch(version: string): boolean {
219219
return /\.x($|[^\w])/.test(version) || /\.\*($|[^\w])/.test(version) || !UnityVersion.UNITY_RELEASE_PATTERN.test(version);
220220
}
221221

222-
private static resolveFallbackCandidates(
222+
private static resolveVersionCandidates(
223223
version: string,
224224
releases: UnityReleaseInfo[]
225225
): UnityReleaseInfo[] {

0 commit comments

Comments
 (0)