-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
241 lines (188 loc) · 5.4 KB
/
app.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
/**
* Jira Console
*
* @author Mohammad Fares <[email protected]>
*/
var request = require('request'),
is = require('is_js'),
q = require('q'),
inquirer = require('inquirer'),
lodash = require('lodash'),
moment = require('moment'),
async = require('async'),
glob = require('glob'),
clear = require('clear'),
chalk = require('chalk'),
yaml = require('js-yaml'),
ora = require('ora'),
fs = require('fs-extra'),
onChange = require('on-change'),
changeCase = require('change-case'),
Listr = require('listr'),
EventEmitter = require('events'),
url = require('url'),
querystring = require('querystring'),
Flow = require('step-flow'),
fuzzy = require('fuzzy');
var config = require('./config'),
data = require('./data'),
input = require('./input'),
tasks = require('./tasks'),
api = require('./api'),
jira = require('./jira'),
di = require('./di');
var flow = new Flow();
// Define the DI as a global object
global.di = di;
// Fix
EventEmitter.defaultMaxListeners = 0;
// Dependency Injection
di.set('request', request);
di.set('is', is);
di.set('q', q);
di.set('inquirer', inquirer);
di.set('moment', moment);
di.set('async', async);
di.set('glob', glob);
di.set('clear', clear);
di.set('lodash', lodash);
di.set('chalk', chalk);
di.set('yaml', yaml);
di.set('ora', ora);
di.set('fs', fs);
di.set('onChange', onChange);
di.set('changeCase', changeCase);
di.set('Listr', Listr);
di.set('url', url);
di.set('querystring', querystring);
di.set('Flow', Flow);
di.set('fuzzy', fuzzy);
di.set('config', config);
di.set('data', data);
di.set('input', input);
di.set('tasks', tasks);
di.set('api', api);
di.set('jira', jira);
// Loading
input.load();
tasks.load();
// Steps
flow.use('main', main);
// Flow
flow.use('selectProject', selectProject);
flow.use('selectTask', selectTask);
flow.use('continueConfirm', continueConfirm);
// Flow
flow.use('manageProjects', manageProjects);
// Flow
flow.use('configurations', configurations);
// Error handling
flow.catch(function(error) {
console.error(chalk.red(error));
});
// The starting point
flow.run({});
/**
* Main menu
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function main(context, next, jump) {
var menu = [
{title: 'Select Project', summary: 'Select from the already added projects', step: 'selectProject'},
{title: 'Manage Projects', summary: 'Add and remove projects', step: 'manageProjects'},
{title: 'Configurations and Preferences', summary: 'View and edit the configurations and preferencesstep', step: 'configurations'}
];
input.selectItem(menu, 'Select', ['title'], ['summary']).then(function(item) {
jump(item.step);
});
}
/**
* Select a project and store it as context.project
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function selectProject(context, next, jump) {
input.selectItem(di.data.projects, 'Select a project', ['name'], ['key']).then(function(project) {
// Add the selected project to the context object
context.project = project;
next();
});
}
/**
* Select a task to be performed on the project
* and store it as context.task
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function selectTask(context, next, jump) {
input.selectItem(tasks.getTasks(), 'Select a task', ['name'], ['summary']).then(function(task) {
// Add the selected task to the context object
context.task = task;
// Execute the task
task.run(context).then(function(result) {
next();
}).catch(function(error) {
console.error(chalk.red(error));
next();
});
});
}
/**
* Prompt the user to confirm if he want to
* continue using the app or to exit
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function continueConfirm(context, next, jump) {
di.input.confirm('Do you want to continue').then(function(result) {
if (!result) {
return process.exit();
}
clear();
jump('selectProject');
});
}
/**
* Load all projects to choose and add a new one
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function manageProjects(context, next, jump) {
// Get a list of all projects and pick [id, key, name] keys
var projects = di.api.get('/rest/api/2/project').then(function(projects) {
return lodash.map(projects, function(project) {
return lodash.pick(project, ['id', 'key', 'name'])
});
});
// Prompt the user to add/remove projects
input.selectItem(projects, 'Select a project', ['name'], ['key'], true, data.projects).then(function(projects) {
// Update the saved projects
data.projects = projects;
jump('main');
});
}
/**
* Prompt the user to confirm if he want to
* continue using the app or to exit
*
* @param {Object} context
* @param {Function} next
* @param {Function} jump
*/
function configurations(context, next, jump) {
di.input.edit('Configurations and Preferences', config.getContent()).then(function(content) {
console.log(chalk.blue(yaml.dump(yaml.load(content))));
jump('main');
}).catch(next);
}