feat(fs): rmdir method (#3730)

This commit is contained in:
Enishowk 2018-11-28 17:42:32 +01:00 committed by Julien Fontanet
parent 2e327be49d
commit 8751180634
3 changed files with 38 additions and 0 deletions

View File

@ -21,6 +21,7 @@
},
"dependencies": {
"@marsaud/smb2": "^0.9.0",
"@xen-orchestra/async-map": "^0.0.0",
"execa": "^1.0.0",
"fs-extra": "^7.0.0",
"get-stream": "^4.0.0",

View File

@ -1,6 +1,7 @@
// @flow
// $FlowFixMe
import asyncMap from '@xen-orchestra/async-map'
import getStream from 'get-stream'
import { randomBytes } from 'crypto'
import { fromCallback, fromEvent, ignoreErrors, timeout } from 'promise-toolbox'
@ -145,6 +146,38 @@ export default class RemoteHandlerAbstract {
throw new Error('Not implemented')
}
async rmdir(
dir: string,
{ recursive = false }: { recursive?: boolean } = {}
) {
await (recursive ? this._rmtree(dir) : this._rmdir(dir))
}
async _rmdir(dir: string) {
throw new Error('Not implemented')
}
async _rmtree(dir: string) {
try {
return await this._rmdir(dir)
} catch (error) {
if (error.code !== 'ENOTEMPTY') {
throw error
}
}
const files = await this._list(dir)
await asyncMap(files, file =>
this._unlink(`${dir}/${file}`).catch(error => {
if (error.code === 'EISDIR') {
return this._rmtree(`${dir}/${file}`)
}
throw error
})
)
return this._rmtree(dir)
}
async list(
dir: string = '.',
{

View File

@ -120,4 +120,8 @@ export default class LocalHandler extends RemoteHandlerAbstract {
async _closeFile(fd) {
return fs.close(fd)
}
async _rmdir(dir) {
return fs.rmdir(dir)
}
}