Skip to content

Commit 9f9dafd

Browse files
committed
fix(cp): add default arguments
1 parent 5df3efe commit 9f9dafd

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

lib/cp/polyfill.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const {
2222
ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY,
2323
ERR_FS_CP_UNKNOWN,
2424
ERR_FS_EISDIR,
25-
} = require('../errors.js')
26-
25+
ERR_INVALID_ARG_TYPE,
26+
} = require('../errors.js');
2727
const {
2828
constants: {
2929
errno: {
@@ -33,7 +33,7 @@ const {
3333
ENOTDIR,
3434
}
3535
}
36-
} = require('os')
36+
} = require('os');
3737
const {
3838
chmod,
3939
copyFile,
@@ -53,9 +53,37 @@ const {
5353
parse,
5454
resolve,
5555
sep,
56+
toNamespacedPath,
5657
} = require('path');
58+
const { fileURLToPath } = require('url');
59+
60+
const defaultOptions = {
61+
dereference: false,
62+
errorOnExist: false,
63+
filter: undefined,
64+
force: true,
65+
preserveTimestamps: false,
66+
recursive: false,
67+
};
5768

5869
async function cp(src, dest, opts) {
70+
if (opts != undefined && typeof opts !== 'object') {
71+
throw new ERR_INVALID_ARG_TYPE('options', ['Object'], opts);
72+
}
73+
return cpFn(
74+
toNamespacedPath(getValidatedPath(src)),
75+
toNamespacedPath(getValidatedPath(dest)),
76+
{ ...defaultOptions, ...opts });
77+
}
78+
79+
function getValidatedPath(fileURLOrPath) {
80+
const path = fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin
81+
? fileURLToPath(fileURLOrPath)
82+
: fileURLOrPath
83+
return path;
84+
}
85+
86+
async function cpFn(src, dest, opts) {
5987
// Warn about using preserveTimestamps on 32-bit node
6088
if (opts.preserveTimestamps && process.arch === 'ia32') {
6189
const warning = 'Using the preserveTimestamps option in 32-bit ' +

0 commit comments

Comments
 (0)