@@ -133,26 +133,38 @@ export async function readDirAsZip(
133133 } ) ;
134134}
135135
136- function _checkDir (
137- objects : ObjectsClient ,
138- id : string ,
139- root : string ,
140- parts : string [ ] ,
141- options : any ,
142- callback : ( ) => void
143- ) : void {
144- if ( ! parts || ! parts . length ) {
145- callback ( ) ;
136+ interface CheckDirOptions {
137+ objects : ObjectsClient ;
138+ id : string ;
139+ root : string ;
140+ parts : string [ ] ;
141+ options : any ;
142+ }
143+
144+ /**
145+ * Check that directory exists recursive
146+ *
147+ * @param _options directory information and objects db
148+ */
149+ async function _checkDir ( _options : CheckDirOptions ) : Promise < void > {
150+ const { parts, id, options, objects } = _options ;
151+ let { root } = _options ;
152+
153+ if ( ! parts ?. length ) {
146154 return ;
147155 }
156+
148157 root += '/' + parts . shift ( ) ;
149- objects . readDir ( id , root , options , ( err , _files ) => {
150- if ( err ?. message === tools . ERRORS . ERROR_NOT_FOUND ) {
151- objects . mkdir ( id , root , options , _err => _checkDir ( objects , id , root , parts , options , callback ) ) ;
152- } else {
153- _checkDir ( objects , id , root , parts , options , callback ) ;
158+
159+ try {
160+ await objects . readDirAsync ( id , root , options ) ;
161+ } catch ( e ) {
162+ if ( e . message === tools . ERRORS . ERROR_NOT_FOUND ) {
163+ await objects . mkdirAsync ( id , root , options ) ;
154164 }
155- } ) ;
165+ }
166+
167+ return _checkDir ( { id, objects, options, root, parts } ) ;
156168}
157169
158170async function _writeOneFile (
@@ -164,24 +176,16 @@ async function _writeOneFile(
164176 options : any
165177) : Promise < void > {
166178 let data = await zip . files [ filename ] . async ( 'nodebuffer' ) ;
167- let _err : Error ;
179+
168180 if ( options . parse ) {
169- try {
170- data = options . parse ( name , filename , data , options ? options . settings : null ) ;
171- } catch ( e ) {
172- _err = e ;
173- }
181+ data = options . parse ( name , filename , data , options ? options . settings : null ) ;
174182 }
175183 const fName = name + filename ;
176184 const parts = fName . split ( '/' ) ;
177185 parts . pop ( ) ;
178- return new Promise ( ( resolve , reject ) =>
179- _checkDir ( objects , id , '' , parts , options , ( ) =>
180- objects . writeFile ( id , name + filename , data , options , err =>
181- _err || err ? reject ( _err || err ) : resolve ( )
182- )
183- )
184- ) ;
186+
187+ await _checkDir ( { objects, id, root : '' , parts, options } ) ;
188+ return objects . writeFileAsync ( id , name + filename , data , options ) ;
185189}
186190
187191export async function writeDirAsZip (
0 commit comments