Skip to content

Commit 8709b6c

Browse files
committed
fix: fail before writing empty yaml files
1 parent f9d6994 commit 8709b6c

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/git.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,14 @@ export class Git {
188188
async writeFile(file: string, data: Record<string, unknown>, unsetBlankAttributes = true): Promise<void> {
189189
let cleanedData = data
190190
if (unsetBlankAttributes) cleanedData = removeBlankAttributes(data, { emptyArrays: true })
191-
if (isEmpty(cleanedData) && file.match(secretFileRegex)) {
192-
// remove empty secrets file which sops can't handle
193-
return this.removeFile(file)
191+
if (isEmpty(cleanedData)) {
192+
if (file.match(secretFileRegex)) {
193+
// remove empty secrets file which sops can't handle
194+
return this.removeFile(file)
195+
} else {
196+
// Other cases of empty content will not generate valid YAML
197+
throw new ValidationError('Empty payload')
198+
}
194199
}
195200
// we also bail when no changes found
196201
const hasDiff = await this.diffFile(file, data)
@@ -199,7 +204,7 @@ export class Git {
199204
const absolutePath = join(this.path, file)
200205
debug(`Writing to file: ${absolutePath}`)
201206
const sortedData = JSON.parse(stringifyJson(data) as string)
202-
const content = isEmpty(sortedData) ? '' : stringifyYaml(sortedData, undefined, 4)
207+
const content = stringifyYaml(sortedData, undefined, 4)
203208
const dir = dirname(absolutePath)
204209
await ensureDir(dir)
205210
await writeFile(absolutePath, content, 'utf8')

0 commit comments

Comments
 (0)