Skip to content

Commit e970c1f

Browse files
authored
Merge pull request #8921 from liranmauda/liran-bump-deps
Replace `ncp` with native `fs/promises`
2 parents 627ef67 + f12a259 commit e970c1f

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

package-lock.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
"mongodb": "3.7.4",
106106
"morgan": "1.10.0",
107107
"nan": "2.22.2",
108-
"ncp": "2.0.0",
109108
"node-addon-api": "8.3.1",
110109
"node-rdkafka": "3.3.1",
111110
"performance-now": "2.1.0",

src/util/fs_utils.js

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const fs = require('fs'); // For createWriteStream
55
const fsp = require('fs/promises');
6-
const ncp = require('ncp').ncp;
76
const path = require('path');
87
const crypto = require('crypto');
98

@@ -219,31 +218,55 @@ async function file_delete(file_name) {
219218
}
220219
}
221220

222-
function full_dir_copy(src, dst, filter_regex) {
223-
return P.fromCallback(callback => {
224-
ncp.limit = 10;
225-
const ncp_options = {};
226-
if (filter_regex) {
227-
//this regexp will filter out files that matches, except path.
228-
const ncp_filter_regex = new RegExp(filter_regex);
229-
const ncp_filter_function = input => {
230-
if (input.indexOf('/') > 0) {
231-
return false;
232-
} else if (ncp_filter_regex.test(input)) {
233-
return false;
234-
} else {
235-
return true;
236-
}
237-
};
238-
ncp_options.filter = ncp_filter_function;
221+
/**
222+
* Recursively copies files and directories from a source path to a destination path,
223+
* while optionally filtering out files that match a given regular expression.
224+
*
225+
* @async
226+
* @function _filtered_file_copy
227+
* @param {string} src - The source directory path.
228+
* @param {string} dst - The destination directory path.
229+
* @param {RegExp} [filter_regex] - An optional regular expression to filter out files by name.
230+
* Files matching this regex will be skipped.
231+
* @returns {Promise<void>} Resolves when the copy operation is complete.
232+
*/
233+
async function _filtered_file_copy(src, dst, filter_regex) {
234+
await fsp.mkdir(dst, { recursive: true });
235+
const files = await fsp.readdir(src, { withFileTypes: true });
236+
237+
for (const file of files) {
238+
const src_path = path.join(src, file.name);
239+
const dst_path = path.join(dst, file.name);
240+
241+
if (filter_regex && filter_regex.test(file.name)) {
242+
continue;
239243
}
240-
if (!src || !dst) {
241-
throw new Error('Both src and dst must be given');
244+
245+
if (file.isDirectory()) {
246+
await _filtered_file_copy(src_path, dst_path);
247+
} else if (file.isFile()) {
248+
await fsp.copyFile(src_path, dst_path);
242249
}
243-
ncp(src, dst, ncp_options, callback);
244-
}).then(() => {
245-
// do nothing.
246-
});
250+
}
251+
}
252+
253+
/**
254+
* Copies an entire directory from the source path to the destination path,
255+
* optionally filtering files based on a regular expression.
256+
*
257+
* @param {string} src - The source directory path.
258+
* @param {string} dst - The destination directory path.
259+
* @param {RegExp | string | undefined} [filter_regex] - An optional regular expression or string to filter files.
260+
* @returns {Promise<void>} Resolves when the copy operation is complete.
261+
*/
262+
async function full_dir_copy(src, dst, filter_regex) {
263+
264+
if (!src || !dst) {
265+
throw new Error('Both src and dst must be given');
266+
}
267+
268+
const cp_filter_regex = filter_regex ? new RegExp(filter_regex) : null;
269+
await _filtered_file_copy(src, dst, cp_filter_regex);
247270
}
248271

249272
function tar_pack(tar_file_name, source, ignore_file_changes) {

0 commit comments

Comments
 (0)