2017-07-27 11:07:05 +00:00
|
|
|
// Generated by CoffeeScript 1.12.7
|
2016-01-11 19:58:35 +00:00
|
|
|
|
|
|
|
/*
|
2017-06-14 21:20:15 +00:00
|
|
|
Copyright 2016-2017 Resin.io
|
2016-01-11 19:58:35 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2017-08-11 10:02:58 +00:00
|
|
|
var Promise, getApplication;
|
2017-03-22 09:46:06 +00:00
|
|
|
|
|
|
|
Promise = require('bluebird');
|
|
|
|
|
|
|
|
exports.getGroupDefaults = function(group) {
|
2017-03-24 09:48:14 +00:00
|
|
|
var _;
|
|
|
|
_ = require('lodash');
|
2017-03-22 09:46:06 +00:00
|
|
|
return _.chain(group).get('options').map(function(question) {
|
|
|
|
return [question.name, question["default"]];
|
|
|
|
}).object().value();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.stateToString = function(state) {
|
2017-03-24 09:48:14 +00:00
|
|
|
var _str, chalk, percentage, result;
|
|
|
|
_str = require('underscore.string');
|
|
|
|
chalk = require('chalk');
|
|
|
|
percentage = _str.lpad(state.percentage, 3, '0') + '%';
|
2017-03-22 09:46:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-24 09:48:14 +00:00
|
|
|
exports.sudo = function(command) {
|
2017-03-27 09:14:55 +00:00
|
|
|
var _, os, presidentExecuteAsync;
|
2017-03-24 09:48:14 +00:00
|
|
|
_ = require('lodash');
|
|
|
|
os = require('os');
|
2017-03-22 09:46:06 +00:00
|
|
|
if (os.platform() !== 'win32') {
|
2017-03-24 09:48:14 +00:00
|
|
|
console.log('If asked please type your computer password to continue');
|
2017-03-22 09:46:06 +00:00
|
|
|
}
|
2017-03-24 09:48:14 +00:00
|
|
|
command = _.union(_.take(process.argv, 2), command);
|
2017-03-27 09:14:55 +00:00
|
|
|
presidentExecuteAsync = Promise.promisify(require('president').execute);
|
|
|
|
return presidentExecuteAsync(command);
|
2017-03-22 09:46:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getManifest = function(image, deviceType) {
|
2017-03-24 09:48:14 +00:00
|
|
|
var imagefs, resin, rindle;
|
|
|
|
rindle = require('rindle');
|
|
|
|
imagefs = require('resin-image-fs');
|
|
|
|
resin = require('resin-sdk-preconfigured');
|
2017-03-22 09:46:06 +00:00
|
|
|
return imagefs.read({
|
|
|
|
image: image,
|
|
|
|
partition: {
|
|
|
|
primary: 1
|
|
|
|
},
|
|
|
|
path: '/device-type.json'
|
|
|
|
}).then(rindle.extractAsync).then(JSON.parse)["catch"](function() {
|
|
|
|
return resin.models.device.getManifestBySlug(deviceType);
|
|
|
|
});
|
|
|
|
};
|
2017-03-24 09:48:14 +00:00
|
|
|
|
|
|
|
exports.osProgressHandler = function(step) {
|
2017-03-28 09:09:58 +00:00
|
|
|
var progressBars, rindle, visuals;
|
2017-03-24 09:48:14 +00:00
|
|
|
rindle = require('rindle');
|
|
|
|
visuals = require('resin-cli-visuals');
|
|
|
|
step.on('stdout', process.stdout.write.bind(process.stdout));
|
|
|
|
step.on('stderr', process.stderr.write.bind(process.stderr));
|
|
|
|
step.on('state', function(state) {
|
|
|
|
if (state.operation.command === 'burn') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return console.log(exports.stateToString(state));
|
|
|
|
});
|
2017-03-28 09:09:58 +00:00
|
|
|
progressBars = {
|
|
|
|
write: new visuals.Progress('Writing Device OS'),
|
|
|
|
check: new visuals.Progress('Validating Device OS')
|
|
|
|
};
|
|
|
|
step.on('burn', function(state) {
|
|
|
|
return progressBars[state.type].update(state);
|
|
|
|
});
|
2017-03-24 09:48:14 +00:00
|
|
|
return rindle.wait(step);
|
|
|
|
};
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
exports.getAppInfo = function(application) {
|
|
|
|
var _, resin;
|
|
|
|
resin = require('resin-sdk-preconfigured');
|
|
|
|
_ = require('lodash');
|
2017-08-11 10:02:58 +00:00
|
|
|
return Promise.join(getApplication(application), resin.models.config.getDeviceTypes(), function(app, config) {
|
2017-03-29 11:03:40 +00:00
|
|
|
config = _.find(config, {
|
|
|
|
'slug': app.device_type
|
|
|
|
});
|
|
|
|
if (config == null) {
|
|
|
|
throw new Error('Could not read application information!');
|
|
|
|
}
|
|
|
|
app.arch = config.arch;
|
|
|
|
return app;
|
|
|
|
});
|
|
|
|
};
|
2017-05-11 11:34:22 +00:00
|
|
|
|
2017-08-11 10:02:58 +00:00
|
|
|
getApplication = function(application) {
|
|
|
|
var match, resin;
|
|
|
|
resin = require('resin-sdk-preconfigured');
|
|
|
|
if ((match = /(\w+)\/(\w+)/.exec(application))) {
|
|
|
|
return resin.models.application.getAppWithOwner(match[2], match[1]);
|
|
|
|
}
|
|
|
|
return resin.models.application.get(application);
|
|
|
|
};
|
|
|
|
|
2017-05-11 11:34:22 +00:00
|
|
|
exports.getSubShellCommand = function(command) {
|
|
|
|
var os;
|
|
|
|
os = require('os');
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
return {
|
|
|
|
program: 'cmd.exe',
|
|
|
|
args: ['/s', '/c', command]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
program: '/bin/sh',
|
|
|
|
args: ['-c', command]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|