| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,12 +55,20 @@ function cpSyncFn(src, dest, opts) { | |||
| 55 | 55 | 'node is not recommended'; | |
| 56 | 56 | process.emitWarning(warning, 'TimestampPrecisionWarning'); | |
| 57 | 57 | } | |
| 58 | - const { srcStat, destStat } = checkPathsSync(src, dest, opts); | ||
| 58 | + const { srcStat, destStat, skipped } = checkPathsSync(src, dest, opts); | ||
| 59 | + if (skipped) return; | ||
| 59 | 60 | checkParentPathsSync(src, srcStat, dest); | |
| 60 | - return handleFilterAndCopy(destStat, src, dest, opts); | ||
| 61 | + return checkParentDir(destStat, src, dest, opts); | ||
| 61 | 62 | } | |
| 62 | 63 | ||
| 63 | 64 | function checkPathsSync(src, dest, opts) { | |
| 65 | + if (opts.filter) { | ||
| 66 | + const shouldCopy = opts.filter(src, dest); | ||
| 67 | + if (isPromise(shouldCopy)) { | ||
| 68 | + throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy); | ||
| 69 | + } | ||
| 70 | + if (!shouldCopy) return { __proto__: null, skipped: true }; | ||
| 71 | + } | ||
| 64 | 72 | const { srcStat, destStat } = getStatsSync(src, dest, opts); | |
| 65 | 73 | ||
| 66 | 74 | if (destStat) { | |
@@ -104,7 +112,7 @@ function checkPathsSync(src, dest, opts) { | |||
| 104 | 112 | code: 'EINVAL', | |
| 105 | 113 | }); | |
| 106 | 114 | } | |
| 107 | - return { srcStat, destStat }; | ||
| 115 | + return { __proto__: null, srcStat, destStat, skipped: false }; | ||
| 108 | 116 | } | |
| 109 | 117 | ||
| 110 | 118 | function getStatsSync(src, dest, opts) { | |
@@ -145,24 +153,12 @@ function checkParentPathsSync(src, srcStat, dest) { | |||
| 145 | 153 | return checkParentPathsSync(src, srcStat, destParent); | |
| 146 | 154 | } | |
| 147 | 155 | ||
| 148 | - function handleFilterAndCopy(destStat, src, dest, opts) { | ||
| 149 | - if (opts.filter) { | ||
| 150 | - const shouldCopy = opts.filter(src, dest); | ||
| 151 | - if (isPromise(shouldCopy)) { | ||
| 152 | - throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy); | ||
| 153 | - } | ||
| 154 | - if (!shouldCopy) return; | ||
| 155 | - } | ||
| 156 | + function checkParentDir(destStat, src, dest, opts) { | ||
| 156 | 157 | const destParent = dirname(dest); | |
| 157 | 158 | if (!existsSync(destParent)) mkdirSync(destParent, { recursive: true }); | |
| 158 | 159 | return getStats(destStat, src, dest, opts); | |
| 159 | 160 | } | |
| 160 | 161 | ||
| 161 | - function startCopy(destStat, src, dest, opts) { | ||
| 162 | - if (opts.filter && !opts.filter(src, dest)) return; | ||
| 163 | - return getStats(destStat, src, dest, opts); | ||
| 164 | - } | ||
| 165 | - | ||
| 166 | 162 | function getStats(destStat, src, dest, opts) { | |
| 167 | 163 | const statSyncFn = opts.dereference ? statSync : lstatSync; | |
| 168 | 164 | const srcStat = statSyncFn(src); | |
@@ -284,9 +280,8 @@ function copyDir(src, dest, opts) { | |||
| 284 | 280 | const { name } = dirent; | |
| 285 | 281 | const srcItem = join(src, name); | |
| 286 | 282 | const destItem = join(dest, name); | |
| 287 | - const { destStat } = checkPathsSync(srcItem, destItem, opts); | ||
| 288 | - | ||
| 289 | - startCopy(destStat, srcItem, destItem, opts); | ||
| 283 | + const { destStat, skipped } = checkPathsSync(srcItem, destItem, opts); | ||
| 284 | + if (!skipped) getStats(destStat, srcItem, destItem, opts); | ||
| 290 | 285 | } | |
| 291 | 286 | } finally { | |
| 292 | 287 | dir.closeSync(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -746,24 +746,26 @@ if (!isWindows) { | |||
| 746 | 746 | })); | |
| 747 | 747 | } | |
| 748 | 748 | ||
| 749 | - // Copy async should not throw exception if child folder is filtered out. | ||
| 749 | + // Copy should not throw exception if child folder is filtered out. | ||
| 750 | 750 | { | |
| 751 | 751 | const src = nextdir(); | |
| 752 | - mkdirSync(join(src, 'test-cp-async'), mustNotMutateObjectDeep({ recursive: true })); | ||
| 752 | + mkdirSync(join(src, 'test-cp'), mustNotMutateObjectDeep({ recursive: true })); | ||
| 753 | 753 | ||
| 754 | 754 | const dest = nextdir(); | |
| 755 | 755 | mkdirSync(dest, mustNotMutateObjectDeep({ recursive: true })); | |
| 756 | - writeFileSync(join(dest, 'test-cp-async'), 'test-content', mustNotMutateObjectDeep({ mode: 0o444 })); | ||
| 756 | + writeFileSync(join(dest, 'test-cp'), 'test-content', mustNotMutateObjectDeep({ mode: 0o444 })); | ||
| 757 | 757 | ||
| 758 | - cp(src, dest, { | ||
| 759 | - filter: (path) => !path.includes('test-cp-async'), | ||
| 758 | + const opts = { | ||
| 759 | + filter: (path) => !path.includes('test-cp'), | ||
| 760 | 760 | recursive: true, | |
| 761 | - }, mustCall((err) => { | ||
| 761 | + }; | ||
| 762 | + cp(src, dest, opts, mustCall((err) => { | ||
| 762 | 763 | assert.strictEqual(err, null); | |
| 763 | 764 | })); | |
| 765 | + cpSync(src, dest, opts); | ||
| 764 | 766 | } | |
| 765 | 767 | ||
| 766 | - // Copy async should not throw exception if dest is invalid but filtered out. | ||
| 768 | + // Copy should not throw exception if dest is invalid but filtered out. | ||
| 767 | 769 | { | |
| 768 | 770 | // Create dest as a file. | |
| 769 | 771 | // Expect: cp skips the copy logic entirely and won't throw any exception in path validation process. | |
@@ -775,12 +777,14 @@ if (!isWindows) { | |||
| 775 | 777 | mkdirSync(destParent, mustNotMutateObjectDeep({ recursive: true })); | |
| 776 | 778 | writeFileSync(dest, 'test-content', mustNotMutateObjectDeep({ mode: 0o444 })); | |
| 777 | 779 | ||
| 778 | - cp(src, dest, { | ||
| 780 | + const opts = { | ||
| 779 | 781 | filter: (path) => !path.includes('bar'), | |
| 780 | 782 | recursive: true, | |
| 781 | - }, mustCall((err) => { | ||
| 783 | + }; | ||
| 784 | + cp(src, dest, opts, mustCall((err) => { | ||
| 782 | 785 | assert.strictEqual(err, null); | |
| 783 | 786 | })); | |
| 787 | + cpSync(src, dest, opts); | ||
| 784 | 788 | } | |
| 785 | 789 | ||
| 786 | 790 | // It throws if options is not object. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments