Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/swagger-merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (require.main === module) {
/^.+\.(json|yaml|yml)$/gi, null)
.option('-o, --output <*.json|yaml|yml file>', 'output a merged JSON/YAML swagger file, default is `swagger.*`',
/^.+\.(json|yaml|yml)$/gi, null)
.option('-t, --token <string>', 'auth token that gets added to header', null, null)
.option('-c, --compact', 'compact JSON/YAML format string', null, null)
.option('--debug', 'debug mode, such as print error tracks', null, null)
.action((options) => {
Expand All @@ -31,7 +32,8 @@ if (require.main === module) {
merger({
input: options.input || '',
output: options.output || '',
compact: options.compact
compact: options.compact,
token: program.opts().token || ''
}).catch(e => {
if (options.debug) {
console.error(e)
Expand Down
2 changes: 1 addition & 1 deletion lib/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function * merger (param) {
clean(param.tag)
merge(param)

yield downloading(param.tag).then(isCache => {
yield downloading(param.tag, param.token).then(isCache => {
if (isCache) {
param.input = param.output
param.dir = path.dirname(param.output)
Expand Down
15 changes: 11 additions & 4 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,21 @@ function download (tag, url) {
return tmpPath
}

function downloadTasks (tag) {
function downloadTasks (tag, token) {
const result = []
Object.keys(tasks[tag]).forEach((filePath) => {
const url = tasks[tag][filePath]
result.push((callback) => {
const file = fs.createWriteStream(filePath)
let timeout = null
const req = (url.startsWith('https://') ? https : http).get(url, (resp) => {
const options = {
headers: {}
}
if (token !== '') {
options.headers['Authorization'] = `Bearer ${token}`
}

const req = (url.startsWith('https://') ? https : http).get(url, options, (resp) => {
if (timeout) {
clearTimeout(timeout)
}
Expand Down Expand Up @@ -121,14 +128,14 @@ function deleteFolder (dirPath) {
return false
}

function downloading (tag) {
function downloading (tag, token) {
return new Promise(resolve => {
if (!tasks[tag]) {
resolve(fs.existsSync(path.join(tmpDir, tag)))
return
}
async.parallel(
downloadTasks(tag),
downloadTasks(tag, token),
function (_, result) {
result.forEach(r => console.log(r))
resolve(fs.existsSync(path.join(tmpDir, tag)))
Expand Down