BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / fs-extra / lib / copy / copy.js

copy.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
copy.js 1.5 KiB Download Raw
1var fs = require('graceful-fs')
2var path = require('path')
3var ncp = require('./ncp')
4var mkdir = require('../mkdirs')
5
6function copy (src, dest, options, callback) {
7 if (typeof options === 'function' && !callback) {
8 callback = options
9 options = {}
10 } else if (typeof options === 'function' || options instanceof RegExp) {
11 options = {filter: options}
12 }
13 callback = callback || function () {}
14 options = options || {}
15
16 // Warn about using preserveTimestamps on 32-bit node:
17 if (options.preserveTimestamps && process.arch === 'ia32') {
18 console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' +
19 'see https://github.com/jprichardson/node-fs-extra/issues/269')
20 }
21
22 // don't allow src and dest to be the same
23 var basePath = process.cwd()
24 var currentPath = path.resolve(basePath, src)
25 var targetPath = path.resolve(basePath, dest)
26 if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
27
28 fs.lstat(src, function (err, stats) {
29 if (err) return callback(err)
30
31 var dir = null
32 if (stats.isDirectory()) {
33 var parts = dest.split(path.sep)
34 parts.pop()
35 dir = parts.join(path.sep)
36 } else {
37 dir = path.dirname(dest)
38 }
39
40 fs.exists(dir, function (dirExists) {
41 if (dirExists) return ncp(src, dest, options, callback)
42 mkdir.mkdirs(dir, function (err) {
43 if (err) return callback(err)
44 ncp(src, dest, options, callback)
45 })
46 })
47 })
48}
49
50module.exports = copy