Skip to content

Commit

Permalink
reduce boilerplate with optionArray helper
Browse files Browse the repository at this point in the history
  • Loading branch information
David Herman committed Nov 23, 2023
1 parent 9ebe1cc commit b3029a1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/cli/src/commands/add-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Command, CommandDetail, CommandSection } from '../command.js';
import { expandTargetFamily, getCurrentTarget, isNodeTarget, isRustTarget, isTargetFamilyKey, NodeTarget, RustTarget, TargetPair } from '../target.js';
import { SourceManifest } from '../manifest.js';

function optionArray<T>(option: T | undefined | null): T[] {
return option == null ? [] : [option];
}

const OPTIONS = [
{ name: 'bundle', alias: 'b', type: String, defaultValue: null },
{ name: 'platform', alias: 'p', type: String, defaultValue: null },
Expand Down Expand Up @@ -105,16 +109,13 @@ export default class AddTarget implements Command {
async addTarget(sourceManifest: SourceManifest): Promise<TargetPair[]> {
if (!this._target) {
this.log('adding default system target');
const pair = await sourceManifest.addRustTarget(await getCurrentTarget(msg => this.log(msg)));
return pair ? [pair] : [];
return optionArray(await sourceManifest.addRustTarget(await getCurrentTarget(msg => this.log(msg))));
} else if (isRustTarget(this._target)) {
this.log(`adding Rust target ${this._target}`);
const pair = await sourceManifest.addRustTarget(this._target);
return pair ? [pair] : [];
return optionArray(await sourceManifest.addRustTarget(this._target));
} else if (isNodeTarget(this._target)) {
this.log(`adding Node target ${this._target}`);
const pair = await sourceManifest.addNodeTarget(this._target);
return pair ? [pair] : [];
return optionArray(await sourceManifest.addNodeTarget(this._target));
} else if (isTargetFamilyKey(this._target)) {
return sourceManifest.addTargets(expandTargetFamily(this._target));
} else {
Expand Down

0 comments on commit b3029a1

Please sign in to comment.