Skip to content

Commit

Permalink
feat: support remote types served by https (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: denis-heka <44803580+denis-heka@users.noreply.github.com>
close issue #7
  • Loading branch information
DenisKent authored Jul 18, 2021
1 parent fc0027b commit 4cc8f24
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"build": "tsc --declaration",
"prepublish": "npm run build",
"release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'"
"release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'",
"test": "jest"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/__test__/downloadSpecHelper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
13 changes: 13 additions & 0 deletions src/__test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { downloadFile } from '../index'
import path from 'path'

describe('download file', () => {
test('can download https files', async () => {
const isSuccess = await downloadFile('https://jsonplaceholder.typicode.com/posts/1', path.resolve('src/__test__', 'downloadSpecHelper.json'))
expect(isSuccess).toBe(true);
});
test('can download http files', async () => {
const isSuccess = await downloadFile('http://jsonplaceholder.typicode.com/posts/1', path.resolve('src/__test__', 'downloadSpecHelper.json'))
expect(isSuccess).toBe(true);
});
})
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Compiler } from 'webpack'
import { URL } from 'url'
import fs from 'fs-extra'
import https from 'https'
import http from 'http'
import path from 'path'
import tar from 'tar'

const cwd = process.cwd()

function downloadFile(url: string, targetPath: string) {
export function downloadFile(url: string, targetPath: string) {
const get = url.includes('https://')? https.get : http.get

return new Promise<boolean>((resolve) => {
const target = fs.createWriteStream(targetPath)
http.get(url, (response) => {
get(url, (response) => {
response
.pipe(target)
.on('close', () => {
Expand Down

0 comments on commit 4cc8f24

Please sign in to comment.