mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-22 15:02:22 +00:00
210680c9c9
In my system (MBPr 13), printing the current version takes over 2 seconds: ```sh $ time ./bin/resin version 2.4.0 ./bin/resin version 1.37s user 0.19s system 73% cpu 2.130 total ``` The CLI takes almost all of these time to parse the dependency tree before returning control over the actually called command. To mitigate this problem, we only require the NPM dependencies a command requires when executing such command, and thus prevent dependencies from being required and parsed unnecessary. After this improvement, printing the original example (`resin version`) returns in less than a second (2x improvement): ```sh $ time ./bin/resin version 2.4.0 ./bin/resin version 0.88s user 0.09s system 102% cpu 0.938 total ```
175 lines
6.1 KiB
JavaScript
175 lines
6.1 KiB
JavaScript
(function() {
|
|
var commandOptions, stepHandler;
|
|
|
|
commandOptions = require('./command-options');
|
|
|
|
exports.download = {
|
|
signature: 'os download <type>',
|
|
description: 'download an unconfigured os image',
|
|
help: 'Use this command to download an unconfigured os image for a certain device type.\n\nExamples:\n\n $ resin os download parallella -o ../foo/bar/parallella.img',
|
|
permission: 'user',
|
|
options: [
|
|
{
|
|
signature: 'output',
|
|
description: 'output path',
|
|
parameter: 'output',
|
|
alias: 'o',
|
|
required: 'You have to specify an output location'
|
|
}
|
|
],
|
|
action: function(params, options, done) {
|
|
var fs, manager, rindle, unzip, visuals;
|
|
unzip = require('unzip2');
|
|
fs = require('fs');
|
|
rindle = require('rindle');
|
|
manager = require('resin-image-manager');
|
|
visuals = require('resin-cli-visuals');
|
|
console.info("Getting device operating system for " + params.type);
|
|
return manager.get(params.type).then(function(stream) {
|
|
var bar, output, spinner;
|
|
bar = new visuals.Progress('Downloading Device OS');
|
|
spinner = new visuals.Spinner('Downloading Device OS (size unknown)');
|
|
stream.on('progress', function(state) {
|
|
if (state != null) {
|
|
return bar.update(state);
|
|
} else {
|
|
return spinner.start();
|
|
}
|
|
});
|
|
stream.on('end', function() {
|
|
return spinner.stop();
|
|
});
|
|
if (stream.mime === 'application/zip') {
|
|
output = unzip.Extract({
|
|
path: options.output
|
|
});
|
|
} else {
|
|
output = fs.createWriteStream(options.output);
|
|
}
|
|
return rindle.wait(stream.pipe(output))["return"](options.output);
|
|
}).tap(function(output) {
|
|
return console.info('The image was downloaded successfully');
|
|
}).nodeify(done);
|
|
}
|
|
};
|
|
|
|
stepHandler = function(step) {
|
|
var _, bar, helpers, rindle, visuals;
|
|
_ = require('lodash');
|
|
rindle = require('rindle');
|
|
visuals = require('resin-cli-visuals');
|
|
helpers = require('../utils/helpers');
|
|
step.on('stdout', _.bind(process.stdout.write, process.stdout));
|
|
step.on('stderr', _.bind(process.stderr.write, process.stderr));
|
|
step.on('state', function(state) {
|
|
if (state.operation.command === 'burn') {
|
|
return;
|
|
}
|
|
return console.log(helpers.stateToString(state));
|
|
});
|
|
bar = new visuals.Progress('Writing Device OS');
|
|
step.on('burn', _.bind(bar.update, bar));
|
|
return rindle.wait(step);
|
|
};
|
|
|
|
exports.configure = {
|
|
signature: 'os configure <image> <uuid>',
|
|
description: 'configure an os image',
|
|
help: 'Use this command to configure a previously download operating system image with a device.\n\nExamples:\n\n $ resin os configure ../path/rpi.img 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9',
|
|
permission: 'user',
|
|
options: [
|
|
{
|
|
signature: 'advanced',
|
|
description: 'show advanced commands',
|
|
boolean: true,
|
|
alias: 'v'
|
|
}
|
|
],
|
|
action: function(params, options, done) {
|
|
var _, form, helpers, init, resin;
|
|
_ = require('lodash');
|
|
resin = require('resin-sdk');
|
|
form = require('resin-cli-form');
|
|
init = require('resin-device-init');
|
|
helpers = require('../utils/helpers');
|
|
console.info('Configuring operating system image');
|
|
return resin.models.device.get(params.uuid).get('device_type').then(resin.models.device.getManifestBySlug).get('options').then(function(questions) {
|
|
var advancedGroup, override;
|
|
if (!options.advanced) {
|
|
advancedGroup = _.findWhere(questions, {
|
|
name: 'advanced',
|
|
isGroup: true
|
|
});
|
|
if (advancedGroup != null) {
|
|
override = helpers.getGroupDefaults(advancedGroup);
|
|
}
|
|
}
|
|
return form.run(questions, {
|
|
override: override
|
|
});
|
|
}).then(function(answers) {
|
|
return init.configure(params.image, params.uuid, answers).then(stepHandler);
|
|
}).nodeify(done);
|
|
}
|
|
};
|
|
|
|
exports.initialize = {
|
|
signature: 'os initialize <image>',
|
|
description: 'initialize an os image',
|
|
help: 'Use this command to initialize a previously configured operating system image.\n\nExamples:\n\n $ resin os initialize ../path/rpi.img --type \'raspberry-pi\'',
|
|
permission: 'user',
|
|
options: [
|
|
commandOptions.yes, {
|
|
signature: 'type',
|
|
description: 'device type',
|
|
parameter: 'type',
|
|
alias: 't',
|
|
required: 'You have to specify a device type'
|
|
}, {
|
|
signature: 'drive',
|
|
description: 'drive',
|
|
parameter: 'drive',
|
|
alias: 'd'
|
|
}
|
|
],
|
|
root: true,
|
|
action: function(params, options, done) {
|
|
var Promise, form, init, patterns, resin, umount;
|
|
Promise = require('bluebird');
|
|
umount = Promise.promisifyAll(require('umount'));
|
|
resin = require('resin-sdk');
|
|
form = require('resin-cli-form');
|
|
init = require('resin-device-init');
|
|
patterns = require('../utils/patterns');
|
|
console.info('Initializing device');
|
|
return resin.models.device.getManifestBySlug(options.type).then(function(manifest) {
|
|
var ref;
|
|
return (ref = manifest.initialization) != null ? ref.options : void 0;
|
|
}).then(function(questions) {
|
|
return form.run(questions, {
|
|
override: {
|
|
drive: options.drive
|
|
}
|
|
});
|
|
}).tap(function(answers) {
|
|
var message;
|
|
if (answers.drive == null) {
|
|
return;
|
|
}
|
|
message = "This will erase " + answers.drive + ". Are you sure?";
|
|
return patterns.confirm(options.yes, message)["return"](answers.drive).then(umount.umountAsync);
|
|
}).tap(function(answers) {
|
|
return init.initialize(params.image, options.type, answers).then(stepHandler);
|
|
}).then(function(answers) {
|
|
if (answers.drive == null) {
|
|
return;
|
|
}
|
|
return umount.umountAsync(answers.drive).tap(function() {
|
|
return console.info("You can safely remove " + answers.drive + " now");
|
|
});
|
|
}).nodeify(done);
|
|
}
|
|
};
|
|
|
|
}).call(this);
|