/
interactive-config.coffee
175 lines (137 loc) · 4.49 KB
/
interactive-config.coffee
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
inquirer = require('inquirer')
fs = require('fs')
yaml = require('js-yaml')
packageData = require('../package.json')
interactiveConfig = {}
interactiveConfig.prompt = (config = {}, callback) ->
questions = []
questions.push {
type: "input",
name: "blueprint",
message: "Location of the API description document"
default: config['blueprint'] || "apiary.apib"
}
questions.push {
type: "input"
name: "server"
message: "Command to start API backend server e.g. (bundle exec rails server)"
default: config['server']
}
questions.push {
type: "input"
name: "endpoint"
message: "URL of tested API endpoint"
default: config['endpoint'] || "http://127.0.0.1:3000"
}
questions.push {
type: "list"
name: "language"
message: "Programming language of hooks"
default: "nodejs"
choices: [
"ruby"
"python"
"nodejs"
"php"
"perl"
"go"
"rust"
]
}
questions.push {
type: "confirm"
name: "apiary"
message: "Do you want to use Apiary test inspector?"
default: true
when: (answers) -> config['reporter'] != 'apiary'
}
questions.push {
type: "input"
name: "apiaryApiKey"
message: "Please enter Apiary API key or leave empty for anonymous reporter"
default: config['custom']?['apiaryApiKey']
when: (answers) ->
(answers['apiary'] == true && ! config['custom']?['apiaryApiKey']?)
}
questions.push {
type: "input"
name: "apiaryApiName"
message: "Please enter Apiary API name"
default: config['custom']?['apiaryApiName']
when: (answers) ->
(answers['apiary'] == true && ! config['custom']?['apiaryApiName']?) &&
(answers['apiary'] == true && answers['apiaryApiKey'] != '')
}
questions.push {
type: "confirm"
name: "travisAdd"
message: "Found Travis CI configuration, do you want to add Dredd to the build?"
default: true
when: () -> fs.existsSync '.travis.yml'
}
questions.push {
type: "confirm"
name: "circleAdd"
message: "Found CircleCI configuration, do you want to add Dredd to the build?"
default: true
when: () -> fs.existsSync 'circle.yml'
}
questions.push {
type: "confirm"
name: "circleCreate"
message: "Dredd is best served with Continuous Integration. Create CircleCI config for Dredd?"
when: (answers) -> (! fs.existsSync('circle.yml') && ! fs.existsSync('.travis.yml'))
}
inquirer.prompt(questions).then(callback)
interactiveConfig.processAnswers = (config, answers, callback) ->
config ?= {}
config['_'] ?= []
config['_'][0] = answers['blueprint']
config['_'][1] = answers['endpoint']
config['server'] = answers['server'] || null
config['language'] = answers['language']
config['reporter'] = "apiary" if answers['apiary'] == true
config['custom'] ?= {}
config['custom']['apiaryApiKey'] = answers['apiaryApiKey'] if answers['apiaryApiKey']?
config['custom']['apiaryApiName'] = answers['apiaryApiName'] if answers['apiaryApiName']?
interactiveConfig.updateCircle() if answers['circleAdd'] or answers['circleCreate']
interactiveConfig.updateTravis() if answers['travisAdd'] == true
callback(config)
return
interactiveConfig.run = (config, callback) ->
interactiveConfig.prompt config, (answers) ->
interactiveConfig.processAnswers config, answers, callback
return
interactiveConfig.updateCircle = () ->
file = "circle.yml"
if fs.existsSync file
config = yaml.safeLoad fs.readFileSync file
else
config = {}
config['dependencies'] ?= {}
config['dependencies']['pre'] ?= []
if config['dependencies']['pre'].indexOf("npm install -g dredd") == -1
config['dependencies']['pre'].push("npm install -g dredd@#{packageData.version}")
config['test'] ?= {}
config['test']['pre'] ?= []
if config['test']['pre'].indexOf("dredd") == -1
config['test']['pre'].push("dredd")
yamlData = yaml.safeDump config
fs.writeFileSync file, yamlData
return
interactiveConfig.updateTravis = () ->
file = ".travis.yml"
if fs.existsSync file
config = yaml.safeLoad fs.readFileSync file
else
config = {}
config['before_install'] ?= []
if config['before_install'].indexOf("npm install -g dredd") == -1
config['before_install'].push("npm install -g dredd@#{packageData.version}")
config['before_script'] ?= []
if config['before_script'].indexOf("dredd") == -1
config['before_script'].push("dredd")
yamlData = yaml.safeDump config
fs.writeFileSync file, yamlData
return
module.exports = interactiveConfig