UNPKG

3.08 kBJavaScriptView Raw
1var Promise = require('bluebird');
2var inquirer = require('inquirer');
3var Domo = require('ryuu-client-beta');
4var Manifest = require('../util/manifest');
5var Login = require('../util/login');
6var log = require('../util/log');
7var evoke = require('../util/evoke');
8var constants = require('../util/constants');
9
10module.exports = function(program){
11 program
12 .command('migrate')
13 .description('migrate a mason app to a Custom App')
14 .option('-i, --instance <value>', 'Domo instance')
15 .option('-a, --app-id <id>', 'app id')
16 .action(function(args){
17 var manifest = Manifest.get(program.manifest);
18 var mostRecent = Login.getMostRecentLogin();
19 if (!manifest) return;
20
21 var prompts = [];
22
23 if (!args.instance) {
24 prompts.push({
25 type: "input",
26 message: "Domo instance",
27 name: "instance",
28 default: mostRecent.instance
29 });
30 }
31
32 if (!args.appId) {
33 prompts.push({
34 type: "input",
35 message: "app id",
36 name: "id"
37 });
38 }
39
40 inquirer.prompt(prompts, function(answers) {
41 answers.instance = args.instance || answers.instance;
42 answers.id = args.appId || answers.id;
43 var login = Login.getLogin(answers.instance);
44 Login.verifyLogin(login);
45
46 var domo = new Domo(answers.instance, login.refreshToken, constants.CLIENT_ID);
47
48 migrateApp(domo, answers.id, manifest)
49 .tap(evoke(log.ok, 'Migration successful'))
50 .catch(evoke(log.fail, 'Migration unsuccessful'))
51 .finally(process.exit)
52 });
53 });
54
55}
56
57
58/**
59 * migrate a mason app to a Custom App
60 * 1. get the kpi info
61 * 2. create a new Custom App design
62 * 3. publish design assets
63 * 4. create a new Custom App
64 * 5. update the kpi
65 */
66function migrateApp(domo, appId, manifest){
67 return Promise.all([
68 domo.getKpi(appId),
69
70 // .catch(evoke(fail, 'No app found with id ' + appId)),
71 // publish new design (and persist id) if not already published
72 domo.createDesign(manifest)
73 .tap(evoke(log.ok, 'Design published'))
74 .then(function(designResult){
75 var design = designResult[0];
76 return Manifest.persistDesignId(design, manifest);
77 })
78 .catch(function(){
79 return manifest;
80 })
81 ])
82 .spread(function(kpi, design){
83 return domo
84 .createApp(design.id)
85 .tap(evoke(log.ok, 'New Custom App created'))
86 .catch(evoke(log.fail, 'Error creating new Custom App'))
87 .then(function(domoapp){
88 kpi.type = kpi.metadata.chartType = kpi.metadata.kpiType = 'domoapp';
89 kpi.domoapp.id = domoapp.id;
90
91 return domo.updateKpi(kpi)
92 .tap(evoke(log.ok, 'Card type updated and linked'))
93 .catch(evoke(log.fail, 'Error updating card'))
94 .then(function(){
95 return domo.uploadAllAssets(manifest);
96 })
97 .tap(evoke(log.ok, 'Assets uploaded'))
98 .catch(evoke(log.fail, 'Error uploading assets'));
99 });
100 });
101
102}