19
19
20
20
const fs = require ( 'fs' )
21
21
const path = require ( 'path' )
22
- const rimraf = require ( 'rimraf' )
23
22
const tmp = require ( 'tmp' )
24
23
25
24
/**
@@ -52,24 +51,16 @@ function checkedCall(fn) {
52
51
* @return {!Promise } A promise to be resolved when the operation has
53
52
* completed.
54
53
*/
55
- exports . rmDir = function ( dirPath ) {
54
+ function rmDir ( dirPath ) {
56
55
return new Promise ( function ( fulfill , reject ) {
57
- var numAttempts = 0
58
- attemptRm ( )
59
- function attemptRm ( ) {
60
- numAttempts += 1
61
- rimraf ( dirPath , function ( err ) {
62
- if ( err ) {
63
- if ( err . code && err . code === 'ENOTEMPTY' && numAttempts < 2 ) {
64
- attemptRm ( )
65
- return
66
- }
67
- reject ( err )
68
- } else {
69
- fulfill ( )
70
- }
71
- } )
72
- }
56
+ fs . rm ( dirPath , { recursive : true , maxRetries : 2 } , function ( err ) {
57
+ if ( err && err . code === 'ENOENT' ) {
58
+ fulfill ( )
59
+ } else if ( err ) {
60
+ reject ( err )
61
+ }
62
+ fulfill ( )
63
+ } )
73
64
} )
74
65
}
75
66
@@ -79,7 +70,7 @@ exports.rmDir = function (dirPath) {
79
70
* @param {string } dst The destination file.
80
71
* @return {!Promise<string> } A promise for the copied file's path.
81
72
*/
82
- exports . copy = function ( src , dst ) {
73
+ function copy ( src , dst ) {
83
74
return new Promise ( function ( fulfill , reject ) {
84
75
const rs = fs . createReadStream ( src )
85
76
rs . on ( 'error' , reject )
@@ -102,7 +93,7 @@ exports.copy = function (src, dst) {
102
93
* @return {!Promise<string> } A promise for the destination
103
94
* directory's path once all files have been copied.
104
95
*/
105
- exports . copyDir = function ( src , dst , opt_exclude ) {
96
+ function copyDir ( src , dst , opt_exclude ) {
106
97
let predicate = opt_exclude
107
98
if ( opt_exclude && typeof opt_exclude !== 'function' ) {
108
99
predicate = function ( p ) {
@@ -172,9 +163,8 @@ exports.stat = function stat(aPath) {
172
163
* @param {string } aPath The path to remove.
173
164
* @return {!Promise } A promise for when the file has been removed.
174
165
*/
175
- exports . unlink = function ( aPath ) {
166
+ function unlink ( aPath ) {
176
167
return new Promise ( function ( fulfill , reject ) {
177
- // eslint-disable-next-line node/no-deprecated-api
178
168
const exists = fs . existsSync ( aPath )
179
169
if ( exists ) {
180
170
fs . unlink ( aPath , function ( err ) {
@@ -190,7 +180,7 @@ exports.unlink = function (aPath) {
190
180
* @return {!Promise<string> } A promise for the path to a temporary directory.
191
181
* @see https://www.npmjs.org/package/tmp
192
182
*/
193
- exports . tmpDir = function ( ) {
183
+ function tmpDir ( ) {
194
184
return checkedCall ( ( callback ) => tmp . dir ( { unsafeCleanup : true } , callback ) )
195
185
}
196
186
@@ -199,7 +189,7 @@ exports.tmpDir = function () {
199
189
* @return {!Promise<string> } A promise for the path to a temporary file.
200
190
* @see https://www.npmjs.org/package/tmp
201
191
*/
202
- exports . tmpFile = function ( opt_options ) {
192
+ function tmpFile ( opt_options ) {
203
193
return checkedCall ( ( callback ) => {
204
194
/** check fixed in v > 0.2.1 if
205
195
* (typeof options === 'function') {
@@ -219,7 +209,7 @@ exports.tmpFile = function (opt_options) {
219
209
* @return {?string } Path to the located file, or {@code null} if it could
220
210
* not be found.
221
211
*/
222
- exports . findInPath = function ( file , opt_checkCwd ) {
212
+ function findInPath ( file , opt_checkCwd ) {
223
213
const dirs = [ ]
224
214
if ( opt_checkCwd ) {
225
215
dirs . push ( process . cwd ( ) )
@@ -246,7 +236,7 @@ exports.findInPath = function (file, opt_checkCwd) {
246
236
* @return {!Promise<!Buffer> } A promise that will resolve with a buffer of the
247
237
* file contents.
248
238
*/
249
- exports . read = function ( aPath ) {
239
+ function read ( aPath ) {
250
240
return checkedCall ( ( callback ) => fs . readFile ( aPath , callback ) )
251
241
}
252
242
@@ -258,7 +248,7 @@ exports.read = function (aPath) {
258
248
* @return {!Promise } A promise that will resolve when the operation has
259
249
* completed.
260
250
*/
261
- exports . write = function ( aPath , data ) {
251
+ function write ( aPath , data ) {
262
252
return checkedCall ( ( callback ) => fs . writeFile ( aPath , data , callback ) )
263
253
}
264
254
@@ -269,7 +259,7 @@ exports.write = function (aPath, data) {
269
259
* @return {!Promise<string> } A promise that will resolve with the path of the
270
260
* created directory.
271
261
*/
272
- exports . mkdir = function ( aPath ) {
262
+ function mkdir ( aPath ) {
273
263
return checkedCall ( ( callback ) => {
274
264
fs . mkdir ( aPath , undefined , ( err ) => {
275
265
if ( err && err . code !== 'EEXIST' ) {
@@ -288,7 +278,7 @@ exports.mkdir = function (aPath) {
288
278
* @return {!Promise<string> } A promise that will resolve with the path of the
289
279
* created directory.
290
280
*/
291
- exports . mkdirp = function mkdirp ( dir ) {
281
+ function mkdirp ( dir ) {
292
282
return checkedCall ( ( callback ) => {
293
283
fs . mkdir ( dir , undefined , ( err ) => {
294
284
if ( ! err ) {
@@ -324,7 +314,7 @@ exports.mkdirp = function mkdirp(dir) {
324
314
* resolve with a list of entries seen. For each entry, the recorded path
325
315
* will be relative to `rootPath`.
326
316
*/
327
- exports . walkDir = function ( rootPath ) {
317
+ function walkDir ( rootPath ) {
328
318
const seen = [ ]
329
319
return ( function walk ( dir ) {
330
320
return checkedCall ( ( callback ) => fs . readdir ( dir , callback ) ) . then ( ( files ) =>
@@ -343,3 +333,17 @@ exports.walkDir = function (rootPath) {
343
333
)
344
334
} ) ( rootPath ) . then ( ( ) => seen )
345
335
}
336
+
337
+ // PUBLIC API
338
+ module . exports . walkDir = walkDir
339
+ module . exports . rmDir = rmDir
340
+ module . exports . mkdirp = mkdirp
341
+ module . exports . mkdir = mkdir
342
+ module . exports . write = write
343
+ module . exports . read = read
344
+ module . exports . findInPath = findInPath
345
+ module . exports . tmpFile = tmpFile
346
+ module . exports . tmpDir = tmpDir
347
+ module . exports . unlink = unlink
348
+ module . exports . copy = copy
349
+ module . exports . copyDir = copyDir
0 commit comments