UNPKG

7.52 kBJavaScriptView Raw
1var inquirer = require("inquirer");
2var _ = require('lodash');
3var chalk = require('chalk');
4var shell = require('shelljs');
5var multichoice = require('../util/multichoice');
6var log = require('../util/log');
7var Login = require('../util/login');
8var evoke = require('../util/evoke');
9var Domo = require('ryuu-client-beta');
10var Promise = require('bluebird');
11var path = require('path');
12var request = Promise.promisify(require('request'));
13var constants = require('../util/constants');
14
15var logo = `
16┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
17┃ ┃
18┃ ┃
19┃ ┃
20┃ ┃
21┃ ┃
22┃ ┃
23┃ ┃
24┃ ┃
25┃ ┃
26┃ ┃
27┃██████████ ██████████ ███ ██ ██████████ ┃
28┃████████████ ███████████████████ ███████████████████┃
29┃███ ████████ ██████████ ██████████ ███┃
30┃███ ██████ ██████████████████████ ██┃
31┃███ ███████ ██████ ███████ ██████ ██┃
32┃███ █████████ ████████ ███ ███████ ████┃
33┃████████████ ██████████████████ ██████████████████┃
34┃██████████ ██████████ ████ ███ ██████████ ┃
35┃ ┃
36┃ ┃
37┃ ┃
38┃ ┃
39┃ ┃
40┃ ┃
41┃ ┃
42┃ ┃
43┃ ┃
44┃ ┃
45┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛`;
46
47
48module.exports = function(program){
49 program
50 .command('login')
51 .description('login to Domo')
52 .option('-i, --instance <value>', 'Domo instance')
53 .option('-u, --user-email <value>', 'user email')
54 .option('--no-upgrade-check', 'do not check for new versions')
55 .action(function(args){
56
57 var previousLogins = Login.getPreviousLogins();
58 var mostRecent = Login.getMostRecentLogin();
59 var prompts = [];
60
61 var versionCheckFn = !args.upgradeCheck ?
62 function (x) { return x; } :
63 verifyVersion;
64
65 if (!args.instance) {
66 prompts.push(multichoice({
67 type: "list",
68 choices: function(){
69 var options = _.pluck(previousLogins, 'instance');
70 options.push('new instance');
71 return options;
72 },
73 message: "Domo instance " + chalk.grey("e.g. company.domo.com "),
74 name: 'instance',
75 default: mostRecent.instance,
76 // only show up if they have previous logins
77 when: function(answers){
78 return previousLogins.length > 0;
79 }
80 }),
81 {
82 type: "input",
83 message: "Domo instance " + chalk.grey("e.g. company.domo.com "),
84 name: "instance",
85 when: function(answers){
86 // only ask if the instance selector wasn't used or new one was selected
87 return !answers.instance || answers.instance === 'new instance';
88 }
89 });
90 }
91
92 // prompt user
93 inquirer.prompt(prompts, function(answers) {
94
95 answers.instance = args.instance || answers.instance;
96
97 if (!answers.instance.endsWith('domo.com') && !answers.instance.endsWith('domotech.io') && !answers.instance.endsWith('domorig.io')){
98 log.fail('Please login using the full Domo instance.', 'Example: "company.domo.com"');
99 }
100
101 var instanceLogin = Login.getLogin(answers.instance);
102 var login;
103 var domo = new Domo(answers.instance, instanceLogin.refreshToken, constants.CLIENT_ID);
104 domo
105 .login()
106 .then(function(l){
107 return login = l;
108 })
109 .then(checkVersion.bind(null, answers.instance))
110 .then(versionCheckFn)
111 .then(function(){
112 console.log(chalk.grey(logo));
113 log.ok('Login to ' + answers.instance + ' successful.', 'Welcome, ' + login.displayName + '.');
114 return Login.persistLogin(answers.instance)(login);
115 })
116 .catch(evoke(log.fail, 'Login unsuccessful'))
117 .finally(process.exit)
118 });
119 });
120}
121
122/**
123 * E.T. phone home
124 */
125function checkVersion(instance){
126 var cliVersion = require(path.resolve(__dirname + '/../package.json')).version;
127 var options = {
128 url: 'https://' + instance + '/domoapps/admin/checkversion?version=' + cliVersion,
129 method: 'GET',
130 json: true
131 };
132
133 return request(options);
134}
135
136
137/**
138 * log message based on the action
139 * if action is 'block' then 'fail' will get called and end the process early
140 */
141function verifyVersion(res){
142
143 var body = res[0].body;
144 if (body.action === 'block'){
145 log.fail(body.message);
146 }
147
148 if (body.action === 'warn'){
149 log.warn(body.message);
150 return promptUpgrade(body.latest);
151 }
152
153 if (body.action === 'info'){
154 log.info(body.message);
155 return promptUpgrade(body.latest);
156 }
157}
158
159function promptUpgrade(latest){
160 return new Promise(function(resolve, reject){
161 inquirer.prompt([
162 {
163 type: "confirm",
164 message: "Would you like to upgrade now?",
165 name: "upgrade",
166 default: true
167 }
168
169 ], function(answers) {
170 if (answers.upgrade){
171 log.info('Upgrading to latest version', 'Hang tight, this may take a moment...');
172 var output = shell.exec('npm install ryuu -g', {async: false, silent: true});
173 if (output.code === 0){
174 log.ok('Upgrade to version '+latest+' successful');
175 resolve();
176 }
177 else {
178 log.warn('Upgrade unsuccessful', 'Please try upgrading manually with npm install -g ryuu');
179 }
180 }
181 else{
182 resolve();
183 }
184 });
185 });
186}