/
index.js
140 lines (111 loc) · 3.81 KB
/
index.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
'use strict';
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var _ = require('lodash');
var termianlMenu = require('terminal-menu');
var chalk = require('chalk');
var Q = require('q');
var BaseTask = require('./libs/BaseTask');
var TaskManager = require('./libs/TaskManager');
var utils = require('./libs/Utils');
var Shell = require('./libs/Shell');
var logger = utils.logger;
var defaults = {
title: '===============================================',
subtitle: '-----------------------------------------',
helpTxt: 'HELP',
exitTxt: 'EXIT',
width: 100,
x: 3,
y: 2,
version: '',
helpFile: undefined,
preferenceName: undefined,
onFinish: function(id, name) {
logger.success('finish: ', name);
},
onError: function(id, name, err) {
logger.error('failed: ', err);
}
};
var exit = function(code) {
process.exit(code);
};
var createMenu = function(opts) {
var options = opts || {};
options = _.defaults(options, defaults);
var taskMgr = new TaskManager(options);
var emitter = new EventEmitter();
taskMgr.getTaskList().success(function(tasks) {
var menu = termianlMenu({
width: options.width,
x: 3,
y: 2
});
//clean up the terminal
menu.reset();
//display the title
menu.write(chalk.bold(options.title.toUpperCase()) + utils.repeat(' ', options.width - utils.length(options.title, 'utf-8') - options.version.length - 2) + chalk.dim(options.version) + '\n');
//display the subtitle
menu.write(chalk.italic(options.subtitle) + '\n');
//display start separator
menu.write(utils.repeat('-', options.width) + '\n');
tasks.forEach(function(task) {
menu.add(chalk.bold('»') + ' ' + task.name + utils.repeat(' ', options.width - utils.length(task.name) - 3));
});
//display end separator
menu.write(utils.repeat('-', options.width) + '\n');
//display help
if (fs.existsSync(options.helpFile)) {
menu.add(chalk.bold(options.helpTxt) + utils.repeat(' ', options.width - utils.length(options.helpTxt) - 1));
}
//display exit
menu.add(chalk.bold(options.exitTxt) + utils.repeat(' ', options.width - utils.length(options.exitTxt) - 1));
var isTaskRunning;
menu.on('select', function(label, index) {
var name = chalk.stripColor(label).replace(/(^»?\s+)|(\s+$)/g, '');
menu.y = 0;
menu.reset();
menu.close();
isTaskRunning = false;
if (name === options.exitTxt) {
return emitter.emit('exit');
}
if (name === options.helpTxt) {
utils.printFile(options.helpFile);
return;
}
var task = taskMgr.getTaskByIndex(index);
isTaskRunning = true;
var runner = taskMgr.run(task.id);
runner.on('finish', function() {
options.onFinish(task.id, task.name);
process.stdin.end();
});
runner.on('error', function(err) {
options.onError(task.id, task.name, err);
exit(0);
});
});
process.stdin.pipe(menu.createStream()).pipe(process.stdout);
process.stdin.setRawMode(true);
menu.on('close', function() {
process.stdin.setRawMode(false);
if (!isTaskRunning) {
process.stdin.end();
}
});
}).error(function(err) {
logger.error('failed: ', err);
});
return emitter;
};
var TaskRunner = {
Base: BaseTask,
logger: logger,
printFile: utils.printFile,
createMenu: createMenu,
shell: Shell,
Q: Q
};
module.exports = TaskRunner;