Skip to content

Commit

Permalink
refactor: Use download from @serverless/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed May 24, 2021
1 parent c265905 commit 716b312
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/invokeLocal/index.js
Expand Up @@ -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');
Expand Down
27 changes: 19 additions & 8 deletions lib/utils/downloadTemplateFromRepo.js
Expand Up @@ -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');
Expand Down Expand Up @@ -81,7 +81,8 @@ function parseGitHubURL(url) {
downloadUrl,
isSubdirectory,
pathToDirectory: getPathDirectory(pathLength + 1, parts),
auth: url.auth || '',
username: url.username || '',
password: url.password || '',
};
}

Expand Down Expand Up @@ -111,7 +112,8 @@ function parseBitbucketURL(url) {
downloadUrl,
isSubdirectory,
pathToDirectory: getPathDirectory(pathLength + 1, parts),
auth: '',
username: url.username || '',
password: url.password || '',
};
}

Expand All @@ -134,7 +136,8 @@ function parseBitbucketServerURL(url) {
downloadUrl,
isSubdirectory,
pathToDirectory: getPathDirectory(pathLength + 1, parts),
auth: url.auth || '',
username: url.username || '',
password: url.password || '',
};
}

Expand Down Expand Up @@ -176,7 +179,8 @@ function parseGitlabURL(url) {
downloadUrl,
isSubdirectory,
pathToDirectory: getPathDirectory(pathLength + 1, parts),
auth: '',
username: url.username || '',
password: url.password || '',
};
}

Expand All @@ -197,7 +201,8 @@ function parsePlainGitURL(url) {
branch,
downloadUrl,
isSubdirectory,
auth: url.auth || '',
username: url.username || '',
password: url.password || '',
};
}

Expand All @@ -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@')) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
32 changes: 21 additions & 11 deletions test/unit/lib/utils/downloadTemplateFromRepo.test.js
Expand Up @@ -57,7 +57,7 @@ describe('downloadTemplateFromRepo', () => {

return BbPromise.reject(Error('unknown server type'));
},
'download': downloadStub,
'@serverless/utils/download': downloadStub,
'child-process-ext/spawn': spawnStub,
}
);
Expand Down Expand Up @@ -262,7 +262,8 @@ describe('downloadTemplateFromRepo', () => {
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
isSubdirectory: false,
pathToDirectory: '',
auth: '',
username: '',
password: '',
});
}
);
Expand All @@ -279,7 +280,8 @@ describe('downloadTemplateFromRepo', () => {
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
isSubdirectory: true,
pathToDirectory: 'assets',
auth: '',
username: '',
password: '',
});
});
});
Expand All @@ -295,7 +297,8 @@ describe('downloadTemplateFromRepo', () => {
downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip',
isSubdirectory: false,
pathToDirectory: '',
auth: '',
username: '',
password: '',
});
});
});
Expand All @@ -311,7 +314,8 @@ describe('downloadTemplateFromRepo', () => {
downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip',
isSubdirectory: true,
pathToDirectory: 'assets',
auth: '',
username: '',
password: '',
});
});
});
Expand All @@ -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: '',
});
});
Expand All @@ -341,7 +346,8 @@ describe('downloadTemplateFromRepo', () => {
downloadUrl: 'https://bitbucket.org/atlassian/localstack/get/master.zip',
isSubdirectory: false,
pathToDirectory: '',
auth: '',
username: '',
password: '',
});
});
});
Expand All @@ -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: '',
});
});
});
Expand All @@ -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',
});
});
});
Expand All @@ -389,7 +397,8 @@ describe('downloadTemplateFromRepo', () => {
'https://gitlab.com/serverless/serverless/-/archive/master/serverless-master.zip',
isSubdirectory: false,
pathToDirectory: '',
auth: '',
username: '',
password: '',
});
});
});
Expand All @@ -405,7 +414,8 @@ describe('downloadTemplateFromRepo', () => {
'https://gitlab.com/serverless/serverless/-/archive/dev/serverless-dev.zip',
isSubdirectory: true,
pathToDirectory: 'subdir',
auth: '',
username: '',
password: '',
});
}
);
Expand Down

0 comments on commit 716b312

Please sign in to comment.