mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
add a workaround for EPERM issue on windows
Derived from @korto's fix: https://github.com/rlidwka/sinopia/issues/67#issuecomment-44782521
This commit is contained in:
parent
7a7d794207
commit
a854d07a66
1 changed files with 26 additions and 3 deletions
|
@ -20,12 +20,35 @@ try {
|
|||
}
|
||||
}
|
||||
|
||||
function tempFile(str) {
|
||||
return str + '.tmp' + String(Math.random()).substr(2)
|
||||
}
|
||||
|
||||
function renameTmp(src, dst, _cb) {
|
||||
function cb(err) {
|
||||
if (err) fs.unlink(src)
|
||||
_cb(err)
|
||||
}
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
return fs.rename(src, dst, cb)
|
||||
}
|
||||
|
||||
// windows can't remove opened file,
|
||||
// but it seem to be able to rename it
|
||||
var tmp = tempFile(dst)
|
||||
fs.rename(dst, tmp, function(err) {
|
||||
fs.rename(src, dst, cb)
|
||||
if (!err) fs.unlink(tmp)
|
||||
})
|
||||
}
|
||||
|
||||
function write(dest, data, cb) {
|
||||
var safe_write = function(cb) {
|
||||
var tmpname = dest + '.tmp' + String(Math.random()).substr(2)
|
||||
var tmpname = tempFile(dest)
|
||||
fs.writeFile(tmpname, data, function(err) {
|
||||
if (err) return cb(err)
|
||||
return fs.rename(tmpname, dest, cb)
|
||||
renameTmp(tmpname, dest, cb)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -60,7 +83,7 @@ function write_stream(name) {
|
|||
stream.done = function() {
|
||||
function onend() {
|
||||
file.on('close', function() {
|
||||
fs.rename(tmpname, name, function(err) {
|
||||
renameTmp(tmpname, name, function(err) {
|
||||
if (err) {
|
||||
stream.emit('error', err)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Reference in a new issue