@@ -5,10 +5,22 @@ const globby = require('globby')
5
5
const { contains, isNil, last, split, equals, not, pick } = require ( 'ramda' )
6
6
const { readFile, createReadStream, createWriteStream } = require ( 'fs-extra' )
7
7
const { utils } = require ( '@serverless/core' )
8
+ const fs = require ( 'fs' )
9
+ const util = require ( 'util' )
8
10
9
11
const VALID_FORMATS = [ 'zip' , 'tar' ]
10
12
const isValidFormat = ( format ) => contains ( format , VALID_FORMATS )
11
13
14
+ const augmentWithFileModes = async ( files ) => {
15
+ const promisingStat = util . promisify ( fs . stat )
16
+ const promises = files . map ( ( file ) => (
17
+ promisingStat ( file . input ) . then ( ( stats ) => (
18
+ Object . assign ( file , { mode : ( stats . mode & 0o777 ) } )
19
+ ) )
20
+ ) )
21
+ return Promise . all ( promises )
22
+ }
23
+
12
24
const packDir = async ( inputDirPath , outputFilePath , include = [ ] , exclude = [ ] , prefix ) => {
13
25
const format = last ( split ( '.' , outputFilePath ) )
14
26
@@ -22,13 +34,22 @@ const packDir = async (inputDirPath, outputFilePath, include = [], exclude = [],
22
34
exclude . forEach ( ( excludedItem ) => patterns . push ( `!${ excludedItem } ` ) )
23
35
}
24
36
25
- const files = ( await globby ( patterns , { cwd : inputDirPath , dot : true } ) )
37
+ let files = ( await globby ( patterns , { cwd : inputDirPath , dot : true } ) )
26
38
. sort ( ) // we must sort to ensure correct hash
27
39
. map ( ( file ) => ( {
28
40
input : path . join ( inputDirPath , file ) ,
29
41
output : prefix ? path . join ( prefix , file ) : file
30
42
} ) )
31
43
44
+ if ( ! isNil ( include ) ) {
45
+ files = files . concat ( include . map ( ( file ) => ( {
46
+ input : file ,
47
+ output : path . basename ( file )
48
+ } ) ) )
49
+ }
50
+
51
+ files = await augmentWithFileModes ( files )
52
+
32
53
return new Promise ( ( resolve , reject ) => {
33
54
const output = createWriteStream ( outputFilePath )
34
55
const archive = archiver ( format , {
@@ -40,16 +61,9 @@ const packDir = async (inputDirPath, outputFilePath, include = [], exclude = [],
40
61
41
62
// we must set the date to ensure correct hash
42
63
files . forEach ( ( file ) =>
43
- archive . append ( createReadStream ( file . input ) , { name : file . output , date : new Date ( 0 ) } )
64
+ archive . append ( createReadStream ( file . input ) , { name : file . output , date : new Date ( 0 ) , mode : file . mode } )
44
65
)
45
66
46
- if ( ! isNil ( include ) ) {
47
- include . forEach ( ( file ) => {
48
- const stream = createReadStream ( file )
49
- archive . append ( stream , { name : path . basename ( file ) , date : new Date ( 0 ) } )
50
- } )
51
- }
52
-
53
67
archive . finalize ( )
54
68
} )
55
69
0 commit comments