|
3 | 3 |
|
4 | 4 | const fs = require('fs'); // For createWriteStream
|
5 | 5 | const fsp = require('fs/promises');
|
6 |
| -const ncp = require('ncp').ncp; |
7 | 6 | const path = require('path');
|
8 | 7 | const crypto = require('crypto');
|
9 | 8 |
|
@@ -219,31 +218,55 @@ async function file_delete(file_name) {
|
219 | 218 | }
|
220 | 219 | }
|
221 | 220 |
|
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; |
239 | 243 | }
|
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); |
242 | 249 | }
|
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); |
247 | 270 | }
|
248 | 271 |
|
249 | 272 | function tar_pack(tar_file_name, source, ignore_file_changes) {
|
|
0 commit comments