-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor move
API to async/await
#1025
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,59 @@ | ||
'use strict' | ||
|
||
const fs = require('graceful-fs') | ||
const fs = require('../fs') | ||
const path = require('path') | ||
const copy = require('../copy').copy | ||
const remove = require('../remove').remove | ||
const mkdirp = require('../mkdirs').mkdirp | ||
const pathExists = require('../path-exists').pathExists | ||
const { copy } = require('../copy') | ||
const { remove } = require('../remove') | ||
const { mkdirp } = require('../mkdirs') | ||
const { pathExists } = require('../path-exists') | ||
const stat = require('../util/stat') | ||
|
||
function move (src, dest, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} | ||
async function move (src, dest, opts = {}) { | ||
const overwrite = opts.overwrite || opts.clobber || false | ||
|
||
opts = opts || {} | ||
const { srcStat, isChangingCase = false } = await stat.checkPaths(src, dest, 'move', opts) | ||
|
||
const overwrite = opts.overwrite || opts.clobber || false | ||
await stat.checkParentPaths(src, srcStat, dest, 'move') | ||
|
||
stat.checkPaths(src, dest, 'move', opts, (err, stats) => { | ||
if (err) return cb(err) | ||
const { srcStat, isChangingCase = false } = stats | ||
stat.checkParentPaths(src, srcStat, dest, 'move', err => { | ||
if (err) return cb(err) | ||
if (isParentRoot(dest)) return doRename(src, dest, overwrite, isChangingCase, cb) | ||
mkdirp(path.dirname(dest), err => { | ||
if (err) return cb(err) | ||
return doRename(src, dest, overwrite, isChangingCase, cb) | ||
}) | ||
}) | ||
}) | ||
} | ||
// If the parent of dest is not root, make sure it exists before proceeding | ||
const destParent = path.dirname(dest) | ||
const parsedParentPath = path.parse(destParent) | ||
if (parsedParentPath.root !== destParent) { | ||
await mkdirp(destParent) | ||
} | ||
|
||
function isParentRoot (dest) { | ||
const parent = path.dirname(dest) | ||
const parsedPath = path.parse(parent) | ||
return parsedPath.root === parent | ||
return doRename(src, dest, overwrite, isChangingCase) | ||
} | ||
|
||
function doRename (src, dest, overwrite, isChangingCase, cb) { | ||
if (isChangingCase) return rename(src, dest, overwrite, cb) | ||
if (overwrite) { | ||
return remove(dest, err => { | ||
if (err) return cb(err) | ||
return rename(src, dest, overwrite, cb) | ||
}) | ||
async function doRename (src, dest, overwrite, isChangingCase) { | ||
if (!isChangingCase) { | ||
if (overwrite) { | ||
await remove(dest) | ||
} else if (await pathExists(dest)) { | ||
throw new Error('dest already exists.') | ||
} | ||
} | ||
pathExists(dest, (err, destExists) => { | ||
if (err) return cb(err) | ||
if (destExists) return cb(new Error('dest already exists.')) | ||
return rename(src, dest, overwrite, cb) | ||
}) | ||
} | ||
|
||
function rename (src, dest, overwrite, cb) { | ||
fs.rename(src, dest, err => { | ||
if (!err) return cb() | ||
if (err.code !== 'EXDEV') return cb(err) | ||
return moveAcrossDevice(src, dest, overwrite, cb) | ||
}) | ||
try { | ||
// Try w/ rename first, and try copy + remove if EXDEV | ||
await fs.rename(src, dest) | ||
} catch (err) { | ||
if (err.code !== 'EXDEV') { | ||
throw err | ||
} | ||
await moveAcrossDevice(src, dest, overwrite) | ||
} | ||
} | ||
|
||
function moveAcrossDevice (src, dest, overwrite, cb) { | ||
async function moveAcrossDevice (src, dest, overwrite) { | ||
const opts = { | ||
overwrite, | ||
errorOnExist: true, | ||
preserveTimestamps: true | ||
} | ||
copy(src, dest, opts, err => { | ||
if (err) return cb(err) | ||
return remove(src, cb) | ||
}) | ||
|
||
await copy(src, dest, opts) | ||
return remove(src) | ||
} | ||
|
||
module.exports = move |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
move
now uses the Promise version of thefs.rename
API (instead of the callback version), so the stub here needs to be changed.