-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandrake.js
executable file
Β·272 lines (239 loc) Β· 8.5 KB
/
mandrake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env node
"use strict";
require('shelljs/global')
const inquirer = require('inquirer')
const monet = require('monet')
const level = require('level')
const getTemplateConfig = require('./core/templates').getTemplateConfig;
const isItThefirstTime = require('./core/templates').isItThefirstTime;
const buildTemplatesTitlesList = require('./core/templates').buildTemplatesTitlesList;
const getTemplatesTable = require('./core/templates').getTemplatesTable;
const createBrandNewApp = require('./core/applications').createBrandNewApp;
const createAddon = require('./core/addons').createAddon;
const runCmd = require('./core/commands').runCmd;
const logo = require('./core/logo');
isItThefirstTime()
/**
* Level section
* see https://github.com/Level/level
* Level is used to keep some informations in memory
* eg: to propose the last used organization, ...
*/
let db = level('./mandrakedb')
let templatesChoice = {
type: 'list',
pageSize:15,
name: 'templateTitle',
message: 'What kind of application do yo want to generate?',
choices: buildTemplatesTitlesList
}
let regionChoice = {
type: 'list',
name: 'region',
message: 'Where do you want to deploy your application?',
choices: ['par', 'mtl'],
default: function() {
return new Promise((resolve, reject) => {
db.get('last_app_region', (err, value) => {
//if (err) reject('???')
resolve(value)
})
})
}
}
let addOnRegionChoice = {
type: 'list',
name: 'region',
message: 'Where do you want to deploy your addon?',
choices: ['eu', 'us'],
default: function() {
return new Promise((resolve, reject) => {
db.get('last_addon_region', (err, value) => {
//if (err) reject('???')
resolve(value)
})
})
}
}
let organizationName = {
type: 'input',
name: 'organization',
message: 'What is your organization name?',
default: function() {
return new Promise((resolve, reject) => {
db.get('last_organization', (err, value) => {
//if (err) reject('???')
resolve(value)
})
})
}
}
let applicationName = {
type: 'input',
name: 'application',
message: 'What is your application name (or project directory name)?',
validate: function(input) {
return new Promise((resolve, reject) => {
db.get(`app:${input}`, (err, value) => {
if (err) {
resolve(true) // if the application name doesn't exist in the database, it's fine π€
} else {
resolve('π‘ This application already exists in the database')
}
})
})
}
}
let addonName = {
type: 'input',
name: 'addon',
message: 'What is your addon name?',
validate: function(input) {
return new Promise((resolve, reject) => {
db.get(`addon:${input}`, (err, value) => {
if (err) {
resolve(true) // if the addon name doesn't exist in the database, it's fine π€
} else {
resolve('π‘ This addon already exists in the database')
}
})
})
}
}
let displayName = {
type: 'input',
name: 'displayName',
message: 'What is your display name (the display named in the CC Console)?',
validate: function(input) {
return new Promise((resolve, reject) => {
db.get(`displayName:${input}`, (err, value) => {
if (err) {
resolve(true) // if the display name doesn't exist in the database, it's fine π€
} else {
resolve('π‘ This display name already exists in the database')
}
})
})
}
}
let domainName = {
type: 'input',
name: 'domain',
message: 'What is your domain name (<domain>.cleverapps.io)?',
validate: function(input) {
return new Promise((resolve, reject) => {
db.get(`domain:${input}`, (err, value) => {
if (err) {
resolve(true) // if the domain name doesn't exist in the database, it's fine π€
} else {
resolve('π‘ This domain already exists in the database')
}
})
})
}
}
inquirer.prompt([
templatesChoice
]).then((answers) => {
//let template = answers.template
// search templane directory name by title
let template = getTemplatesTable().find(item => item.title == answers.templateTitle).template
// Do something more functional
if(template==null) { throw Error("π") }
switch(template.split("-")[0]) {
case 'app':
inquirer.prompt([
regionChoice, organizationName, applicationName, displayName, domainName
]).then((answers) => {
db.put('last_organization', answers.organization, (err) => {})
db.put('last_app_region', answers.region, (err) => {})
//let config = require(`${process.cwd()}/templates/${template}/config.js`)
getTemplateConfig({template: template}).cata(
err => {
console.error(`π‘ π`, err)
throw new Error("π‘ Houston? We have a problem [getTemplateConfig when creating application]")
},
config => {
//TODO: ask for GitHub or Brand new application project
if(config.prompts) { // check if the template has "embedded" questions
inquirer.prompt(config.prompts(db)).then(promptsAnswers => {
// pass promptsAnswers as parameters to use it cmd
createBrandNewApp({template, db, answers, promptsAnswers}).cata(
err => console.error(`π‘ π`, err),
res => {
console.info('π© β¨ π π')
console.log(res)
db.put("app:"+res.application, res, (err) => {})
db.put("domain:"+res.domain, true, (err) => {})
db.put("displayName:"+res.displayName, true, (err) => {})
console.log("π Remember, to deploy again: git push clever master")
}
)
}) // end of inquirer.prompt
} else {
createBrandNewApp({template, db, answers, promptsAnswers: null}).cata(
err => console.error(`π‘ π`, err),
res => {
console.info('π© β¨ π π')
console.log(res)
db.put("app:"+res.application, res, (err) => {})
db.put("domain:"+res.domain, true, (err) => {})
db.put("displayName:"+res.displayName, true, (err) => {})
console.log("π Remember, to deploy again: git push clever master")
}
)
} // end of if
} // end of Right(config)
) // end of getTemplateConfig cata
})
break;
case 'addon':
inquirer.prompt([
addOnRegionChoice, organizationName, addonName
]).then((answers) => {
db.put('last_organization', answers.organization, (err) => {})
db.put('last_addon_region', answers.region, (err) => {})
createAddon({template, db, answers}).cata(
err => console.error(`π‘ π`, err),
res => {
console.info('π© β¨ π π')
console.log(res)
db.put("addon:"+res.addon, res, (err) => {})
}
)
})
break;
case 'cmd':
//let config = require(`${process.cwd()}/templates/${template}/config.js`)
getTemplateConfig({template: template}).cata(
err => {
console.error(`->π‘ π`, err)
throw new Error("π‘ Houston? We have a problem [getTemplateConfig when running command]")
},
config => {
if(config.prompts) { // check if the template has "embedded" questions
inquirer.prompt(config.prompts(db)).then(promptsAnswers => {
runCmd({template, db, promptsAnswers}).cata(
err => console.error(`π‘ π`, err),
res => {
console.info('π© β¨ π π')
console.log(res)
}
)
})
} else {
runCmd({template, db, promptsAnswers: null}).cata(
err => console.error(`π‘ π`, err),
res => {
console.info('π© β¨ π π')
console.log(res)
}
)
} // end of if
} // end of Right(config)
) // end of getTemplateConfig cata
break;
default:
//TODO
}
}) // end of inquirer.prompt