Skip to content

Commit 9d8c691

Browse files
Fatme HavaluovaFatme Havaluova
authored andcommitted
* Add errors.ts
* Show callstack only if --debug option is specified
1 parent d61bc71 commit 9d8c691

9 files changed

+55
-28
lines changed

lib/command-executor.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
33

4+
import Future = require("fibers/future");
45
import util = require("util");
6+
require("colors");
7+
8+
import errors = require("./errors");
59
import options = require("./options");
610

711
export class CommandExecutor implements ICommandExecutor {
812

913
public execute(): IFuture<void> {
10-
var commandName = this.getCommandName();
11-
var commandArguments = this.getCommandArguments();
14+
return (() => {
15+
var commandName = this.getCommandName();
16+
var commandArguments = this.getCommandArguments();
1217

13-
return this.executeCore(commandName, commandArguments);
18+
this.executeCore(commandName, commandArguments).wait();
19+
}).future<void>()();
1420
}
1521

1622
private executeCore(commandName: string, commandArguments: string[]): IFuture<void> {
17-
var command = new (require("./commands/" + commandName).Command)();
18-
if(!command) {
19-
throw new Error(util.format("Unable to resolve commandName %s", commandName));
20-
}
21-
22-
return command.execute(commandArguments);
23+
return (() => {
24+
var command: ICommand = new (require("./commands/" + commandName).Command)();
25+
if(!command) {
26+
errors.fail("Unable to resolve commandName %s", commandName);
27+
}
28+
29+
try {
30+
command.execute(commandArguments).wait();
31+
} catch(e) {
32+
if(options.debug) {
33+
throw e;
34+
} else {
35+
console.log( "\x1B[31;1m" + e.message + "\x1B[0m");
36+
}
37+
}
38+
}).future<void>()();
2339
}
2440

2541
private getCommandArguments(): string[] {

lib/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ interface IDictionary<T> {
2424
}
2525

2626
interface ISimulator {
27-
validateDeviceIdentifier(): void;
27+
validDeviceIdentifiers: string[];
2828
setSimulatedDevice(config: any): void;
2929
}

lib/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
///<reference path="./.d.ts"/>
2+
"use strict";
3+
4+
import util = require("util");
5+
6+
export function fail(errorMessage: string, ...args: string[]) {
7+
args.unshift(errorMessage);
8+
throw new Error(util.format.apply(null, args));
9+
}

lib/ios-sim.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import commandExecutorLibPath = require("./command-executor");
1010
var fiber = Fiber(() => {
1111
var commandExecutor: ICommandExecutor = new commandExecutorLibPath.CommandExecutor();
1212
commandExecutor.execute().wait();
13+
Future.assertNoFutureLeftBehind();
1314
});
1415

1516
fiber.run();

lib/iphone-simulator-xcode-5.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
33

4+
import errors = require("./errors");
45
import options = require("./options");
56
import utils = require("./utils");
67
import util = require("util");
@@ -21,11 +22,8 @@ export class XCode5Simulator implements ISimulator {
2122
"iPad-Retina-64-bit": "iPad Retina (64-bit)"
2223
}
2324

24-
public validateDeviceIdentifier(): void {
25-
var identifiers: string[] = _.keys(XCode5Simulator.allowedDeviceIdentifiers);
26-
if(!_.contains(identifiers, options.device)) {
27-
throw new Error(util.format("Invalid device identifier %s. Valid device identifiers are %s.", options.device, utils.stringify(identifiers)));
28-
}
25+
public get validDeviceIdentifiers(): string[] {
26+
return _.keys(XCode5Simulator.allowedDeviceIdentifiers);
2927
}
3028

3129
public setSimulatedDevice(config:any): void {

lib/iphone-simulator-xcode-6.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
3+
import errors = require("./errors");
34
import options = require("./options");
45
import utils = require("./utils");
56
import util = require("util");
@@ -29,10 +30,8 @@ export class XCode6Simulator implements ISimulator {
2930
this.availableDevices = Object.create(null);
3031
}
3132

32-
public validateDeviceIdentifier(): void {
33-
if(!_.contains(XCode6Simulator.allowedDeviceIdentifiers, options.device)) {
34-
throw new Error(util.format("Invalid device identifier %s. Valid device identifiers are %s.", options.device, utils.stringify(XCode6Simulator.allowedDeviceIdentifiers)));
35-
}
33+
public get validDeviceIdentifiers(): string[] {
34+
return XCode6Simulator.allowedDeviceIdentifiers;
3635
}
3736

3837
public setSimulatedDevice(config: any): void {
@@ -76,6 +75,6 @@ export class XCode6Simulator implements ISimulator {
7675
}
7776
}
7877

79-
throw new Error(util.format("Unable to find device with identifier ", deviceIdentifier));
78+
errors.fail("Unable to find device with identifier ", deviceIdentifier);
8079
}
8180
}

lib/iphone-simulator.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Future = require("fibers/future");
77
import path = require("path");
88
import util = require("util");
99

10+
import errors = require("./errors");
1011
import options = require("./options");
1112
import utils = require("./utils");
1213
import xcode6SimulatorLib = require("./iphone-simulator-xcode-6");
@@ -28,7 +29,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
2829
public run(appPath: string): IFuture<void> {
2930
return (() => {
3031
if(!fs.existsSync(appPath)) {
31-
throw new Error(util.format("Path does not exist ", appPath));
32+
errors.fail("Path does not exist ", appPath);
3233
}
3334

3435
$.importFramework(iPhoneSimulator.FOUNDATION_FRAMEWORK_NAME);
@@ -38,7 +39,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
3839

3940
var developerDirectoryPath = this.findDeveloperDirectory().wait();
4041
if(!developerDirectoryPath) {
41-
throw new Error("Unable to find developer directory");
42+
errors.fail("Unable to find developer directory");
4243
}
4344

4445
this.loadFrameworks(developerDirectoryPath);
@@ -78,7 +79,10 @@ export class iPhoneSimulator implements IiPhoneSimulator {
7879
}
7980

8081
if(options.device) {
81-
simulator.validateDeviceIdentifier();
82+
var validDeviceIdentifiers = simulator.validDeviceIdentifiers;
83+
if(!_.contains(validDeviceIdentifiers, options.device)) {
84+
errors.fail("Invalid device identifier %s. Valid device identifiers are %s.", options.device, utils.stringify(validDeviceIdentifiers));
85+
}
8286
}
8387
simulator.setSimulatedDevice(config);
8488

@@ -93,7 +97,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
9397
session("setDelegate", delegate);
9498

9599
if(!session("requestStartWithConfig", config, "timeout", timeout, "error", sessionError)) {
96-
throw new Error(util.format("Could not start simulator session ", sessionError));
100+
errors.fail("Could not start simulator session ", sessionError);
97101
}
98102
}
99103

@@ -108,7 +112,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
108112
var platformsError: string = null;
109113
var dvtPlatformClass = this.getClassByName("DVTPlatform");
110114
if(!dvtPlatformClass("loadAllPlatformsReturningError", platformsError)) {
111-
throw new Error(util.format("Unable to loadAllPlatformsReturningError ", platformsError));
115+
errors.fail("Unable to loadAllPlatformsReturningError ", platformsError);
112116
}
113117

114118
var simulatorFrameworkPath = path.join(developerDirectoryPath, iPhoneSimulator.SIMULATOR_FRAMEWORK_RELATIVE_PATH_LEGACY);
@@ -121,7 +125,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
121125
private loadFramework(frameworkPath: string) {
122126
var bundle = $.NSBundle("bundleWithPath", $(frameworkPath));
123127
if(!bundle("load")) {
124-
throw new Error(util.format("Unable to load ", frameworkPath));
128+
errors.fail("Unable to load ", frameworkPath);
125129
}
126130
}
127131

lib/options.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
var yargs = require("yargs");
55

66
var knownOptions: any = {
7-
"sdk": String,
8-
"family": String,
7+
"debug": Boolean,
98
"device": String,
109
"stdout": String,
1110
"stderr": String,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"homepage": "https://github.com/telerik/ios-sim-portable",
2323
"dependencies": {
24+
"colors": "0.6.2",
2425
"fibers": "https://github.com/icenium/node-fibers/tarball/master",
2526
"NodObjC": "https://github.com/telerik/NodObjC/tarball/master",
2627
"underscore": "1.6.0",

0 commit comments

Comments
 (0)