@@ -6,16 +6,11 @@ import { eventEmitter, eventError, tree } from './events'
66
77type StepFunction = ( p : string , uuid : string ) => Promise < string >
88
9- interface FakeLogError {
10- ( message ?: string | undefined ) : Error
11- code : string
12- }
13-
149export default class RmDir {
1510 private steps : Array < StepFunction >
1611 compile : ( uuid : string ) => typeof fs . rmdir
1712
18- constructor ( ) {
13+ constructor ( ) {
1914 this . steps = [ ]
2015 this . compile = ( uuid ) => {
2116 tree [ uuid ] = [ ]
@@ -27,36 +22,40 @@ export default class RmDir {
2722 }
2823 this . steps . reduce ( ( prev : Promise < string > , next : StepFunction ) => {
2924 return prev . then ( ( p ) => next ( p , uuid ) )
30- /* .catch((err: NodeJS.ErrnoException) => {
31- eventError(err, p as string)
32- callback!(err)
33- return Promise.reject(err)
34- }) */
25+ . catch ( ( err : NodeJS . ErrnoException ) => {
26+ if ( err . message !== 'handledPromise' ) {
27+ eventError ( err , p as string )
28+ callback ! ( err )
29+ }
30+ return Promise . reject ( new Error ( 'handledPromise' ) )
31+ } )
3532 } , this . init ( p as string ) )
3633 . then ( ( ) => callback ! ( null ) )
3734 . catch ( ( err : NodeJS . ErrnoException ) => {
38- eventError ( err , p as string )
39- callback ! ( err )
35+ if ( err . message !== 'handledPromise' ) {
36+ eventError ( err , p as string )
37+ callback ! ( err )
38+ }
4039 } )
4140 } ,
4241 { __promisify__ : util . promisify ( fs . rmdir ) } // FIXME
4342 )
4443 }
4544 }
4645
47- private init ( p : string ) : Promise < string > {
46+ private init ( p : string ) : Promise < string > {
4847 return new Promise ( ( resolve ) => {
4948 eventEmitter . emit ( 'start' , p )
5049 resolve ( p )
5150 } )
5251 }
5352
54- then ( fun : StepFunction ) {
53+ then ( fun : StepFunction ) {
5554 this . steps . push ( fun )
5655 return this
5756 }
5857
59- log ( ) {
58+ log ( ) {
6059 this . steps . push (
6160 function ( p : string , uuid : string ) {
6261 return new Promise ( ( resolve , reject ) => {
@@ -76,24 +75,34 @@ export default class RmDir {
7675 }
7776
7877 // Rename to random string
79- rename ( ) {
78+ rename ( ) {
8079 this . steps . push (
8180 function ( p : string ) {
8281 return new Promise ( ( resolve , reject ) => {
83- const newName = crypto . randomBytes ( 9 ) . toString ( 'base64' ) . replace ( / \/ / g, '0' ) . replace ( / \+ / g, 'a' )
84- const newPath = path . join ( path . dirname ( p ) , newName )
85- eventEmitter . emit ( 'verbose' , p , `Renaming to ${ newName } ` )
86- fs . rename ( p , newPath , ( err ) => {
82+ fs . readdir ( p , ( err , files ) => {
8783 if ( err ) reject ( err )
88- else resolve ( newPath )
84+ if ( ! files . length ) {
85+ // directory appears to be empty
86+ const newName = crypto . randomBytes ( 9 ) . toString ( 'base64' ) . replace ( / \/ / g, '0' ) . replace ( / \+ / g, 'a' )
87+ const newPath = path . join ( path . dirname ( p ) , newName )
88+ eventEmitter . emit ( 'verbose' , p , `Renaming to ${ newName } ` )
89+ fs . rename ( p , newPath , ( err ) => {
90+ if ( err ) reject ( err )
91+ else resolve ( newPath )
92+ } )
93+ } else {
94+ fs . rmdir ( p , ( err ) => {
95+ reject ( err )
96+ } )
97+ }
8998 } )
9099 } )
91100 } )
92101 return this
93102 }
94103
95104 // End function: remove the directory
96- rmdir ( ) {
105+ rmdir ( ) {
97106 this . steps . push (
98107 function ( p : string ) {
99108 return new Promise ( ( resolve , reject ) => {
0 commit comments