MCPcopy
hub / github.com/serverless/serverless / pack

Method pack

packages/serverless/lib/plugins/aws/dev/index.js:474–553  ·  view source on GitHub ↗

* Build, bundle and package the dev mode shim responsible for routing events to the local machine.. * * The method performs the following operations: * - Generates the path for the zip file based on the service's name and directory. * - Bundles and minifies the "shim.js" file using esbui

()

Source from the content-addressed store, hash-verified

472 * creating the zip file fails, with a specific error code for easier debugging.
473 */
474 async pack() {
475 // Save the shim package in .serverless just like the service package
476 const zipFilePath = path.join(
477 this.serverless.serviceDir,
478 '.serverless',
479 `${this.serverless.service.service}.zip`,
480 )
481
482 logger.debug(`Packing shim file into ${zipFilePath}`)
483
484 let shimFileContents
485 try {
486 /**
487 * The shim.min.js file is built when the binary is built
488 */
489 shimFileContents = await fs.promises.readFile(
490 path.join(__dirname, 'shim.min.js'),
491 )
492 } catch (e) {
493 console.error(e)
494 throw new ServerlessError(
495 'Failed to build dev mode shim',
496 'BUILD_SHIM_FAILED',
497 { stack: false },
498 )
499 }
500
501 try {
502 const zip = archiver.create('zip')
503
504 // Create the directory structure if it doesn't exist
505 fs.mkdirSync(path.dirname(zipFilePath), { recursive: true })
506 const output = fs.createWriteStream(zipFilePath)
507
508 return new Promise(async (resolve, reject) => {
509 output.on('close', () => {
510 return resolve(zipFilePath)
511 })
512
513 output.on('error', (err) => {
514 logger.debug('Output file error')
515 return reject(err)
516 })
517
518 zip.on('error', (err) => {
519 logger.debug('Zipper error')
520 return reject(err)
521 })
522
523 output.on('open', async () => {
524 zip.pipe(output)
525
526 // Add the bundled shim file contents to the zip file
527 zip.append(shimFileContents, {
528 name: 'index.js', // This is the name expected by the handler. If you change this, you must change the handlers config below.
529 date: new Date(0), // necessary to get the same hash when zipping the same content
530 })
531

Callers 1

constructorMethod · 0.95

Calls 6

debugMethod · 0.80
readFileMethod · 0.80
errorMethod · 0.80
getAllFunctionsMethod · 0.80
createMethod · 0.45
getFunctionMethod · 0.45

Tested by

no test coverage detected