Sha256: 9df4b4df8f35eb01cc171c8c086124862f2173c7d37d890730fdb5137c2161a3

Contents?: true

Size: 1.59 KB

Versions: 23

Compression:

Stored size: 1.59 KB

Contents

'use strict'

const fs = require('graceful-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 stat = require('../util/stat')

function move (src, dest, opts, cb) {
  if (typeof opts === 'function') {
    cb = opts
    opts = {}
  }

  const overwrite = opts.overwrite || opts.clobber || false

  stat.checkPaths(src, dest, 'move', (err, stats) => {
    if (err) return cb(err)
    const { srcStat } = stats
    stat.checkParentPaths(src, srcStat, dest, 'move', err => {
      if (err) return cb(err)
      mkdirp(path.dirname(dest), err => {
        if (err) return cb(err)
        return doRename(src, dest, overwrite, cb)
      })
    })
  })
}

function doRename (src, dest, overwrite, cb) {
  if (overwrite) {
    return remove(dest, err => {
      if (err) return cb(err)
      return rename(src, dest, overwrite, cb)
    })
  }
  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)
  })
}

function moveAcrossDevice (src, dest, overwrite, cb) {
  const opts = {
    overwrite,
    errorOnExist: true
  }
  copy(src, dest, opts, err => {
    if (err) return cb(err)
    return remove(src, cb)
  })
}

module.exports = move

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
appmap-0.72.2 ./node_modules/fs-extra/lib/move/move.js
appmap-0.72.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.72.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.71.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.70.2 ./node_modules/fs-extra/lib/move/move.js
appmap-0.70.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.70.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.69.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.68.2 ./node_modules/fs-extra/lib/move/move.js
appmap-0.68.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.68.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.67.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.67.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.66.2 ./node_modules/fs-extra/lib/move/move.js
appmap-0.66.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.66.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.65.1 ./node_modules/fs-extra/lib/move/move.js
appmap-0.65.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.64.0 ./node_modules/fs-extra/lib/move/move.js
appmap-0.63.0 ./node_modules/fs-extra/lib/move/move.js