Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ user/repo:

> **Note:** the exclude file path is relative to the source path

### Include files that match a specific pattern

Using the includeFilePatterns you can filter the list of files that are synchronized to those that match the regex:

```yml
user/repo:
- source: workflows
dest: .github/workflows/
includeFilePatterns: "*.yml"
```

### Exclude files that match a specific pattern


Using the excludeFilePatterns you can filter out the list of files that are synchronized to those that match the regex:

```yml
user/repo:
- source: workflows
dest: .github/workflows/
excludeFilePatterns: "*.md"
```

### Don't replace existing file(s)

By default if a file already exists in the target repository, it will be replaced. You can change this behaviour by setting the `replace` option to `false`:
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ const parseFiles = (files) => {
template: item.template === undefined ? TEMPLATE_DEFAULT : item.template,
replace: item.replace === undefined ? REPLACE_DEFAULT : item.replace,
deleteOrphaned: item.deleteOrphaned === undefined ? DELETE_ORPHANED_DEFAULT : item.deleteOrphaned,
excludeFilePatterns: item.excludeFilePatterns === undefined ? [] : item.excludeFilePatterns,
includeFilePatterns: item.includeFilePatterns === undefined ? [] : item.includeFilePatterns,
exclude: parseExclude(item.exclude, item.source)
}
}
Expand Down
60 changes: 52 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,74 @@ 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
}
}

// Check if the file matches any of the excludeFilePatterns
if (file.excludeFilePatterns.length > 0) {
for (const pattern of file.excludeFilePatterns) {
if (srcFile.match(pattern)) {
core.debug(`Excluding file ${ srcFile } since it matches the excludeFilePattern ${ pattern }.`)
return false
}
}
}

// Check if the file matches any of the includeFilePatterns
if (file.includeFilePatterns.length > 0) {
let matches = false
for (const pattern of file.includeFilePatterns) {
if (srcFile.match(pattern)) {
matches = true
break
}
}

if (!matches) {
core.debug(`Excluding file ${ srcFile } since it does not match any of the includeFilePatterns.`)
return false
}
}
Expand All @@ -130,7 +174,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 })
}


Expand Down