-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
executable file
·86 lines (74 loc) · 2.55 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
#!/usr/bin/env node
require('dotenv').config();
const converter = require('openapi-to-postmanv2')
const collection = require('./lib/collection')
process.env.SUPPRESS_NO_CONFIG_WARNING = 'y';
var configModule = require('config')
config = configModule.util.loadFileConfigs(__dirname + '/config/')
const fetch = require('./lib/fetch')
const merger=require('./lib/merger')
const program = require('commander')
program.version('1.0.0')
.option('-s --service <service>', 'which service to convert')
.option('-r --replace [repliaces]', 'comma split api name which will replace not merge')
.parse(process.argv)
var serviceConfig = config[program.service]
var url = serviceConfig.url
var collectionName = serviceConfig.collection_name
//run update
update().catch(err => {
console.error("run failed," + err)
})
//get swagger json
function getSwaggerJson(url) {
return fetch({
url: url,
methods: 'get'
}).then(response => {
return response.data
}).catch(err => {
console.log('get swagger json failed: ' + err.message)
process.exit(-1);
})
}
async function update() {
var swaggerJson = await getSwaggerJson(url)
//add postman collection used info
swaggerJson['info'] = {
'title': collectionName,
'description': collectionName + ' api',
'version': '1.0.0',
'_postman_id': '807bb824-b333-4b59-a6ef-a8d46d3b95bf'
}
var converterInputData = {
'type': 'json',
'data': swaggerJson
}
//use postman tool convert to postman collection
converter.convert(converterInputData, { 'folderStrategy': 'Tags' }, async (_a, res) => {
if (res.result === false) {
console.log('convert failed')
console.log(res.reason)
return
}
var convertedJson = res.output[0].data
var id = await collection.getCollectionId(collectionName)
if (id === null) {
return
}
var collectionJson = {
'collection': {
'info': {
'name': collectionName,
'description': collectionName + ' api',
'_postman_id': id,
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": convertedJson.item
}
}
var savedCollection = await collection.getCollectionDetail(id)
var mergedCollection=merger.merge(savedCollection,collectionJson)
collection.updateCollection(id, mergedCollection)
})
}