Skip to content

Commit 786b6d8

Browse files
committed
Delete missing files
1 parent 8b92be3 commit 786b6d8

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
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/config.js

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ try {
119119
type: 'boolean',
120120
default: false
121121
}),
122+
DELETE_ORPHANED: getInput({
123+
key: 'DELETE_ORPHANED',
124+
type: 'boolean',
125+
default: false
126+
}),
122127
BRANCH_PREFIX: getInput({
123128
key: 'BRANCH_PREFIX',
124129
default: 'repo-sync/SOURCE_REPO_NAME'

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {
2020
COMMIT_AS_PR_TITLE,
2121
FORK,
2222
REVIEWERS,
23-
TEAM_REVIEWERS
23+
TEAM_REVIEWERS,
2424
} = config
2525

2626
async function run() {
@@ -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)