Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / init / state_machine.js
1 var util = require('util')
2 var EventEmitter = require('events').EventEmitter
3
4 var StateMachine = function (rli, colors) {
5   var questions
6   var currentQuestion
7   var answers
8   var currentOptions
9   var currentOptionsPointer
10   var currentQuestionId
11   var done
12
13   EventEmitter.call(this)
14
15   var showPrompt = function () {
16     rli.write(colors.ANSWER)
17     rli.prompt()
18   }
19
20   this.onKeypress = function (key) {
21     if (!currentOptions || !key) {
22       return
23     }
24
25     if (key.name === 'tab' || key.name === 'right' || key.name === 'down') {
26       this.suggestNextOption()
27     } else if (key.name === 'left' || key.name === 'up') {
28       currentOptionsPointer = currentOptionsPointer + currentOptions.length - 2
29       this.suggestNextOption()
30     }
31
32     if (!key.ctrl && !key.meta && key.name !== 'enter' && key.name !== 'return') {
33       key.name = 'escape'
34     }
35   }
36
37   this.suggestNextOption = function () {
38     if (!currentOptions) {
39       return
40     }
41
42     currentOptionsPointer = (currentOptionsPointer + 1) % currentOptions.length
43     rli._deleteLineLeft()
44     rli._deleteLineRight()
45     rli.write(currentOptions[currentOptionsPointer])
46   }
47
48   this.kill = function () {
49     currentOptions = null
50     currentQuestionId = null
51     rli.write('\n' + colors.RESET + '\n')
52     rli.close()
53   }
54
55   this.onLine = function (line) {
56     if (currentQuestionId) {
57       rli.write(colors.RESET)
58       line = line.trim().replace(colors.ANSWER, '').replace(colors.RESET, '')
59
60       if (currentOptions) {
61         currentOptionsPointer = currentOptions.indexOf(line)
62         if (currentOptionsPointer === -1) {
63           return
64         }
65       }
66
67       if (line === '') {
68         line = null
69       }
70
71       if (currentQuestion.boolean) {
72         line = (line === 'yes' || line === 'true' || line === 'on')
73       }
74
75       if (line !== null && currentQuestion.validate) {
76         currentQuestion.validate(line)
77       }
78
79       if (currentQuestion.multiple) {
80         answers[currentQuestionId] = answers[currentQuestionId] || []
81         if (line !== null) {
82           answers[currentQuestionId].push(line)
83           showPrompt()
84
85           if (currentOptions) {
86             currentOptions.splice(currentOptionsPointer, 1)
87             currentOptionsPointer = -1
88           }
89         } else {
90           this.nextQuestion()
91         }
92       } else {
93         answers[currentQuestionId] = line
94         this.nextQuestion()
95       }
96     }
97   }
98
99   this.nextQuestion = function () {
100     currentQuestion = questions.shift()
101
102     while (currentQuestion && currentQuestion.condition && !currentQuestion.condition(answers)) {
103       currentQuestion = questions.shift()
104     }
105
106     this.emit('next_question', currentQuestion)
107
108     if (currentQuestion) {
109       currentQuestionId = null
110
111       rli.write('\n' + colors.question(currentQuestion.question) + '\n')
112       rli.write(currentQuestion.hint + '\n')
113       showPrompt()
114
115       currentOptions = currentQuestion.options || null
116       currentOptionsPointer = -1
117       currentQuestionId = currentQuestion.id
118
119       this.suggestNextOption()
120     } else {
121       currentQuestionId = null
122       currentOptions = null
123
124       // end
125       this.kill()
126       done(answers)
127     }
128   }
129
130   this.process = function (_questions, _done) {
131     questions = _questions
132     answers = {}
133     done = _done
134
135     this.nextQuestion()
136   }
137 }
138
139 util.inherits(StateMachine, EventEmitter)
140
141 module.exports = StateMachine