11'use strict'
2+
3+ const pify = require ( 'pify' )
4+
25const fs = process . versions . electron ? require ( 'original-fs' ) : require ( 'fs' )
36const path = require ( 'path' )
47const minimatch = require ( 'minimatch' )
5- const mkdirp = require ( 'mkdirp' )
8+ const mkdirp = pify ( require ( 'mkdirp' ) )
69
710const Filesystem = require ( './filesystem' )
811const disk = require ( './disk' )
912const crawlFilesystem = require ( './crawlfs' )
10- const createSnapshot = require ( './snapshot' )
11-
12- // Return whether or not a directory should be excluded from packing due to
13- // "--unpack-dir" option
14- //
15- // @param {string } path - diretory path to check
16- // @param {string } pattern - literal prefix [for backward compatibility] or glob pattern
17- // @param {array } unpackDirs - Array of directory paths previously marked as unpacked
18- //
19- const isUnpackDir = function ( path , pattern , unpackDirs ) {
20- if ( path . indexOf ( pattern ) === 0 || minimatch ( path , pattern ) ) {
21- if ( unpackDirs . indexOf ( path ) === - 1 ) {
22- unpackDirs . push ( path )
13+
14+ /**
15+ * Whether a directory should be excluded from packing due to the `--unpack-dir" option.
16+ *
17+ * @param {string } dirPath - directory path to check
18+ * @param {string } pattern - literal prefix [for backward compatibility] or glob pattern
19+ * @param {array } unpackDirs - Array of directory paths previously marked as unpacked
20+ */
21+ function isUnpackedDir ( dirPath , pattern , unpackDirs ) {
22+ if ( dirPath . startsWith ( pattern ) || minimatch ( dirPath , pattern ) ) {
23+ if ( ! unpackDirs . includes ( dirPath ) ) {
24+ unpackDirs . push ( dirPath )
2325 }
2426 return true
2527 } else {
26- for ( let i = 0 ; i < unpackDirs . length ; i ++ ) {
27- if ( path . indexOf ( unpackDirs [ i ] ) === 0 ) {
28- return true
29- }
30- }
31- return false
28+ return unpackDirs . some ( unpackDir => dirPath . startsWith ( unpackDir ) )
3229 }
3330}
3431
35- module . exports . createPackage = function ( src , dest , callback ) {
36- return module . exports . createPackageWithOptions ( src , dest , { } , callback )
32+ module . exports . createPackage = function ( src , dest ) {
33+ return module . exports . createPackageWithOptions ( src , dest , { } )
3734}
3835
39- module . exports . createPackageWithOptions = function ( src , dest , options , callback ) {
36+ module . exports . createPackageWithOptions = function ( src , dest , options ) {
4037 const globOptions = options . globOptions ? options . globOptions : { }
4138 globOptions . dot = options . dot === undefined ? true : options . dot
4239
@@ -45,10 +42,8 @@ module.exports.createPackageWithOptions = function (src, dest, options, callback
4542 pattern = src + options . pattern
4643 }
4744
48- return crawlFilesystem ( pattern , globOptions , function ( error , filenames , metadata ) {
49- if ( error ) { return callback ( error ) }
50- module . exports . createPackageFromFiles ( src , dest , filenames , metadata , options , callback )
51- } )
45+ return crawlFilesystem ( pattern , globOptions )
46+ . then ( ( [ filenames , metadata ] ) => module . exports . createPackageFromFiles ( src , dest , filenames , metadata , options ) )
5247}
5348
5449/*
@@ -58,9 +53,8 @@ dest: Archive filename (& path).
5853filenames: Array of filenames relative to src.
5954metadata: Object with filenames as keys and {type='directory|file|link', stat: fs.stat} as values. (Optional)
6055options: The options.
61- callback: The callback function. Accepts (err).
6256*/
63- module . exports . createPackageFromFiles = function ( src , dest , filenames , metadata , options , callback ) {
57+ module . exports . createPackageFromFiles = function ( src , dest , filenames , metadata , options ) {
6458 if ( typeof metadata === 'undefined' || metadata === null ) { metadata = { } }
6559 if ( typeof options === 'undefined' || options === null ) { options = { } }
6660
@@ -112,67 +106,58 @@ module.exports.createPackageFromFiles = function (src, dest, filenames, metadata
112106 filenamesSorted = filenames
113107 }
114108
115- const handleFile = function ( filename , done ) {
109+ const handleFile = function ( filename ) {
116110 let file = metadata [ filename ]
117111 let type
118112 if ( ! file ) {
119113 const stat = fs . lstatSync ( filename )
120114 if ( stat . isDirectory ( ) ) { type = 'directory' }
121115 if ( stat . isFile ( ) ) { type = 'file' }
122116 if ( stat . isSymbolicLink ( ) ) { type = 'link' }
123- file = { stat, type}
117+ file = { stat, type }
124118 metadata [ filename ] = file
125119 }
126120
127121 let shouldUnpack
128122 switch ( file . type ) {
129123 case 'directory' :
130- shouldUnpack = options . unpackDir
131- ? isUnpackDir ( path . relative ( src , filename ) , options . unpackDir , unpackDirs )
132- : false
124+ if ( options . unpackDir ) {
125+ shouldUnpack = isUnpackedDir ( path . relative ( src , filename ) , options . unpackDir , unpackDirs )
126+ } else {
127+ shouldUnpack = false
128+ }
133129 filesystem . insertDirectory ( filename , shouldUnpack )
134130 break
135131 case 'file' :
136132 shouldUnpack = false
137133 if ( options . unpack ) {
138- shouldUnpack = minimatch ( filename , options . unpack , { matchBase : true } )
134+ shouldUnpack = minimatch ( filename , options . unpack , { matchBase : true } )
139135 }
140136 if ( ! shouldUnpack && options . unpackDir ) {
141137 const dirName = path . relative ( src , path . dirname ( filename ) )
142- shouldUnpack = isUnpackDir ( dirName , options . unpackDir , unpackDirs )
138+ shouldUnpack = isUnpackedDir ( dirName , options . unpackDir , unpackDirs )
143139 }
144- files . push ( { filename : filename , unpack : shouldUnpack } )
145- filesystem . insertFile ( filename , shouldUnpack , file , options , done )
146- return
140+ files . push ( { filename : filename , unpack : shouldUnpack } )
141+ return filesystem . insertFile ( filename , shouldUnpack , file , options )
147142 case 'link' :
148143 filesystem . insertLink ( filename , file . stat )
149144 break
150145 }
151- return process . nextTick ( done )
146+ return Promise . resolve ( )
152147 }
153148
154149 const insertsDone = function ( ) {
155- return mkdirp ( path . dirname ( dest ) , function ( error ) {
156- if ( error ) { return callback ( error ) }
157- return disk . writeFilesystem ( dest , filesystem , files , metadata , function ( error ) {
158- if ( error ) { return callback ( error ) }
159- if ( options . snapshot ) {
160- return createSnapshot ( src , dest , filenames , metadata , options , callback )
161- } else {
162- return callback ( null )
163- }
164- } )
165- } )
150+ return mkdirp ( path . dirname ( dest ) )
151+ . then ( ( ) => disk . writeFilesystem ( dest , filesystem , files , metadata ) )
166152 }
167153
168154 const names = filenamesSorted . slice ( )
169155
170156 const next = function ( name ) {
171157 if ( ! name ) { return insertsDone ( ) }
172158
173- return handleFile ( name , function ( ) {
174- return next ( names . shift ( ) )
175- } )
159+ return handleFile ( name )
160+ . then ( ( ) => next ( names . shift ( ) ) )
176161 }
177162
178163 return next ( names . shift ( ) )
@@ -203,7 +188,7 @@ module.exports.extractAll = function (archive, dest) {
203188 mkdirp . sync ( dest )
204189
205190 return filenames . map ( ( filename ) => {
206- filename = filename . substr ( 1 ) // get rid of leading slash
191+ filename = filename . substr ( 1 ) // get rid of leading slash
207192 const destFilename = path . join ( dest , filename )
208193 const file = filesystem . getFile ( filename , followLinks )
209194 if ( file . files ) {
0 commit comments