1
1
'use strict'
2
2
3
- const pify = require ( 'pify' )
4
-
5
- const fs = process . versions . electron ? require ( 'original-fs' ) : require ( 'fs' )
3
+ const fs = require ( './wrapped-fs' )
6
4
const path = require ( 'path' )
7
5
const minimatch = require ( 'minimatch' )
8
- const mkdirp = pify ( require ( 'mkdirp' ) )
9
6
10
7
const Filesystem = require ( './filesystem' )
11
8
const disk = require ( './disk' )
@@ -29,32 +26,30 @@ function isUnpackedDir (dirPath, pattern, unpackDirs) {
29
26
}
30
27
}
31
28
32
- module . exports . createPackage = function ( src , dest ) {
29
+ module . exports . createPackage = async function ( src , dest ) {
33
30
return module . exports . createPackageWithOptions ( src , dest , { } )
34
31
}
35
32
36
- module . exports . createPackageWithOptions = function ( src , dest , options ) {
33
+ module . exports . createPackageWithOptions = async function ( src , dest , options ) {
37
34
const globOptions = options . globOptions ? options . globOptions : { }
38
35
globOptions . dot = options . dot === undefined ? true : options . dot
39
36
40
- let pattern = src + '/**/*'
41
- if ( options . pattern ) {
42
- pattern = src + options . pattern
43
- }
37
+ const pattern = src + ( options . pattern ? options . pattern : '/**/*' )
44
38
45
- return crawlFilesystem ( pattern , globOptions )
46
- . then ( ( [ filenames , metadata ] ) => module . exports . createPackageFromFiles ( src , dest , filenames , metadata , options ) )
39
+ const [ filenames , metadata ] = await crawlFilesystem ( pattern , globOptions )
40
+ return module . exports . createPackageFromFiles ( src , dest , filenames , metadata , options )
47
41
}
48
42
49
- /*
50
- createPackageFromFiles - Create an asar-archive from a list of filenames
51
- src: Base path. All files are relative to this.
52
- dest: Archive filename (& path).
53
- filenames: Array of filenames relative to src.
54
- metadata: Object with filenames as keys and {type='directory|file|link', stat: fs.stat} as values. (Optional)
55
- options: The options.
43
+ /**
44
+ * Create an ASAR archive from a list of filenames.
45
+ *
46
+ * @param {string } src: Base path. All files are relative to this.
47
+ * @param {string } dest: Archive filename (& path).
48
+ * @param {array } filenames: List of filenames relative to src.
49
+ * @param {object } metadata: Object with filenames as keys and {type='directory|file|link', stat: fs.stat} as values. (Optional)
50
+ * @param {object } options: Options passed to `createPackageWithOptions`.
56
51
*/
57
- module . exports . createPackageFromFiles = function ( src , dest , filenames , metadata , options ) {
52
+ module . exports . createPackageFromFiles = async function ( src , dest , filenames , metadata , options ) {
58
53
if ( typeof metadata === 'undefined' || metadata === null ) { metadata = { } }
59
54
if ( typeof options === 'undefined' || options === null ) { options = { } }
60
55
@@ -68,7 +63,7 @@ module.exports.createPackageFromFiles = function (src, dest, filenames, metadata
68
63
69
64
let filenamesSorted = [ ]
70
65
if ( options . ordering ) {
71
- const orderingFiles = fs . readFileSync ( options . ordering ) . toString ( ) . split ( '\n' ) . map ( function ( line ) {
66
+ const orderingFiles = ( await fs . readFile ( options . ordering ) ) . toString ( ) . split ( '\n' ) . map ( line => {
72
67
if ( line . includes ( ':' ) ) { line = line . split ( ':' ) . pop ( ) }
73
68
line = line . trim ( )
74
69
if ( line . startsWith ( '/' ) ) { line = line . slice ( 1 ) }
@@ -106,17 +101,11 @@ module.exports.createPackageFromFiles = function (src, dest, filenames, metadata
106
101
filenamesSorted = filenames
107
102
}
108
103
109
- const handleFile = function ( filename ) {
110
- let file = metadata [ filename ]
111
- let type
112
- if ( ! file ) {
113
- const stat = fs . lstatSync ( filename )
114
- if ( stat . isDirectory ( ) ) { type = 'directory' }
115
- if ( stat . isFile ( ) ) { type = 'file' }
116
- if ( stat . isSymbolicLink ( ) ) { type = 'link' }
117
- file = { stat, type }
118
- metadata [ filename ] = file
104
+ const handleFile = async function ( filename ) {
105
+ if ( ! metadata [ filename ] ) {
106
+ metadata [ filename ] = await crawlFilesystem . determineFileType ( filename )
119
107
}
108
+ const file = metadata [ filename ]
120
109
121
110
let shouldUnpack
122
111
switch ( file . type ) {
@@ -146,18 +135,18 @@ module.exports.createPackageFromFiles = function (src, dest, filenames, metadata
146
135
return Promise . resolve ( )
147
136
}
148
137
149
- const insertsDone = function ( ) {
150
- return mkdirp ( path . dirname ( dest ) )
151
- . then ( ( ) => disk . writeFilesystem ( dest , filesystem , files , metadata ) )
138
+ const insertsDone = async function ( ) {
139
+ await fs . mkdirp ( path . dirname ( dest ) )
140
+ return disk . writeFilesystem ( dest , filesystem , files , metadata )
152
141
}
153
142
154
143
const names = filenamesSorted . slice ( )
155
144
156
- const next = function ( name ) {
145
+ const next = async function ( name ) {
157
146
if ( ! name ) { return insertsDone ( ) }
158
147
159
- return handleFile ( name )
160
- . then ( ( ) => next ( names . shift ( ) ) )
148
+ await handleFile ( name )
149
+ return next ( names . shift ( ) )
161
150
}
162
151
163
152
return next ( names . shift ( ) )
@@ -185,15 +174,15 @@ module.exports.extractAll = function (archive, dest) {
185
174
const followLinks = process . platform === 'win32'
186
175
187
176
// create destination directory
188
- mkdirp . sync ( dest )
177
+ fs . mkdirpSync ( dest )
189
178
190
179
return filenames . map ( ( filename ) => {
191
180
filename = filename . substr ( 1 ) // get rid of leading slash
192
181
const destFilename = path . join ( dest , filename )
193
182
const file = filesystem . getFile ( filename , followLinks )
194
183
if ( file . files ) {
195
184
// it's a directory, create it and continue with the next entry
196
- mkdirp . sync ( destFilename )
185
+ fs . mkdirpSync ( destFilename )
197
186
} else if ( file . link ) {
198
187
// it's a symlink, create a symlink
199
188
const linkSrcPath = path . dirname ( path . join ( dest , file . link ) )
0 commit comments