From 716b31216e4873bbb986c5a2a54fda708a591cd1 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Mon, 24 May 2021 11:28:51 +0200 Subject: [PATCH] refactor: Use `download` from `@serverless/utils` --- lib/plugins/aws/invokeLocal/index.js | 2 +- lib/utils/downloadTemplateFromRepo.js | 27 +++++++++++----- package.json | 1 - .../utils/downloadTemplateFromRepo.test.js | 32 ++++++++++++------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/plugins/aws/invokeLocal/index.js b/lib/plugins/aws/invokeLocal/index.js index 6e73fc60f6e..bd84c8eb09b 100644 --- a/lib/plugins/aws/invokeLocal/index.js +++ b/lib/plugins/aws/invokeLocal/index.js @@ -10,7 +10,7 @@ const stdin = require('get-stdin'); const spawnExt = require('child-process-ext/spawn'); const { spawn } = require('child_process'); const inspect = require('util').inspect; -const download = require('download'); +const download = require('@serverless/utils/download'); const { ensureDir } = require('fs-extra'); const cachedir = require('cachedir'); const decompress = require('decompress'); diff --git a/lib/utils/downloadTemplateFromRepo.js b/lib/utils/downloadTemplateFromRepo.js index da061a496e7..c174aef3af7 100644 --- a/lib/utils/downloadTemplateFromRepo.js +++ b/lib/utils/downloadTemplateFromRepo.js @@ -3,7 +3,7 @@ const path = require('path'); const os = require('os'); const URL = require('url'); -const download = require('download'); +const download = require('@serverless/utils/download'); const BbPromise = require('bluebird'); const fse = require('fs-extra'); const qs = require('querystring'); @@ -81,7 +81,8 @@ function parseGitHubURL(url) { downloadUrl, isSubdirectory, pathToDirectory: getPathDirectory(pathLength + 1, parts), - auth: url.auth || '', + username: url.username || '', + password: url.password || '', }; } @@ -111,7 +112,8 @@ function parseBitbucketURL(url) { downloadUrl, isSubdirectory, pathToDirectory: getPathDirectory(pathLength + 1, parts), - auth: '', + username: url.username || '', + password: url.password || '', }; } @@ -134,7 +136,8 @@ function parseBitbucketServerURL(url) { downloadUrl, isSubdirectory, pathToDirectory: getPathDirectory(pathLength + 1, parts), - auth: url.auth || '', + username: url.username || '', + password: url.password || '', }; } @@ -176,7 +179,8 @@ function parseGitlabURL(url) { downloadUrl, isSubdirectory, pathToDirectory: getPathDirectory(pathLength + 1, parts), - auth: '', + username: url.username || '', + password: url.password || '', }; } @@ -197,7 +201,8 @@ function parsePlainGitURL(url) { branch, downloadUrl, isSubdirectory, - auth: url.auth || '', + username: url.username || '', + password: url.password || '', }; } @@ -215,6 +220,11 @@ function parseRepoURL(inputUrl) { } const url = URL.parse(inputUrl.replace(/\/$/, '')); + if (url.auth) { + const [username, password] = url.auth.split(':'); + url.username = username; + url.password = password; + } // check if url parameter is a valid url if (!url.host && !url.href.startsWith('git@')) { @@ -259,7 +269,7 @@ function downloadTemplateFromRepo(inputUrl, templateName, downloadPath, options let serviceName; let dirName; let downloadServicePath; - const { auth } = repoInformation; + const { username, password } = repoInformation; if (repoInformation.isSubdirectory) { const folderName = repoInformation.pathToDirectory.split('/').splice(-1)[0]; @@ -296,7 +306,8 @@ function downloadTemplateFromRepo(inputUrl, templateName, downloadPath, options extract: true, strip: 1, mode: '755', - auth, + username, + password, }; // download service return download(repoInformation.downloadUrl, downloadServicePath, downloadOptions) diff --git a/package.json b/package.json index 98d14bb6d8b..cdca3fdca76 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "dayjs": "^1.10.4", "decompress": "^4.2.1", "dotenv": "^9.0.2", - "download": "^8.0.0", "essentials": "^1.1.1", "fastest-levenshtein": "^1.0.12", "filesize": "^6.3.0", diff --git a/test/unit/lib/utils/downloadTemplateFromRepo.test.js b/test/unit/lib/utils/downloadTemplateFromRepo.test.js index 80bd4b88733..007cbd1a146 100644 --- a/test/unit/lib/utils/downloadTemplateFromRepo.test.js +++ b/test/unit/lib/utils/downloadTemplateFromRepo.test.js @@ -57,7 +57,7 @@ describe('downloadTemplateFromRepo', () => { return BbPromise.reject(Error('unknown server type')); }, - 'download': downloadStub, + '@serverless/utils/download': downloadStub, 'child-process-ext/spawn': spawnStub, } ); @@ -262,7 +262,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip', isSubdirectory: false, pathToDirectory: '', - auth: '', + username: '', + password: '', }); } ); @@ -279,7 +280,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip', isSubdirectory: true, pathToDirectory: 'assets', - auth: '', + username: '', + password: '', }); }); }); @@ -295,7 +297,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip', isSubdirectory: false, pathToDirectory: '', - auth: '', + username: '', + password: '', }); }); }); @@ -311,7 +314,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip', isSubdirectory: true, pathToDirectory: 'assets', - auth: '', + username: '', + password: '', }); }); }); @@ -326,7 +330,8 @@ describe('downloadTemplateFromRepo', () => { branch: 'master', downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip', isSubdirectory: false, - auth: 'username:password', + username: 'username', + password: 'password', pathToDirectory: '', }); }); @@ -341,7 +346,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://bitbucket.org/atlassian/localstack/get/master.zip', isSubdirectory: false, pathToDirectory: '', - auth: '', + username: '', + password: '', }); }); }); @@ -357,7 +363,8 @@ describe('downloadTemplateFromRepo', () => { downloadUrl: 'https://bitbucket.org/atlassian/localstack/get/mvn.zip', isSubdirectory: true, pathToDirectory: `localstack${path.sep}dashboard`, - auth: '', + username: '', + password: '', }); }); }); @@ -374,7 +381,8 @@ describe('downloadTemplateFromRepo', () => { 'https://mybitbucket.server.ltd/rest/api/latest/projects/myproject/repos/myrepo/archive?at=refs%2Fheads%2Fdevelop&format=zip', isSubdirectory: false, pathToDirectory: '', - auth: 'user:pass', + username: 'user', + password: 'pass', }); }); }); @@ -389,7 +397,8 @@ describe('downloadTemplateFromRepo', () => { 'https://gitlab.com/serverless/serverless/-/archive/master/serverless-master.zip', isSubdirectory: false, pathToDirectory: '', - auth: '', + username: '', + password: '', }); }); }); @@ -405,7 +414,8 @@ describe('downloadTemplateFromRepo', () => { 'https://gitlab.com/serverless/serverless/-/archive/dev/serverless-dev.zip', isSubdirectory: true, pathToDirectory: 'subdir', - auth: '', + username: '', + password: '', }); } );