UNPKG

929 BJavaScriptView Raw
1"use strict";
2
3const fs = require('fs');
4const promisify = require('../function/promisify');
5
6//from graceful-fs
7
8// on Windows, A/V software can lock the directory, causing this
9// to fail with an EACCES or EPERM if the directory contains newly
10// created files. Try again on failure, for up to 2 seconds.
11
12let rename = fs.rename;
13
14/*istanbul ignore next*/
15if(process.platform === "win32") {
16
17 rename = function(from, to, cb) {
18
19 var start = Date.now();
20 var backoff = 0;
21
22 fs.rename(from, to, function CB(err) {
23 //retry later
24 if(err
25 && (err.code === "EACCES" || err.code === "EPERM")
26 && Date.now() - start < 60 * 1000) {
27
28 setTimeout(function() {
29 fs.rename(from, to, CB); //retry
30 }, backoff);
31
32 if(backoff < 100)
33 backoff += 10;
34 return;
35 }
36 if(cb)
37 cb(err);
38 });
39 };
40
41}
42
43module.exports = promisify(rename);