Skip to content

Commit fddb7cf

Browse files
authored
feat: polyfill fs.cp. (#6)
1 parent f11b6a3 commit fddb7cf

File tree

9 files changed

+1085
-0
lines changed

9 files changed

+1085
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ polyfills, and extensions, of the core `fs` module.
1212
- `fs.mkdtemp` extended to accept an `owner` option
1313
- `fs.writeFile` extended to accept an `owner` option
1414
- `fs.withTempDir` added
15+
- `fs.cp` polyfill for node < 16.7.0
1516

1617
## The `owner` option
1718

lib/cp/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.

lib/cp/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fs = require('../fs.js')
2+
const getOptions = require('../common/get-options.js')
3+
const node = require('../common/node.js')
4+
const polyfill = require('./polyfill.js')
5+
6+
// node 16.7.0 added fs.cp
7+
const useNative = node.satisfies('>=16.7.0')
8+
9+
const cp = async (src, dest, opts) => {
10+
const options = getOptions(opts, {
11+
copy: ['dereference', 'errorOnExist', 'filter', 'force', 'preserveTimestamps', 'recursive'],
12+
})
13+
14+
// the polyfill is tested separately from this module, no need to hack
15+
// process.version to try to trigger it just for coverage
16+
// istanbul ignore next
17+
return useNative
18+
? fs.cp(src, dest, options)
19+
: polyfill(src, dest, options)
20+
}
21+
22+
module.exports = cp

0 commit comments

Comments
 (0)