Skip to content

Commit 5a7175e

Browse files
committed
Delete missing files
1 parent 8b92be3 commit 5a7175e

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ This is some content
255255

256256
### Delete orphaned files
257257

258-
With the `deleteOrphaned` option you can choose to delete files in the target repository if they are deleted in the source repository. The option defaults to `false` and only works when [syncing entire directories](#sync-entire-directories):
258+
With the `deleteOrphaned` option you can choose to delete files in the target repository if they are deleted in the
259+
source repository. The option defaults to `false` works either when [syncing entire directories](#sync-entire-directories), or specific
260+
files:
259261

260262
```yml
261263
user/repo:
@@ -264,8 +266,6 @@ user/repo:
264266
deleteOrphaned: true
265267
```
266268

267-
It only takes effect on that specific directory.
268-
269269
### Sync the same files to multiple repositories
270270

271271
Instead of repeating yourself listing the same files for multiple repositories, you can create a group:

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git.js

+7
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ export default class Git {
153153
)
154154
}
155155

156+
async remove(file) {
157+
return execCmd(
158+
`git rm -f "${ file }"`,
159+
this.workingDir
160+
)
161+
}
162+
156163
isOneCommitPush() {
157164
return github.context.eventName === 'push' && github.context.payload.commits.length === 1
158165
}

src/index.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,36 @@ async function run() {
6161
// Loop through all selected files of the source repo
6262
await forEach(item.files, async (file) => {
6363
const fileExists = fs.existsSync(file.source)
64-
if (fileExists === false) return core.warning(`Source ${ file.source } not found`)
6564

6665
const localDestination = `${ git.workingDir }/${ file.dest }`
67-
6866
const destExists = fs.existsSync(localDestination)
69-
if (destExists === true && file.replace === false) return core.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)
67+
let isDirectory
68+
69+
if (fileExists === false) {
70+
// if there is no local file, then we assume it's not a directory
71+
isDirectory = false
72+
73+
if (!file.deleteOrphaned) {
74+
return core.warning(`Source ${ file.source } not found`)
75+
}
7076

71-
const isDirectory = await pathIsDirectory(file.source)
72-
const source = isDirectory ? `${ addTrailingSlash(file.source) }` : file.source
73-
const dest = isDirectory ? `${ addTrailingSlash(localDestination) }` : localDestination
77+
if (destExists === true) {
78+
await git.remove(file.dest)
79+
}
80+
} else {
81+
isDirectory = await pathIsDirectory(file.source)
82+
83+
if (destExists === true && file.replace === false) return core.warning(`File(s) already exist(s) in destination and 'replace' option is set to false`)
7484

75-
if (isDirectory) core.info(`Source is directory`)
85+
const source = isDirectory ? `${ addTrailingSlash(file.source) }` : file.source
86+
const dest = isDirectory ? `${ addTrailingSlash(localDestination) }` : localDestination
7687

77-
await copy(source, dest, isDirectory, file)
88+
if (isDirectory) core.info(`Source is directory`)
7889

79-
await git.add(file.dest)
90+
await copy(source, dest, isDirectory, file)
91+
92+
await git.add(file.dest)
93+
}
8094

8195
// Commit each file separately, if option is set to false commit all files at once later
8296
if (COMMIT_EACH_FILE === true) {

0 commit comments

Comments
 (0)