refactor and add more logging to instruments_client.js

This commit is contained in:
Jonathan Lipps
2013-05-28 09:48:02 -07:00
parent 55805bcc5f
commit a47ba7f273
+64 -44
View File
@@ -11,16 +11,25 @@ var sysExec = function(cmd) {
if (res.exitCode !== 0) {
throw new Error("Failed executing the command " + cmd + " (exit code " + res.exitCode + ")");
} else {
var path = res.stdout.trim();
if (path.length) {
return path;
var output = res.stdout.trim();
if (output.length) {
return output;
} else {
throw new Error("Executing " + cmd + " failed!");
}
}
};
// clientPath is relative to where you run the appium server from
// get npm-installed instruments_client bin if necessary
var globalPath = (function() {
try {
return sysExec('which instruments_client');
} catch (e) {
return null;
}
})();
// figure out where instruments client is (relative to where appium is run)
var clientPath = (function() {
var client = 'instruments/client.js';
var module = 'node_modules/appium/';
@@ -33,9 +42,55 @@ var clientPath = (function() {
sysExec('ls ' + module + client);
return module + client;
} catch(e) {
if (globalPath === null) {
console.log("WARNING: could not find instruments/client.js in its " +
"usual place, and global instruments_client not aroudn " +
"either. This could cause problems");
}
}
}
})();
// figure out where node is
var nodePath = (function() {
var path = null;
try {
path = sysExec('which node');
} catch (e) {
var appScript = [
'try'
, ' set appiumIsRunning to false'
, ' tell application "System Events"'
, ' set appiumIsRunning to name of every process contains "Appium"'
, ' end tell'
, ' if appiumIsRunning then'
, ' tell application "Appium" to return node path'
, ' end if'
, 'end try'
, 'return "NULL"'
].join("\n");
var appNodeWorked = false;
try {
path = sysExec("osascript -e '" + appScript + "'");
appNodeWorked = path !== "NULL";
} catch(e) {}
if (!appNodeWorked) {
try {
path = sysExec("ls /usr/local/bin/node");
} catch (e) {
try {
path = sysExec("ls /opt/local/bin/node");
} catch (e) {
throw new Error("Could not find node using `which node`, at /usr/" +
"local/bin/node, at /opt/local/bin/node, or by " +
"querying Appium.app. Where is it?");
}
}
}
}
return path;
})();
var isAppiumApp = (function() {
try {
return sysExec('echo $Appium_app') !== null;
@@ -66,8 +121,12 @@ var sendResultAndGetNext = function(result) {
}
if (res.exitCode !== 0) {
console.log("Error talking with instruments client, here's stderr:");
var cmd = binaryPath + " " + args.join(" ");
console.log("Instruments client (" + cmd + ") exited with " + res.exitCode +
", here's stderr:");
console.log(res.stderr);
console.log("And stdout:");
console.log(res.stdout);
}
return res.stdout;
};
@@ -76,42 +135,3 @@ var getFirstCommand = function() {
return sendResultAndGetNext();
};
var globalPath = null;
try {
globalPath = sysExec('which instruments_client');
} catch (e) { }
var nodePath = null;
try {
nodePath = sysExec('which node');
} catch (e) {
var appScript = [
'try'
, ' set appiumIsRunning to false'
, ' tell application "System Events"'
, ' set appiumIsRunning to name of every process contains "Appium"'
, ' end tell'
, ' if appiumIsRunning then'
, ' tell application "Appium" to return node path'
, ' end if'
, 'end try'
, 'return "NULL"'
].join("\n");
var appNodeWorked = false;
try {
nodePath = sysExec("osascript -e '" + appScript + "'");
appNodeWorked = nodePath !== "NULL";
} catch(e) {}
if (!appNodeWorked) {
try {
nodePath = sysExec("ls /usr/local/bin/node");
} catch (e) {
try {
nodePath = sysExec("ls /opt/local/bin/node");
} catch (e) {
throw new Error("Could not find node using `which node`, at /usr/" +
"local/bin/node, at /opt/local/bin/node, or by " +
"querying Appium.app. Where is it?");
}
}
}
}