@@ -22,177 +22,176 @@ const Transform = streamLib.Transform;
22
22
23
23
const FILE_PATH_ERROR = 'Expected a file path to be compressed as an archive' ;
24
24
const TAR_PATH_ERROR = 'Expected a file path where an archive file will be created' ;
25
- const TAR_TRANSFORM_ERROR = '`tarTransform` option must be a transform stream ' +
26
- 'that modifies the tar archive before writing' ;
25
+ const TAR_TRANSFORM_ERROR = '`tarTransform` option must be a transform stream that modifies the tar archive before writing' ;
27
26
const MAP_STREAM_ERROR = 'The function passed to `mapStream` option must return a stream' ;
28
27
29
28
const unsupportedOptions = [
30
- 'entries' ,
31
- 'filter' ,
32
- 'ignore' ,
33
- 'strip'
29
+ 'entries' ,
30
+ 'filter' ,
31
+ 'ignore' ,
32
+ 'strip'
34
33
] ;
35
34
36
35
module . exports = function fileToTar ( filePath , tarPath , options ) {
37
- return new Observable ( observer => {
38
- if ( typeof filePath !== 'string' ) {
39
- throw new TypeError ( `${ FILE_PATH_ERROR } , but got a non-string value ${ inspectWithKind ( filePath ) } .` ) ;
40
- }
41
-
42
- if ( filePath . length === 0 ) {
43
- throw new Error ( `${ FILE_PATH_ERROR } , but got '' (empty string).` ) ;
44
- }
45
-
46
- if ( typeof tarPath !== 'string' ) {
47
- throw new TypeError ( `${ TAR_PATH_ERROR } , but got a non-string value ${ inspectWithKind ( tarPath ) } .` ) ;
48
- }
49
-
50
- if ( tarPath . length === 0 ) {
51
- throw new Error ( `${ TAR_PATH_ERROR } , but got '' (empty string).` ) ;
52
- }
53
-
54
- const absoluteFilePath = resolve ( filePath ) ;
55
- const absoluteTarPath = resolve ( tarPath ) ;
56
- const dirPath = dirname ( absoluteFilePath ) ;
57
-
58
- if ( absoluteFilePath === absoluteTarPath ) {
59
- throw new Error ( `Source file path must be different from the archive path. Both were specified to ${
60
- absoluteFilePath
61
- } .`) ;
62
- }
63
-
64
- if ( options !== undefined ) {
65
- if ( ! isPlainObj ( options ) ) {
66
- throw new TypeError ( `Expected a plain object to set file-to-tar options, but got ${
67
- inspectWithKind ( options )
68
- } .`) ;
69
- }
70
- } else {
71
- options = { } ;
72
- }
73
-
74
- for ( const optionName of unsupportedOptions ) {
75
- const val = options [ optionName ] ;
76
-
77
- if ( val !== undefined ) {
78
- throw new Error ( `file-to-tar doesn't support \`${ optionName } \` option, but ${
79
- inspectWithKind ( val )
80
- } was provided.`) ;
81
- }
82
- }
83
-
84
- if ( options . tarTransform !== undefined ) {
85
- if ( ! isStream ( options . tarTransform ) ) {
86
- throw new TypeError ( `${ TAR_TRANSFORM_ERROR } , but got a non-stream value ${
87
- inspectWithKind ( options . tarTransform )
88
- } .`) ;
89
- }
90
-
91
- if ( ! isStream . transform ( options . tarTransform ) ) {
92
- throw new TypeError ( `${ TAR_TRANSFORM_ERROR } , but got a ${
93
- [ 'duplex' , 'writable' , 'readable' ] . find ( type => isStream [ type ] ( options . tarTransform ) )
94
- } stream instead.`) ;
95
- }
96
- }
97
-
98
- let cancel ;
99
-
100
- lstat ( absoluteFilePath , ( lstatErr , stat ) => {
101
- if ( lstatErr ) {
102
- observer . error ( lstatErr ) ;
103
- return ;
104
- }
105
-
106
- if ( ! stat . isFile ( ) ) {
107
- observer . error ( new Error ( `Expected ${ absoluteFilePath } to be a file path, but it was a ${
108
- stat . isDirectory ( ) ? 'directory' : 'symbolic link'
109
- } .`) ) ;
110
-
111
- return ;
112
- }
113
-
114
- let firstWriteFailed = false ;
115
-
116
- const firstWriteStream = fs . createWriteStream ( tarPath , options ) . on ( 'error' , err => {
117
- if ( err . code === 'EISDIR' ) {
118
- err . message = `Tried to write an archive file to ${ absoluteTarPath } , but a directory already exists there.` ;
119
- observer . error ( err ) ;
120
-
121
- return ;
122
- }
123
-
124
- firstWriteFailed = true ;
125
- } ) ;
126
-
127
- mkdirp ( dirname ( tarPath ) , Object . assign ( { fs} , options ) , mkdirpErr => {
128
- if ( mkdirpErr ) {
129
- observer . error ( mkdirpErr ) ;
130
- return ;
131
- }
132
-
133
- const packStream = pack ( dirPath , Object . assign ( { fs} , options , {
134
- entries : [ basename ( filePath ) ] ,
135
- map ( header ) {
136
- if ( options . map ) {
137
- header = options . map ( header ) ;
138
- }
139
-
140
- return header ;
141
- } ,
142
- mapStream ( fileStream , header ) {
143
- const newStream = options . mapStream ? options . mapStream ( fileStream , header ) : fileStream ;
144
-
145
- if ( ! isStream . readable ( newStream ) ) {
146
- packStream . emit ( 'error' , new TypeError ( `${ MAP_STREAM_ERROR } ${
147
- isStream ( newStream ) ?
148
- ' that is readable, but returned a non-readable stream' :
149
- `, but returned a non-stream value ${ inspectWithKind ( newStream ) } `
150
- } .`) ) ;
151
-
152
- return new PassThrough ( ) ;
153
- }
154
-
155
- let bytes = 0 ;
156
-
157
- observer . next ( { bytes, header} ) ;
158
-
159
- return newStream . pipe ( new Transform ( {
160
- transform ( chunk , encoding , cb ) {
161
- bytes += chunk . length ;
162
-
163
- observer . next ( { bytes, header} ) ;
164
- cb ( null , chunk ) ;
165
- }
166
- } ) ) ;
167
- }
168
- } ) ) ;
169
-
170
- function getDest ( ) {
171
- return firstWriteFailed ? fs . createWriteStream ( tarPath , options ) : firstWriteStream ;
172
- }
173
-
174
- cancel = cancelablePump ( options . tarTransform ? [
175
- packStream ,
176
- options . tarTransform ,
177
- getDest ( )
178
- ] : [
179
- packStream ,
180
- getDest ( )
181
- ] , err => {
182
- if ( err ) {
183
- observer . error ( err ) ;
184
- return ;
185
- }
186
-
187
- observer . complete ( ) ;
188
- } ) ;
189
- } ) ;
190
- } ) ;
191
-
192
- return function cancelCompression ( ) {
193
- if ( cancel ) {
194
- cancel ( ) ;
195
- }
196
- } ;
197
- } ) ;
36
+ return new Observable ( observer => {
37
+ if ( typeof filePath !== 'string' ) {
38
+ throw new TypeError ( `${ FILE_PATH_ERROR } , but got a non-string value ${ inspectWithKind ( filePath ) } .` ) ;
39
+ }
40
+
41
+ if ( filePath . length === 0 ) {
42
+ throw new Error ( `${ FILE_PATH_ERROR } , but got '' (empty string).` ) ;
43
+ }
44
+
45
+ if ( typeof tarPath !== 'string' ) {
46
+ throw new TypeError ( `${ TAR_PATH_ERROR } , but got a non-string value ${ inspectWithKind ( tarPath ) } .` ) ;
47
+ }
48
+
49
+ if ( tarPath . length === 0 ) {
50
+ throw new Error ( `${ TAR_PATH_ERROR } , but got '' (empty string).` ) ;
51
+ }
52
+
53
+ const absoluteFilePath = resolve ( filePath ) ;
54
+ const absoluteTarPath = resolve ( tarPath ) ;
55
+ const dirPath = dirname ( absoluteFilePath ) ;
56
+
57
+ if ( absoluteFilePath === absoluteTarPath ) {
58
+ throw new Error ( `Source file path must be different from the archive path. Both were specified to ${
59
+ absoluteFilePath
60
+ } .`) ;
61
+ }
62
+
63
+ if ( options !== undefined ) {
64
+ if ( ! isPlainObj ( options ) ) {
65
+ throw new TypeError ( `Expected a plain object to set file-to-tar options, but got ${
66
+ inspectWithKind ( options )
67
+ } .`) ;
68
+ }
69
+ } else {
70
+ options = { } ;
71
+ }
72
+
73
+ for ( const optionName of unsupportedOptions ) {
74
+ const val = options [ optionName ] ;
75
+
76
+ if ( val !== undefined ) {
77
+ throw new Error ( `file-to-tar doesn't support \`${ optionName } \` option, but ${
78
+ inspectWithKind ( val )
79
+ } was provided.`) ;
80
+ }
81
+ }
82
+
83
+ if ( options . tarTransform !== undefined ) {
84
+ if ( ! isStream ( options . tarTransform ) ) {
85
+ throw new TypeError ( `${ TAR_TRANSFORM_ERROR } , but got a non-stream value ${
86
+ inspectWithKind ( options . tarTransform )
87
+ } .`) ;
88
+ }
89
+
90
+ if ( ! isStream . transform ( options . tarTransform ) ) {
91
+ throw new TypeError ( `${ TAR_TRANSFORM_ERROR } , but got a ${
92
+ [ 'duplex' , 'writable' , 'readable' ] . find ( type => isStream [ type ] ( options . tarTransform ) )
93
+ } stream instead.`) ;
94
+ }
95
+ }
96
+
97
+ let cancel ;
98
+
99
+ lstat ( absoluteFilePath , ( lstatErr , stat ) => {
100
+ if ( lstatErr ) {
101
+ observer . error ( lstatErr ) ;
102
+ return ;
103
+ }
104
+
105
+ if ( ! stat . isFile ( ) ) {
106
+ observer . error ( new Error ( `Expected ${ absoluteFilePath } to be a file path, but it was a ${
107
+ stat . isDirectory ( ) ? 'directory' : 'symbolic link'
108
+ } .`) ) ;
109
+
110
+ return ;
111
+ }
112
+
113
+ let firstWriteFailed = false ;
114
+
115
+ const firstWriteStream = fs . createWriteStream ( tarPath , options ) . on ( 'error' , err => {
116
+ if ( err . code === 'EISDIR' ) {
117
+ err . message = `Tried to write an archive file to ${ absoluteTarPath } , but a directory already exists there.` ;
118
+ observer . error ( err ) ;
119
+
120
+ return ;
121
+ }
122
+
123
+ firstWriteFailed = true ;
124
+ } ) ;
125
+
126
+ mkdirp ( dirname ( tarPath ) , Object . assign ( { fs} , options ) , mkdirpErr => {
127
+ if ( mkdirpErr ) {
128
+ observer . error ( mkdirpErr ) ;
129
+ return ;
130
+ }
131
+
132
+ const packStream = pack ( dirPath , Object . assign ( { fs} , options , {
133
+ entries : [ basename ( filePath ) ] ,
134
+ map ( header ) {
135
+ if ( options . map ) {
136
+ header = options . map ( header ) ;
137
+ }
138
+
139
+ return header ;
140
+ } ,
141
+ mapStream ( fileStream , header ) {
142
+ const newStream = options . mapStream ? options . mapStream ( fileStream , header ) : fileStream ;
143
+
144
+ if ( ! isStream . readable ( newStream ) ) {
145
+ packStream . emit ( 'error' , new TypeError ( `${ MAP_STREAM_ERROR } ${
146
+ isStream ( newStream ) ?
147
+ ' that is readable, but returned a non-readable stream' :
148
+ `, but returned a non-stream value ${ inspectWithKind ( newStream ) } `
149
+ } .`) ) ;
150
+
151
+ return new PassThrough ( ) ;
152
+ }
153
+
154
+ let bytes = 0 ;
155
+
156
+ observer . next ( { bytes, header} ) ;
157
+
158
+ return newStream . pipe ( new Transform ( {
159
+ transform ( chunk , encoding , cb ) {
160
+ bytes += chunk . length ;
161
+
162
+ observer . next ( { bytes, header} ) ;
163
+ cb ( null , chunk ) ;
164
+ }
165
+ } ) ) ;
166
+ }
167
+ } ) ) ;
168
+
169
+ function getDest ( ) {
170
+ return firstWriteFailed ? fs . createWriteStream ( tarPath , options ) : firstWriteStream ;
171
+ }
172
+
173
+ cancel = cancelablePump ( options . tarTransform ? [
174
+ packStream ,
175
+ options . tarTransform ,
176
+ getDest ( )
177
+ ] : [
178
+ packStream ,
179
+ getDest ( )
180
+ ] , err => {
181
+ if ( err ) {
182
+ observer . error ( err ) ;
183
+ return ;
184
+ }
185
+
186
+ observer . complete ( ) ;
187
+ } ) ;
188
+ } ) ;
189
+ } ) ;
190
+
191
+ return function cancelCompression ( ) {
192
+ if ( cancel ) {
193
+ cancel ( ) ;
194
+ }
195
+ } ;
196
+ } ) ;
198
197
} ;
0 commit comments