Skip to content

Commit

Permalink
Rename download to fetch and visa-versa
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Iwana committed Dec 19, 2020
1 parent 5a7cf4a commit 4f6fc0b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 61 deletions.
64 changes: 47 additions & 17 deletions src/utils/download.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
import axios from 'axios'
import * as stream from 'stream'
import { promisify } from 'util'
import GitlyOptions from '../interfaces/options'

import { GitlyDownloadError } from './error'
import write from './write'
import fetch from './fetch'
import execute from './execute'
import exists from './exists'
import { isOffline } from './offline'
import parse from './parse'
import { getFile, getUrl } from './tar'

const pipeline = promisify(stream.pipeline)
/**
* Download the tar file from the repository
* and store it in a temporary directory
* @param repository The repository to download
* @param options
*
* @example
* ```js
* // ..
* const path = await download('iwatakeshi/git-copy')
* // ...
* ```
*/
export default async function download(
repository: string,
options: GitlyOptions = {}
) {
const info = parse(repository, options)
const file = getFile(info, options)
const url = getUrl(info, options)
const local = async () => exists(file)
const remote = async () => fetch(url, file)
let order = [local, remote]
if ((await isOffline()) || options.cache) {
order = [local]
} else if (options.force || info.type === 'master') {
order = [remote, local]
}

export default async function download(url: string, file: string): Promise<string> {
const response = await axios.get(url, {
responseType: 'stream', validateStatus: status => status >= 200 && status < 500
})

const { statusText: message, status: code } = response
if (code >= 400) throw new GitlyDownloadError(message, code)
else if (code >= 300 && code < 400) {
return download(response.headers.location, file)
} else await pipeline(response.data, await write(file))
return file
try {
const result = await execute(order)
if (typeof result === 'boolean') {
return file
}
return result
} catch (error) {
if (options.throw) {
throw error
}
}
return ''
}
65 changes: 21 additions & 44 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
import GitlyOptions from '../interfaces/options'
import axios from 'axios'
import * as stream from 'stream'
import { promisify } from 'util'

import download from './download'
import execute from './execute'
import exists from './exists'
import { isOffline } from './offline'
import parse from './parse'
import { getFile, getUrl } from './tar'
import { GitlyDownloadError } from './error'
import write from './write'

/**
* Fetch the tar file from the repository
* and store it in a temporary directory
* @param repository The repository to download
* @param options
*
* @example
* ```js
* // ..
* const path = await fetch('iwatakeshi/git-copy')
* // ...
* ```
*/
export default async (repository: string, options: GitlyOptions = {}) => {
const info = parse(repository, options)
const file = getFile(info, options)
const url = getUrl(info, options)
const local = async () => exists(file)
const remote = async () => download(url, file)
let order = [local, remote]
if (await isOffline() || options.cache) {
order = [local]
} else if (options.force || info.type === 'master') {
order = [remote, local]
}
const pipeline = promisify(stream.pipeline)

try {
const result = await execute(order)
if (typeof result === 'boolean') {
return file
}
return result
} catch (error) {
if (options.throw) {
throw error
}
}
return ''
export default async function fetch(
url: string,
file: string
): Promise<string> {
const response = await axios.get(url, {
responseType: 'stream',
validateStatus: (status) => status >= 200 && status < 500,
})

const { statusText: message, status: code } = response
if (code >= 400) throw new GitlyDownloadError(message, code)
else if (code >= 300 && code < 400) {
return fetch(response.headers.location, file)
} else await pipeline(response.data, await write(file))
return file
}

0 comments on commit 4f6fc0b

Please sign in to comment.