Last change
on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
992 bytes
|
Line | |
---|
1 | 'use strict'
|
---|
2 |
|
---|
3 | const u = require('universalify').fromCallback
|
---|
4 | const fs = require('graceful-fs')
|
---|
5 | const path = require('path')
|
---|
6 | const mkdir = require('../mkdirs')
|
---|
7 | const remove = require('../remove')
|
---|
8 |
|
---|
9 | const emptyDir = u(function emptyDir (dir, callback) {
|
---|
10 | callback = callback || function () {}
|
---|
11 | fs.readdir(dir, (err, items) => {
|
---|
12 | if (err) return mkdir.mkdirs(dir, callback)
|
---|
13 |
|
---|
14 | items = items.map(item => path.join(dir, item))
|
---|
15 |
|
---|
16 | deleteItem()
|
---|
17 |
|
---|
18 | function deleteItem () {
|
---|
19 | const item = items.pop()
|
---|
20 | if (!item) return callback()
|
---|
21 | remove.remove(item, err => {
|
---|
22 | if (err) return callback(err)
|
---|
23 | deleteItem()
|
---|
24 | })
|
---|
25 | }
|
---|
26 | })
|
---|
27 | })
|
---|
28 |
|
---|
29 | function emptyDirSync (dir) {
|
---|
30 | let items
|
---|
31 | try {
|
---|
32 | items = fs.readdirSync(dir)
|
---|
33 | } catch (err) {
|
---|
34 | return mkdir.mkdirsSync(dir)
|
---|
35 | }
|
---|
36 |
|
---|
37 | items.forEach(item => {
|
---|
38 | item = path.join(dir, item)
|
---|
39 | remove.removeSync(item)
|
---|
40 | })
|
---|
41 | }
|
---|
42 |
|
---|
43 | module.exports = {
|
---|
44 | emptyDirSync,
|
---|
45 | emptydirSync: emptyDirSync,
|
---|
46 | emptyDir,
|
---|
47 | emptydir: emptyDir
|
---|
48 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.