Sha256: 6c9acfd568d682cec7ac75697bf79c4ad64a6e14adda4a27520464030441ab88

Contents?: true

Size: 1.62 KB

Versions: 46

Compression:

Stored size: 1.62 KB

Contents

'use strict'

const fs = require('graceful-fs')
const path = require('path')
const ncp = require('./ncp')
const mkdir = require('../mkdirs')
const pathExists = require('../path-exists').pathExists

function copy (src, dest, options, callback) {
  if (typeof options === 'function' && !callback) {
    callback = options
    options = {}
  } else if (typeof options === 'function' || options instanceof RegExp) {
    options = {filter: options}
  }
  callback = callback || function () {}
  options = options || {}

  // Warn about using preserveTimestamps on 32-bit node:
  if (options.preserveTimestamps && process.arch === 'ia32') {
    console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
    see https://github.com/jprichardson/node-fs-extra/issues/269`)
  }

  // don't allow src and dest to be the same
  const basePath = process.cwd()
  const currentPath = path.resolve(basePath, src)
  const targetPath = path.resolve(basePath, dest)
  if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))

  fs.lstat(src, (err, stats) => {
    if (err) return callback(err)

    let dir = null
    if (stats.isDirectory()) {
      const parts = dest.split(path.sep)
      parts.pop()
      dir = parts.join(path.sep)
    } else {
      dir = path.dirname(dest)
    }

    pathExists(dir, (err, dirExists) => {
      if (err) return callback(err)
      if (dirExists) return ncp(src, dest, options, callback)
      mkdir.mkdirs(dir, err => {
        if (err) return callback(err)
        ncp(src, dest, options, callback)
      })
    })
  })
}

module.exports = copy

Version data entries

46 entries across 46 versions & 2 rubygems

Version Path
ilog-0.4.1 node_modules/fs-extra/lib/copy/copy.js
ilog-0.4.0 node_modules/fs-extra/lib/copy/copy.js
ilog-0.3.3 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-18.0.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.21.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.20.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.19.1 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.19.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.18.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.17.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.16.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.15.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.14.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.13.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.12.2 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.12.1 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.12.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.11.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.10.0 node_modules/fs-extra/lib/copy/copy.js
govuk_publishing_components-17.9.0 node_modules/fs-extra/lib/copy/copy.js