2015-08-13 18:00:23 +00:00
|
|
|
(function() {
|
2015-10-01 17:07:53 +00:00
|
|
|
var Promise, _, capitano, chalk, child_process, os;
|
2015-09-29 17:03:14 +00:00
|
|
|
|
|
|
|
Promise = require('bluebird');
|
2015-08-27 14:01:33 +00:00
|
|
|
|
2015-10-01 17:07:53 +00:00
|
|
|
capitano = Promise.promisifyAll(require('capitano'));
|
|
|
|
|
2015-08-27 14:01:33 +00:00
|
|
|
_ = require('lodash');
|
2015-08-13 18:00:23 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
_.str = require('underscore.string');
|
2015-08-13 18:00:23 +00:00
|
|
|
|
2015-10-01 17:07:53 +00:00
|
|
|
child_process = require('child_process');
|
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
os = require('os');
|
2015-08-13 18:00:23 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
chalk = require('chalk');
|
2015-08-27 14:01:33 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
exports.getOperatingSystem = function() {
|
|
|
|
var platform;
|
|
|
|
platform = os.platform();
|
|
|
|
if (platform === 'darwin') {
|
|
|
|
platform = 'osx';
|
|
|
|
}
|
|
|
|
return platform;
|
2015-08-27 14:01:33 +00:00
|
|
|
};
|
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
exports.stateToString = function(state) {
|
|
|
|
var percentage, result;
|
|
|
|
percentage = _.str.lpad(state.percentage, 3, '0') + '%';
|
|
|
|
result = (chalk.blue(percentage)) + " " + (chalk.cyan(state.operation.command));
|
|
|
|
switch (state.operation.command) {
|
|
|
|
case 'copy':
|
|
|
|
return result + " " + state.operation.from.path + " -> " + state.operation.to.path;
|
|
|
|
case 'replace':
|
|
|
|
return result + " " + state.operation.file.path + ", " + state.operation.copy + " -> " + state.operation.replace;
|
|
|
|
case 'run-script':
|
|
|
|
return result + " " + state.operation.script;
|
|
|
|
default:
|
|
|
|
throw new Error("Unsupported operation: " + state.operation.type);
|
|
|
|
}
|
2015-08-27 14:01:33 +00:00
|
|
|
};
|
|
|
|
|
2015-09-29 17:03:14 +00:00
|
|
|
exports.waitStream = function(stream) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
stream.on('close', resolve);
|
2015-09-29 17:36:29 +00:00
|
|
|
stream.on('end', resolve);
|
2015-09-29 17:03:14 +00:00
|
|
|
return stream.on('error', reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-10-01 17:07:53 +00:00
|
|
|
exports.sudo = function(command) {
|
|
|
|
var spawn;
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
return capitano.runAsync(command.join(' '));
|
|
|
|
}
|
|
|
|
command = _.union(_.take(process.argv, 2), command);
|
|
|
|
spawn = child_process.spawn('sudo', command, {
|
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
|
|
|
return exports.waitStream(spawn);
|
|
|
|
};
|
|
|
|
|
2015-08-13 18:00:23 +00:00
|
|
|
}).call(this);
|