Skip to content

Commit 716b312

Browse files
committed
refactor: Use download from @serverless/utils
1 parent c265905 commit 716b312

4 files changed

Lines changed: 41 additions & 21 deletions

File tree

lib/plugins/aws/invokeLocal/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stdin = require('get-stdin');
1010
const spawnExt = require('child-process-ext/spawn');
1111
const { spawn } = require('child_process');
1212
const inspect = require('util').inspect;
13-
const download = require('download');
13+
const download = require('@serverless/utils/download');
1414
const { ensureDir } = require('fs-extra');
1515
const cachedir = require('cachedir');
1616
const decompress = require('decompress');

lib/utils/downloadTemplateFromRepo.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const path = require('path');
44
const os = require('os');
55
const URL = require('url');
6-
const download = require('download');
6+
const download = require('@serverless/utils/download');
77
const BbPromise = require('bluebird');
88
const fse = require('fs-extra');
99
const qs = require('querystring');
@@ -81,7 +81,8 @@ function parseGitHubURL(url) {
8181
downloadUrl,
8282
isSubdirectory,
8383
pathToDirectory: getPathDirectory(pathLength + 1, parts),
84-
auth: url.auth || '',
84+
username: url.username || '',
85+
password: url.password || '',
8586
};
8687
}
8788

@@ -111,7 +112,8 @@ function parseBitbucketURL(url) {
111112
downloadUrl,
112113
isSubdirectory,
113114
pathToDirectory: getPathDirectory(pathLength + 1, parts),
114-
auth: '',
115+
username: url.username || '',
116+
password: url.password || '',
115117
};
116118
}
117119

@@ -134,7 +136,8 @@ function parseBitbucketServerURL(url) {
134136
downloadUrl,
135137
isSubdirectory,
136138
pathToDirectory: getPathDirectory(pathLength + 1, parts),
137-
auth: url.auth || '',
139+
username: url.username || '',
140+
password: url.password || '',
138141
};
139142
}
140143

@@ -176,7 +179,8 @@ function parseGitlabURL(url) {
176179
downloadUrl,
177180
isSubdirectory,
178181
pathToDirectory: getPathDirectory(pathLength + 1, parts),
179-
auth: '',
182+
username: url.username || '',
183+
password: url.password || '',
180184
};
181185
}
182186

@@ -197,7 +201,8 @@ function parsePlainGitURL(url) {
197201
branch,
198202
downloadUrl,
199203
isSubdirectory,
200-
auth: url.auth || '',
204+
username: url.username || '',
205+
password: url.password || '',
201206
};
202207
}
203208

@@ -215,6 +220,11 @@ function parseRepoURL(inputUrl) {
215220
}
216221

217222
const url = URL.parse(inputUrl.replace(/\/$/, ''));
223+
if (url.auth) {
224+
const [username, password] = url.auth.split(':');
225+
url.username = username;
226+
url.password = password;
227+
}
218228

