UNPKG

4.95 kBJavaScriptView Raw
1var Domo = require('ryuu-client-beta');
2var inquirer = require('inquirer');
3var _ = require('lodash');
4var opn = require('opn');
5var Manifest = require('../util/manifest');
6var Login = require('../util/login');
7var log = require('../util/log');
8var appStructure = require('../util/appStructure');
9var messages = require('../util/messages');
10var evoke = require('../util/evoke');
11var constants = require('../util/constants');
12
13module.exports = function(program){
14 program
15 .command('publish')
16 .description('publish a new or existing Custom App design')
17 .option('-g, --go', 'navigate to design in Asset Library after publishing')
18 .action(function(options){
19 var manifest = Manifest.get(program.manifest);
20 if (!manifest) return;
21
22 var login = Login.getMostRecentLogin();
23 Login.verifyLogin(login);
24 var domo = new Domo(login.instance, login.refreshToken, constants.CLIENT_ID);
25
26 if (!appStructure.hasThumbnail()) {
27 log.warn(messages.THUMBNAIL_CREATE_WARNING, messages.CREATE_THUMBNAIL);
28 }
29
30 if (!manifest.id){
31 createDesign(domo, manifest, login, options);
32 }
33 else{
34 domo.getDesign(manifest.id).then(function(result) {
35 var latestVersionNumber = result.latestVersion;
36 var latestVersion = result.versions.filter(function(obj) {
37 return obj.version === latestVersionNumber;
38 });
39 var oldMapping = latestVersion[0].mapping[0];
40 var newMapping = manifest.mapping[0];
41
42 if (oldMapping) {
43 delete oldMapping.dql;
44 }
45
46 if(!_.isEqual(oldMapping, newMapping)) {
47 log.warn('Mapping has changed. Check current cards for potential miss-mappings.');
48 }
49
50 domo.uploadAllAssets(manifest)
51 .tap(evoke(log.ok, 'Publishing ' + manifest.name + ' to ' + login.instance + ' on ' + new Date()))
52 .then(logUploadSuccess)
53 .catch(function(e){
54 if(e.statusCode === 401) {
55 log.notAuthenticated(login);
56 }
57 else if(e.statusCode === 403) {
58 log.fail('You do not have access to the design with id ' + manifest.id + ' on ' + login.instance
59 + '.\nIf that seems unlikely, other possibilities are that you are publishing this design to a new environment other than the one in which it was first created.'
60 + '\nAlternatively, you may have inadvertently changed the design id.');
61 }
62 else if(e.message && e.message.startsWith('DA')) {
63 switch (e.message.substr(0, 6)) {
64 case 'DA0086':
65 log.warn(e.message);
66 inquirer.prompt([
67 {
68 type: 'confirm',
69 message: 'This design has been deleted. Would you like to publish a new design with these assets?',
70 name: 'askRecreate'
71 }
72 ], function (answers) {
73 if (answers.askRecreate){
74 manifest.id = null;
75 createDesign(domo, manifest, login);
76 }
77 });
78
79 break;
80
81 default:
82 // Pass on error messages that are in the set of documented errors.
83 log.fail(e.message);
84 break;
85 }
86 }
87 else {
88 log.fail(log.handleErrorMessage(e, 'Error uploading assets'));
89 }
90 });
91 })
92 .then(function() {
93 goToDomo(options, domo, manifest);
94 })
95 .catch(function(e) {
96 log.clientRequestFailed('Error uploading assets');
97 });
98 }
99 });
100}
101
102
103/**
104 * @param {Object} domo
105 * @param {Object} manifest
106 * @param {Object} login
107 * @param {Object} options
108 * @return {Promise}
109 */
110function createDesign(domo, manifest, login, options) {
111 return domo.createDesign(manifest)
112 .then(function(designResult){
113 var design = designResult[0];
114 // add the version returned when design published
115 design.version = designResult[1].version;
116 return Manifest.persistDesignId(design, manifest);
117 })
118 .tap(evoke(log.ok, 'New design created on ' + login.instance))
119 .catch(evoke(log.fail, 'Error creating new design'))
120 .then(domo.uploadAllAssets.bind(domo, manifest))
121 .then(logUploadSuccess)
122 .catch(function(e){
123 log.fail(e || 'Error uploading assets');
124 })
125 .then(function() {
126 goToDomo(options, domo, manifest);
127 });
128}
129
130function logUploadSuccess(files){
131 files.forEach(function(file){
132 log.ok('Uploaded: ' + file.path);
133 });
134}
135
136function goToDomo(options, domo, manifest) {
137 console.log('Design can be found at https://' + domo.instance + '/assetlibrary?designId=' + manifest.id);
138 if(options.go) {
139 opn('https://' + domo.instance + '/assetlibrary?designId=' + manifest.id);
140 }
141}