2009-10-13 13:26:00 +02:00
|
|
|
exports.print = function (x) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.stdio.write(x);
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.puts = function (x) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.stdio.write(x.toString() + "\n");
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.debug = function (x) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.stdio.writeError("DEBUG: " + x.toString() + "\n");
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.error = function (x) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.stdio.writeError(x.toString() + "\n");
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echos the value of a value. Trys to print the value out
|
|
|
|
* in the best way possible given the different types.
|
|
|
|
*
|
|
|
|
* @param {Object} value The object to print out
|
|
|
|
*/
|
|
|
|
exports.inspect = function (value) {
|
|
|
|
if (value === 0) return "0";
|
|
|
|
if (value === false) return "false";
|
|
|
|
if (value === "") return '""';
|
|
|
|
if (typeof(value) == "function") return "[Function]";
|
|
|
|
if (value === undefined) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.stringify(value);
|
|
|
|
} catch (e) {
|
|
|
|
// TODO make this recusrive and do a partial JSON output of object.
|
|
|
|
if (e.message.search("circular")) {
|
|
|
|
return "[Circular Object]";
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.p = function (x) {
|
|
|
|
exports.error(exports.inspect(x));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.exec = function (command) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var child = process.createChildProcess("/bin/sh", ["-c", command]);
|
2009-10-13 13:26:00 +02:00
|
|
|
var stdout = "";
|
|
|
|
var stderr = "";
|
2009-10-29 23:34:10 +01:00
|
|
|
var promise = new process.Promise();
|
2009-10-13 13:26:00 +02:00
|
|
|
|
|
|
|
child.addListener("output", function (chunk) {
|
|
|
|
if (chunk) stdout += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.addListener("error", function (chunk) {
|
|
|
|
if (chunk) stderr += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.addListener("exit", function (code) {
|
|
|
|
if (code == 0) {
|
|
|
|
promise.emitSuccess(stdout, stderr);
|
|
|
|
} else {
|
|
|
|
promise.emitError(code, stdout, stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|