219229
// check if url parameter is a valid url
220230
if (!url.host && !url.href.startsWith('git@')) {
@@ -259,7 +269,7 @@ function downloadTemplateFromRepo(inputUrl, templateName, downloadPath, options
259269
let serviceName;
260270
let dirName;
261271
let downloadServicePath;
262-
const { auth } = repoInformation;
272+
const { username, password } = repoInformation;
263273

264274
if (repoInformation.isSubdirectory) {
265275
const folderName = repoInformation.pathToDirectory.split('/').splice(-1)[0];
@@ -296,7 +306,8 @@ function downloadTemplateFromRepo(inputUrl, templateName, downloadPath, options
296306
extract: true,
297307
strip: 1,
298308
mode: '755',
299-
auth,
309+
username,
310+
password,
300311
};
301312
// download service
302313
return download(repoInformation.downloadUrl, downloadServicePath, downloadOptions)

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"dayjs": "^1.10.4",
4141
"decompress": "^4.2.1",
4242
"dotenv": "^9.0.2",
43-
"download": "^8.0.0",
4443
"essentials": "^1.1.1",
4544
"fastest-levenshtein": "^1.0.12",
4645
"filesize": "^6.3.0",

test/unit/lib/utils/downloadTemplateFromRepo.test.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('downloadTemplateFromRepo', () => {
5757

5858
return BbPromise.reject(Error('unknown server type'));
5959
},
60-
'download': downloadStub,
60+
'@serverless/utils/download': downloadStub,
6161
'child-process-ext/spawn': spawnStub,
6262
}
6363
);
@@ -262,7 +262,8 @@ describe('downloadTemplateFromRepo', () => {
262262
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
263263
isSubdirectory: false,
264264
pathToDirectory: '',
265-
auth: '',
265+
username: '',
266+
password: '',
266267
});
267268
}
268269
);
@@ -279,7 +280,8 @@ describe('downloadTemplateFromRepo', () => {
279280
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
280281
isSubdirectory: true,
281282
pathToDirectory: 'assets',
282-
auth: '',
283+
username: '',
284+
password: '',
283285
});
284286
});
285287
});
@@ -295,7 +297,8 @@ describe('downloadTemplateFromRepo', () => {
295297
downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip',
296298
isSubdirectory: false,
297299
pathToDirectory: '',
298-
auth: '',
300+
username: '',
301+
password: '',
299302
});
300303
});
301304
});
@@ -311,7 +314,8 @@ describe('downloadTemplateFromRepo', () => {
311314
downloadUrl: 'https://github.mydomain.com/serverless/serverless/archive/master.zip',
312315
isSubdirectory: true,
313316
pathToDirectory: 'assets',
314-
auth: '',
317+
username: '',
318+
password: '',
315319
});
316320
});
317321
});
@@ -326,7 +330,8 @@ describe('downloadTemplateFromRepo', () => {
326330
branch: 'master',
327331
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
328332
isSubdirectory: false,
329-
auth: 'username:password',
333+
username: 'username',
334+
password: 'password',
330335
pathToDirectory: '',
331336
});
332337
});
@@ -341,7 +346,8 @@ describe('downloadTemplateFromRepo', () => {
341346
downloadUrl: 'https://bitbucket.org/atlassian/localstack/get/master.zip',
342347
isSubdirectory: false,
343348
pathToDirectory: '',
344-
auth: '',
349+
username: '',
350+
password: '',
345351
});
346352
});
347353
});
@@ -357,7 +363,8 @@ describe('downloadTemplateFromRepo', () => {
357363
downloadUrl: 'https://bitbucket.org/atlassian/localstack/get/mvn.zip',
358364
isSubdirectory: true,
359365
pathToDirectory: `localstack${path.sep}dashboard`,
360-
auth: '',
366+
username: '',
367+
password: '',
361368
});
362369
});
363370
});
@@ -374,7 +381,8 @@ describe('downloadTemplateFromRepo', () => {
374381
'https://mybitbucket.server.ltd/rest/api/latest/projects/myproject/repos/myrepo/archive?at=refs%2Fheads%2Fdevelop&format=zip',
375382
isSubdirectory: false,
376383
pathToDirectory: '',
377-
auth: 'user:pass',
384+
username: 'user',
385+
password: 'pass',
378386
});
379387
});
380388
});
@@ -389,7 +397,8 @@ describe('downloadTemplateFromRepo', () => {
389397
'https://gitlab.com/serverless/serverless/-/archive/master/serverless-master.zip',
390398
isSubdirectory: false,
391399
pathToDirectory: '',
392-
auth: '',
400+
username: '',
401+
password: '',
393402
});
394403
});
395404
});
@@ -405,7 +414,8 @@ describe('downloadTemplateFromRepo', () => {
405414
'https://gitlab.com/serverless/serverless/-/archive/dev/serverless-dev.zip',
406415
isSubdirectory: true,
407416
pathToDirectory: 'subdir',
408-
auth: '',
417+
username: '',
418+
password: '',
409419
});
410420
}
411421
);

0 commit comments

Comments
 (0)