1
+ 'use strict'
2
+
1
3
const { spawn } = require ( 'child_process' )
2
4
const { readFile : readFileAsync } = require ( 'fs' )
3
5
const { promisify } = require ( 'util' )
@@ -16,7 +18,9 @@ const run = (cmd, args, options) => new Promise((resolve, reject) => {
16
18
let stderr = [ ]
17
19
18
20
p . stdout . on ( 'data' , data => {
19
- process . stdout . write ( chalk . magenta ( data . toString ( ) ) )
21
+ // Re-route stdout to stderr, because the only thing the 'local' function should return to stdout
22
+ // is the name of the zip file.
23
+ process . stderr . write ( chalk . magenta ( data . toString ( ) ) )
20
24
stdout . push ( data . toString ( ) )
21
25
} )
22
26
@@ -59,28 +63,40 @@ const existsOnS3 = async (zipFileName, bucket) => {
59
63
. catch ( ( ) => false )
60
64
}
61
65
62
- const local = async ( bucket , sourcefolder , layer ) => createTempDir ( )
66
+ const local = async ( bucket , sourcefolder , layer , scriptInstallPackages , alwaysUpload ) => createTempDir ( )
63
67
. then ( async tempDir => {
64
68
const pkg = path . join ( sourcefolder , 'package.json' )
65
69
const { name, version } = JSON . parse ( await readFile ( pkg ) , 'utf-8' )
66
70
console . error ( `${ chalk . blue ( name ) } ${ chalk . green ( version ) } ` )
67
71
const zipFileName = `${ name . split ( '/' ) [ 1 ] || name } -${ version } .zip`
68
- if ( await existsOnS3 ( zipFileName , bucket ) ) {
72
+ if ( ! alwaysUpload && await existsOnS3 ( zipFileName , bucket ) ) {
69
73
console . error ( chalk . yellow ( `s3://${ bucket } /${ zipFileName } exists` ) )
70
74
return zipFileName
71
75
}
72
76
try {
73
77
if ( layer ) {
74
78
// For a Lambda layer, zip up the entire source tree. Don't install it.
79
+ console . error ( `${ chalk . gray ( 'Packaging as Lambda layer' ) } ` )
75
80
await ncp ( sourcefolder , tempDir )
76
81
} else {
77
82
// For a Lambda function, zip up the file structure we expect
83
+ console . error ( `${ chalk . gray ( 'Packaging as Lambda function' ) } ` )
78
84
await ncp ( pkg , path . join ( tempDir , 'package.json' ) )
79
85
await ncp ( path . join ( sourcefolder , 'package-lock.json' ) , path . join ( tempDir , 'package-lock.json' ) )
80
86
await ncp ( path . join ( sourcefolder , 'dist' ) , path . join ( tempDir , 'dist' ) )
81
87
// Install the package
82
88
await run ( 'npm' , [ 'ci' , '--ignore-scripts' , '--only=prod' ] , { cwd : tempDir } )
83
89
}
90
+ // For each package in this list, run 'npm install' to run certain scripts defined in its package.json,
91
+ // as described in https://docs.npmjs.com/misc/scripts
92
+ // Normally scripts aren't run, due to the --ignore-scripts option in the above "npm ci" command.
93
+ // This gives the option to run install scripts required for some packages to work, such as 'sharp'.
94
+ if ( scriptInstallPackages ) {
95
+ for ( let i = 0 ; i < scriptInstallPackages . length ; i ++ ) {
96
+ console . error ( `${ chalk . gray ( 'Installing with scripts:' ) } ${ chalk . yellow ( scriptInstallPackages [ i ] ) } ` )
97
+ await run ( 'npm' , [ 'install' , '--only=prod' , scriptInstallPackages [ i ] ] , { cwd : tempDir } )
98
+ }
99
+ }
84
100
console . error ( `${ chalk . gray ( 'Uploading to S3:' ) } ${ chalk . yellow ( bucket ) } ${ chalk . gray ( '/' ) } ${ chalk . yellow ( zipFileName ) } ` )
85
101
await publishToS3 ( zipFileName , tempDir , bucket )
86
102
return zipFileName
0 commit comments