-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·233 lines (205 loc) · 7.14 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
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
#!/usr/bin/env node
const { promisify } = require('util')
, arg = require('yargs').argv
, glob = require('tiny-glob')
, fs = require('fs')
, chalk = require('chalk')
, writeAsync = promisify(fs.writeFile)
, stdin = process.stdin
, cleanups = []
, pkg = require('./package.json')
if (stdin.isTTY) console.log(chalk.bold.whiteBright(`pm+ v${pkg.version}`))
if (require.main === module) {
if (!stdin.isTTY) {
let piped = ''
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', data => piped += data)
stdin.on('end', _ => curl2Yaml(piped))
return
}
const isRun = arg.run || arg.r
, isConvert = arg.convert || arg.c
let ok = isConvert || isRun
if (!ok || arg.h) return console.log(`
${chalk.bold.underline.greenBright(`PM+ - arguments required`)}
To convert files:
- pm+ --convert "*.(json|yaml)"
To run tests:
- pm+ --run "file|pattern" [--exclude "pattern"] [-u URL]
* run newman tests on given URL as {{domain}}
* URL defaults to ${chalk.yellowBright(process.env.PMURL || 'http://localhost:3000')}
(use "set PMURL=https://..." or "unset PMURL")
Shorthands:
-c --convert
-r --run
-x --exclude
${chalk.bold.yellowBright(`WARNING:`)} This utility will overwrite files without notice.
`)
if (process.platform === "win32") {
require("readline")
.createInterface({
input: process.stdin,
output: process.stdout
})
.on('SIGINT', _ => process.emit('SIGINT'))
}
process.on('SIGINT', _ => {
// console.log('Cleanup', cleanups.join(' '))
while (cleanups.length > 0) {
const f = cleanups.splice(0, 1)[0]
fs.unlinkSync(f)
}
//graceful shutdown
process.exit()
})
// tpl - nunjucks template for tests functions
// @include(tests from nunjucks template)
// pm+ docs
// pm+ clean < curl
const opts = {
domain: arg.u || process.env.PMURL,
exclude: arg.x || arg.exclude,
isConvert, isRun
}
if (opts.exclude && opts.exclude.startsWith('/') && opts.exclude.endsWith('/')) {
const { trimChar } = require('./lib/curl')
opts.exclude = RegExp(trimChar(opts.exclude,'/'))
}
go(arg.r || arg.run || arg.c || arg.convert, opts)
}
// exclude -> string | regexp
async function go(pattern, { domain, isConvert, isRun, exclude, returnValue }) {
// console.log('start', { pattern, domain, exclude })
const t = +new Date()
const { loadJson, loadYaml } = require('./lib/pmcollection')
const { run } = require('./lib/runner')
// default exclude files begins with !
// if (!exclude && !pattern.startsWith('!')) exclude = /\!.*/
const f = await glob(pattern)
// return console.log(f.join('\n'))
const files = await f.filter(r => {
// console.log(r)
if (typeof exclude === 'string' && r.indexOf(exclude) > -1) return false
else if (exclude instanceof RegExp && exclude.test(r)) return false
return true
}).reduce(async (p, f) => {
p = await p
if (f.endsWith('.json')) {
if (isConvert) await loadJson(f)
p.push(f)
}
else if (f.endsWith('.yaml')) {
const fn = await loadYaml(f, isRun)
if (fn) {
p.push(fn)
cleanups.push(fn)
}
}
return p
}, Promise.resolve([]))
if (isRun) {
const env = {
values: [{
enabled: true,
key: 'domain',
value: domain || 'http://localhost:3000',
type: 'text'
}]
}
return run(env, files).then(errors => {
// cleanup temp files
// if (cleanups.length) console.log('\nCleanups')
while (cleanups.length > 0) {
const f = cleanups.splice(0, 1)[0]
// console.log(' -', f)
fs.unlinkSync(f)
}
if (!returnValue) {
console.log('')
if (files.length) console.log(`Took ${+new Date() - t}ms for ${files.length} files`)
if (errors.total) {
console.error(chalk.yellow(`${errors.total} errors found!`))
errors.files.map(f => {
if (!f.total) return
console.log(' -', f.total ? '❌ ' : '✔️ ', f.name)
// if (f.fails)
let lastSrc = ''
f.fails.map(ff => {
const src = ff.source.name
if (lastSrc !== src) {
lastSrc = src
console.log(' •', chalk.yellowBright(src))
}
console.log(chalk.redBright(' ⓧ ->'), ff.error.test || ff.error.message)
})
})
}
else if (files.length) {
console.info(chalk.greenBright('👍 Yay! All tests passed.'))
}
else console.warn('Nothing to run?')
let tot = 0
errors.files.map(f => {
// console.log(' >>>', f.runs)
if (f.runs && f.runs.tests)
tot += f.runs.tests.total || 0
})
console.log('\n', ' TOTAL:', tot, 'tests processed', '\n')
process.exit(errors ? 1 : 0)
}
return errors
})
}
}
async function convert(pattern) {
return await go(pattern, { isConvert: true, returnValue: true })
}
async function run(pattern, { url, exclude }) {
return await go(pattern, { isRun: true, domain: url, exclude, returnValue: true })
}
async function curl2Yaml(curlCommand) {
const { isJsonContent, makeYaml } = require(`./lib/pmcollection`)
const curl = require('./lib/curl')
const p = curl.parse(curlCommand)
, cleanHeaders = ['accept-language', 'authority', 'origin', 'cookie', 'user-agent']
, cleaned = {}
Object.keys(p.headers).map(k => {
if (k.startsWith('sec-') || cleanHeaders.includes(k.toLowerCase())) return
if (p.headers[k]) cleaned[k] = p.headers[k]
})
p.headers = cleaned
const step = {
[p.method]: `{{domain}}${p.path}`,
headers: p.headers,
prerequest: ''
}
if (p.data) {
if (isJsonContent(p.headers)) {
p.data = JSON.stringify(JSON.parse(p.data), null, 2)
}
step.body = { raw: p.data }
}
// JS trick to append key at bottom of object
step.test = `console.log(JSON.parse(responseBody))\ntests['Test Name'] = true`
const dump = makeYaml({
name: `curl_${+new Date()}`,
steps: [{
[`Give this request a name`]: step
}]
})
console.log(dump)
// const fn = `curl_${+new Date()}.yaml`
// await writeAsync(fn, dump)
// console.log(`👍 ${fn} saved`)
}
module.exports = {
curl2Yaml,
convert,
run
}
// return loadYaml('test.yaml')
// ROADMAP
// - convert CURL to YAML https://github.com/tj/parse-curl.js
// - add sequence tests
// - add yaml test - template