1- const promisifyAll = require ( 'util.promisify-all' )
21const promisify = require ( 'util.promisify' )
32const pump = promisify ( require ( 'pump' ) )
4- const fs = promisifyAll ( require ( 'fs' ) )
53const fromArray = require ( 'from2-array' )
4+ const zipIt = require ( '@netlify/zip-it-and-ship-it' )
5+ const path = require ( 'path' )
66
7- const { hasherCtor, manifestCollectorCtor, fnStatCtor } = require ( './hasher-segments' )
7+ const { hasherCtor, manifestCollectorCtor } = require ( './hasher-segments' )
88
99module . exports = hashFns
1010async function hashFns ( dir , opts ) {
@@ -20,21 +20,32 @@ async function hashFns(dir, opts) {
2020 )
2121 // early out if the functions dir is omitted
2222 if ( ! dir ) return { functions : { } , shaMap : { } }
23- if ( ! opts . filter ) throw new Error ( 'Missing required filter function' )
2423 if ( ! opts . tmpDir ) throw new Error ( 'Missing tmpDir directory for zipping files' )
2524
26- const fileList = await fs . readdir ( dir ) . then ( files => files . filter ( opts . filter ) )
27- const fileStream = fromArray . obj ( fileList )
25+ const functionZips = await zipIt . zipFunctions ( dir , opts . tmpDir )
26+
27+ const fileObjs = functionZips . map ( ( { path : functionPath , runtime } ) => ( {
28+ filepath : functionPath ,
29+ root : opts . tmpDir ,
30+ relname : path . relative ( opts . tmpDir , functionPath ) ,
31+ basename : path . basename ( functionPath ) ,
32+ extname : path . extname ( functionPath ) ,
33+ type : 'file' ,
34+ assetType : 'function' ,
35+ normalizedPath : path . basename ( functionPath , path . extname ( functionPath ) ) ,
36+ runtime
37+ } ) )
38+
39+ const functionStream = fromArray . obj ( fileObjs )
2840
29- const fnStat = fnStatCtor ( { root : dir , concurrentStat : opts . concurrentHash , tmpDir : opts . tmpDir } )
3041 const hasher = hasherCtor ( opts )
3142
3243 // Written to by manifestCollector
3344 const functions = { } // normalizedPath: hash (wanted by deploy API)
3445 const fnShaMap = { } //hash: [fileObj, fileObj, fileObj]
3546 const manifestCollector = manifestCollectorCtor ( functions , fnShaMap , opts )
3647
37- await pump ( fileStream , fnStat , hasher , manifestCollector )
48+ await pump ( functionStream , hasher , manifestCollector )
3849
3950 return { functions, fnShaMap }
4051}
0 commit comments