Skip to content

Commit a993599

Browse files
committed
Using fs-extra in a draft PR.
1 parent de45f90 commit a993599

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2232
-0
lines changed

node_modules/fs-extra/LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2011-2017 JP Richardson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
6+
(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
7+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11+
12+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
'use strict'
2+
3+
const fs = require('graceful-fs')
4+
const path = require('path')
5+
const mkdirsSync = require('../mkdirs').mkdirsSync
6+
const utimesMillisSync = require('../util/utimes').utimesMillisSync
7+
const stat = require('../util/stat')
8+
9+
function copySync (src, dest, opts) {
10+
if (typeof opts === 'function') {
11+
opts = { filter: opts }
12+
}
13+
14+
opts = opts || {}
15+
opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
16+
opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
17+
18+
// Warn about using preserveTimestamps on 32-bit node
19+
if (opts.preserveTimestamps && process.arch === 'ia32') {
20+
console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
21+
see https://github.com/jprichardson/node-fs-extra/issues/269`)
22+
}
23+
24+
const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
25+
stat.checkParentPathsSync(src, srcStat, dest, 'copy')
26+
return handleFilterAndCopy(destStat, src, dest, opts)
27+
}
28+
29+
function handleFilterAndCopy (destStat, src, dest, opts) {
30+
if (opts.filter && !opts.filter(src, dest)) return
31+
const destParent = path.dirname(dest)
32+
if (!fs.existsSync(destParent)) mkdirsSync(destParent)
33+
return getStats(destStat, src, dest, opts)
34+
}
35+
36+
function startCopy (destStat, src, dest, opts) {
37+
if (opts.filter && !opts.filter(src, dest)) return
38+
return getStats(destStat, src, dest, opts)
39+
}
40+
41+
function getStats (destStat, src, dest, opts) {
42+
const statSync = opts.dereference ? fs.statSync : fs.lstatSync
43+
const srcStat = statSync(src)
44+
45+
if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
46+
else if (srcStat.isFile() ||
47+
srcStat.isCharacterDevice() ||
48+
srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
49+
else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
50+
else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
51+
else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
52+
throw new Error(`Unknown file: ${src}`)
53+
}
54+
55+
function onFile (srcStat, destStat, src, dest, opts) {
56+
if (!destStat) return copyFile(srcStat, src, dest, opts)
57+
return mayCopyFile(srcStat, src, dest, opts)
58+
}
59+
60+
function mayCopyFile (srcStat, src, dest, opts) {
61+
if (opts.overwrite) {
62+
fs.unlinkSync(dest)
63+
return copyFile(srcStat, src, dest, opts)
64+
} else if (opts.errorOnExist) {
65+
throw new Error(`'${dest}' already exists`)
66+
}
67+
}
68+
69+
function copyFile (srcStat, src, dest, opts) {
70+
fs.copyFileSync(src, dest)
71+
if (opts.preserveTimestamps) handleTimestamps(srcStat.mode, src, dest)
72+
return setDestMode(dest, srcStat.mode)
73+
}
74+
75+
function handleTimestamps (srcMode, src, dest) {
76+
// Make sure the file is writable before setting the timestamp
77+
// otherwise open fails with EPERM when invoked with 'r+'
78+
// (through utimes call)
79+
if (fileIsNotWritable(srcMode)) makeFileWritable(dest, srcMode)
80+
return setDestTimestamps(src, dest)
81+
}
82+
83+
function fileIsNotWritable (srcMode) {
84+
return (srcMode & 0o200) === 0
85+
}
86+
87+
function makeFileWritable (dest, srcMode) {
88+
return setDestMode(dest, srcMode | 0o200)
89+
}
90+
91+
function setDestMode (dest, srcMode) {
92+
return fs.chmodSync(dest, srcMode)
93+
}
94+
95+
function setDestTimestamps (src, dest) {
96+
// The initial srcStat.atime cannot be trusted
97+
// because it is modified by the read(2) system call
98+
// (See https://nodejs.org/api/fs.html#fs_stat_time_values)
99+
const updatedSrcStat = fs.statSync(src)
100+
return utimesMillisSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
101+
}
102+
103+
function onDir (srcStat, destStat, src, dest, opts) {
104+
if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
105+
return copyDir(src, dest, opts)
106+
}
107+
108+
function mkDirAndCopy (srcMode, src, dest, opts) {
109+
fs.mkdirSync(dest)
110+
copyDir(src, dest, opts)
111+
return setDestMode(dest, srcMode)
112+
}
113+
114+
function copyDir (src, dest, opts) {
115+
fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
116+
}
117+
118+
function copyDirItem (item, src, dest, opts) {
119+
const srcItem = path.join(src, item)
120+
const destItem = path.join(dest, item)
121+
const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
122+
return startCopy(destStat, srcItem, destItem, opts)
123+
}
124+
125+
function onLink (destStat, src, dest, opts) {
126+
let resolvedSrc = fs.readlinkSync(src)
127+
if (opts.dereference) {
128+
resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
129+
}
130+
131+
if (!destStat) {
132+
return fs.symlinkSync(resolvedSrc, dest)
133+
} else {
134+
let resolvedDest
135+
try {
136+
resolvedDest = fs.readlinkSync(dest)
137+
} catch (err) {
138+
// dest exists and is a regular file or directory,
139+
// Windows may throw UNKNOWN error. If dest already exists,
140+
// fs throws error anyway, so no need to guard against it here.
141+
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
142+
throw err
143+
}
144+
if (opts.dereference) {
145+
resolvedDest = path.resolve(process.cwd(), resolvedDest)
146+
}
147+
if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
148+
throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
149+
}
150+
151+
// prevent copy if src is a subdir of dest since unlinking
152+
// dest in this case would result in removing src contents
153+
// and therefore a broken symlink would be created.
154+
if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
155+
throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
156+
}
157+
return copyLink(resolvedSrc, dest)
158+
}
159+
}
160+
161+
function copyLink (resolvedSrc, dest) {
162+
fs.unlinkSync(dest)
163+
return fs.symlinkSync(resolvedSrc, dest)
164+
}
165+
166+
module.exports = copySync
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
module.exports = {
4+
copySync: require('./copy-sync')
5+
}

0 commit comments

Comments
 (0)