MCPcopy
hub / github.com/serverless/serverless / downloadTemplate

Function downloadTemplate

packages/sf-core/src/utils/https/index.js:333–395  ·  view source on GitHub ↗
(inputUrl, newTemplateName)

Source from the content-addressed store, hash-verified

331 * @returns A Promise
332 */
333export const downloadTemplate = async (inputUrl, newTemplateName) => {
334 // Parse the repository URL
335 const repoInformation = await parseRepoURL(inputUrl)
336 const { username, password } = repoInformation
337
338 let subdirectory
339 if (repoInformation.isSubdirectory) {
340 subdirectory = repoInformation.pathToDirectory.split('/').splice(-1)[0]
341 }
342
343 const existingServiceName = subdirectory || repoInformation.repo
344 const tmpDownloadPath = path.join(os.tmpdir(), repoInformation.repo)
345 const newServicePath = path.join(
346 process.cwd(),
347 newTemplateName || existingServiceName,
348 )
349
350 // Check if a folder with the same name already exists
351 if (await dirExists(newServicePath)) {
352 const errorMessage = `A folder named "${newServicePath}" already exists.`
353 throw new Error(errorMessage)
354 }
355
356 // Download repo contents
357 const downloadOptions = {
358 timeout: 30000,
359 extract: true,
360 strip: 1,
361 username,
362 password,
363 }
364 let downloadedDataPath = await downloadDataFromUri(
365 repoInformation.downloadUrl,
366 tmpDownloadPath,
367 downloadOptions,
368 )
369
370 // Check if the downloaded file is a zip file
371 if (downloadedDataPath.endsWith('.zip')) {
372 const unzipPath = path.join(
373 tmpDownloadPath,
374 path.basename(repoInformation.downloadUrl, '.zip'),
375 )
376 // Get the extracted contents and point to the path variable to it
377 downloadedDataPath = await unzipFile(downloadedDataPath, unzipPath)
378 }
379
380 const directory = path.join(
381 downloadedDataPath,
382 repoInformation.pathToDirectory,
383 )
384 await copyDirContents(directory, newServicePath)
385
386 // Remove the temporary directory
387 await removeFileOrDirectory(tmpDownloadPath)
388
389 // Remove serverless.template.yml if it exists
390 await removeFileOrDirectory(

Callers 2

routeNewFunction · 0.90
download.test.jsFile · 0.85

Calls 6

dirExistsFunction · 0.90
unzipFileFunction · 0.90
copyDirContentsFunction · 0.90
removeFileOrDirectoryFunction · 0.90
parseRepoURLFunction · 0.85
downloadDataFromUriFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…