Skip to content

Commit

Permalink
fix(cli): use temp file to prevent empty config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 12, 2024
1 parent f58c078 commit 2b007fe
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/loader/src/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from '@cordisjs/core'
import { dirname, extname, resolve } from 'node:path'
import { access, constants, readdir, readFile, stat, writeFile } from 'node:fs/promises'
import { access, constants, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { remove } from 'cosmokit'
import * as yaml from 'js-yaml'
Expand Down Expand Up @@ -64,10 +64,11 @@ export class LoaderFile {
throw new Error(`cannot overwrite readonly config`)
}
if (this.type === 'application/yaml') {
await writeFile(this.name, yaml.dump(config, { schema }))
await writeFile(this.name + '.tmp', yaml.dump(config, { schema }))
} else if (this.type === 'application/json') {
await writeFile(this.name, JSON.stringify(config, null, 2))
await writeFile(this.name + '.tmp', JSON.stringify(config, null, 2))
}
await rename(this.name + '.tmp', this.name)
}

write(config: EntryOptions[]) {
Expand Down

0 comments on commit 2b007fe

Please sign in to comment.