From ee4d627285df00f8d4d052597aeb46d6509202b0 Mon Sep 17 00:00:00 2001 From: Joaquin Fernandez Campo Date: Thu, 11 Jul 2024 16:02:07 +0100 Subject: [PATCH] Replace now checks file by file --- src/helpers.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 06932908..0cf45f70 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -81,30 +81,48 @@ export async function copy(src, dest, isDirectory, file) { const deleteOrphaned = isDirectory && file.deleteOrphaned const exclude = file.exclude - const filterFunc = (file) => { + const filterFunc = (srcFile, destFile) => { + core.debug(`Filtering file ${ srcFile } to ${ destFile }`) + if (file.replace === false) { + // Directories are always copied + try { + if (fs.lstatSync(destFile).isDirectory()) { + core.debug(`Dest File ${ destFile } already exists and is a directory`) + return true + } + } catch (error) { + core.debug(`Destination file or directory ${ destFile } does not exist`) + return true + } + + if (fs.existsSync(destFile)) { + core.debug(`File ${ destFile } already exists and 'replace' option is set to false`) + return false + } + } if (exclude !== undefined) { // Check if file-path is one of the present filepaths in the excluded paths // This has presedence over the single file, and therefore returns before the single file check let filePath = '' - if (file.endsWith('/')) { + if (srcFile.endsWith('/')) { // File item is a folder - filePath = file + filePath = srcFile } else { // File item is a file - filePath = file.split('\/').slice(0, -1).join('/') + '/' + filePath = srcFile.split('\/').slice(0, -1).join('/') + '/' } if (exclude.includes(filePath)) { - core.debug(`Excluding file ${ file } since its path is included as one of the excluded paths.`) + core.debug(`Excluding file ${ srcFile } since its path is included as one of the excluded paths.`) return false } // Or if the file itself is in the excluded files - if (exclude.includes(file)) { - core.debug(`Excluding file ${ file } since it is explicitly added in the exclusion list.`) + if (exclude.includes(srcFile)) { + core.debug(`Excluding file ${ srcFile } since it is explicitly added in the exclusion list.`) return false } } @@ -130,7 +148,7 @@ export async function copy(src, dest, isDirectory, file) { } } else { core.debug(`Copy ${ src } to ${ dest }`) - await fs.copy(src, dest, file.exclude !== undefined && { filter: filterFunc }) + await fs.copy(src, dest,{ filter: filterFunc }) }