diff --git a/bin/matrix-install.js b/bin/matrix-install.js index fd4edfc..d2547d9 100644 --- a/bin/matrix-install.js +++ b/bin/matrix-install.js @@ -44,7 +44,7 @@ async.series([ } var versionId; - //Validate if the version exist and get the version Id + //Validate if the version exists and get the version Id if (version) { versionId = _.findKey(result.versions, function(appVersion, versionId) { if (appVersion.version === version) return true; @@ -56,35 +56,39 @@ async.series([ return process.exit(); } } else { - //If in the command doesn't set the version use a current version of app + //If the command didn't set a version use current app version versionId = result.meta.currentVersion; } debug('VERSION: '.blue, versionId, 'APP: '.blue, appName); - var options = { - policy: result.versions[versionId].policy, - name: appName, - id: result.id, - versionId: versionId - } - - Matrix.helpers.trackEvent('app-install', { aid: appName, did: Matrix.config.device.identifier }); + Matrix.helpers.promptSettings(function (err, clearSettings) { - Matrix.helpers.installApp(options, function(err) { - Matrix.loader.stop(); - if (err) { - console.log(err); - process.exit(1); + var options = { + policy: result.versions[versionId].policy, + name: appName, + id: result.id, + versionId: versionId, + clearSettingsOnUpdate: clearSettings } - console.log(t('matrix.install.app_install_success').green); - process.exit(0); + Matrix.helpers.trackEvent('app-install', { aid: appName, did: Matrix.config.device.identifier }); + + Matrix.helpers.installApp(options, function(err) { + Matrix.loader.stop(); + if (err) { + console.log(err); + process.exit(1); + } + + console.log(t('matrix.install.app_install_success').green); + process.exit(0); + }); + }); }); - function displayHelp() { console.log('\n> matrix install ¬\n'); console.log('\t matrix install app -', t('matrix.install.help_app', { app: '' }).grey) diff --git a/lib/helpers.js b/lib/helpers.js index 6d7ecf8..b2a001b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -620,13 +620,24 @@ function checkPolicy(policy, name, cb) { } } -function installSetup(appId, versionId, policy, callback) { +/** + * Unified installation process, receives the parameters of the app along with its configuration and uses matrix-firebase to proceed + * @param {*} options Object containing installation parameters + * appId {String} + * versionId {String} + * policy {Object} JSON + * clearSettings {bool} Wether config.settings should be cleared or not + * @param {*} callback + */ +function installSetup(options, callback) { debug("\nInstalling to device... ") var progress; Matrix.loader.start(); - Matrix.firebase.app.install(Matrix.config.user.token, Matrix.config.device.identifier, appId, versionId, policy, { + options.deviceId = Matrix.config.device.identifier; + + Matrix.firebase.app.install(options, { error: function(err) { Matrix.loader.stop(); if (err && err.hasOwnProperty('details') && err.details.hasOwnProperty('error')) { @@ -639,9 +650,9 @@ function installSetup(appId, versionId, policy, callback) { finished: function() { debug('install finished'); // Watch for the app on the user record - Matrix.firebase.deviceapps.watchForNewApp(appId, function(app) { + Matrix.firebase.deviceapps.watchForNewApp(options.appId, function(app) { debug('new app>', app) - if (_.has(app, 'runtime.status') && app.runtime.status != 'pending') { + if (_.has(app, 'runtime.status') && app.runtime.status !== 'pending') { var error; Matrix.loader.stop(); var status = app.runtime.status; @@ -691,14 +702,26 @@ function installSetup(appId, versionId, policy, callback) { } -/* Details: { id - appId, versionId - versionId} */ - +/** + * Installs an application + * @param {Object} details + ** Details: { id - appId, versionId - versionId, clearSettings - clearSettings } + * @param {Function} callback + */ function installApp(details, callback) { //TODO validate details object contents. policy, name, id, versionId + var options = { + appId: details.id, + versionId: details.versionId, + clearSettingsOnUpdate: details.clearSettingsOnUpdate + } + if (details.hasOwnProperty('skipPolicyCheck') && details.skipPolicyCheck === true) { var policy = allowAllPolicy(details.policy); - installSetup(details.id, details.versionId, policy, callback); + options.policy = policy; + + installSetup(options, callback); } else { checkPolicy(details.policy, details.name, function(err, policy) { console.warn('\n⇒ ' + t('matrix.install.installing_x_with_policy', { app: details.name.yellow }) + ':'); @@ -715,7 +738,8 @@ function installApp(details, callback) { }) }); - installSetup(details.id, details.versionId, policy, callback); + options.policy = policy; + installSetup(options, callback); }); } } @@ -1263,6 +1287,44 @@ function syncRefreshToken(refreshToken) { return result; } +function promptSettings(cb) { + var result = false; + var clearP = { + name: 'clear', + description: "Reset application settings to default?", + default: 'y/N', + pattern: /y|n|yes|no|Y|N|y\|N/, + message: "Please answer y or n." + } + + prompt = require('prompt'); + prompt.delimiter = ''; + prompt.message = ''; + + var toPrompt = [clearP]; + + prompt.start(); + Matrix.loader.stop(); + prompt.get(toPrompt, function (err, answers) { + + if (err) { + if (err.toString().indexOf('canceled') > 0) { + console.log(''); + process.exit(); + } else { + console.log("Error: ", err); + process.exit(); + } + } + + if (answers.clear.toLowerCase() === 'y' || answers.clear.toLowerCase() === 'yes') result = true; + else result = false; + + cb(err, result); + + }); +} + module.exports = { allowAllPolicy: allowAllPolicy, checkAppFolder: checkAppFolder, @@ -1295,6 +1357,7 @@ module.exports = { changeName:changeName, uploadPackage: uploadPackage, zipAppFolder: zipAppFolder, + promptSettings: promptSettings, refreshDeviceMap: refreshDeviceMap, trackEvent: trackEvent };