From b714847684edc816ce256742abfd6ef7d658429f Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 13 Aug 2024 00:00:54 +0800 Subject: [PATCH] fix(cli): use temp file to prevent empty config --- packages/core/src/command/index.ts | 4 ++-- packages/loader/src/shared.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/src/command/index.ts b/packages/core/src/command/index.ts index 5d3b8bcf9..3d8266eed 100644 --- a/packages/core/src/command/index.ts +++ b/packages/core/src/command/index.ts @@ -1,5 +1,5 @@ import { Awaitable, defineProperty, Time } from 'cosmokit' -import { Bot, h, Schema, Universal } from '@satorijs/core' +import { Bot, Fragment, h, Schema, Universal } from '@satorijs/core' import { Command } from './command' import { Argv } from './parser' import validate from './validate' @@ -25,7 +25,7 @@ declare module '../context' { 'command-updated'(command: Command): void 'command-removed'(command: Command): void 'command-error'(argv: Argv, error: any): void - 'command/before-execute'(argv: Argv): Awaitable + 'command/before-execute'(argv: Argv): Awaitable 'command/before-attach-channel'(argv: Argv, fields: Set): void 'command/before-attach-user'(argv: Argv, fields: Set): void } diff --git a/packages/loader/src/shared.ts b/packages/loader/src/shared.ts index 3cfe1f259..f8f60dff8 100644 --- a/packages/loader/src/shared.ts +++ b/packages/loader/src/shared.ts @@ -254,10 +254,11 @@ export abstract class Loader { throw new Error(`cannot overwrite readonly config`) } if (this.mime === 'application/yaml') { - await fs.writeFile(this.filename, yaml.dump(this.config)) + await fs.writeFile(this.filename + '.tmp', yaml.dump(this.config)) } else if (this.mime === 'application/json') { - await fs.writeFile(this.filename, JSON.stringify(this.config, null, 2)) + await fs.writeFile(this.filename + '.tmp', JSON.stringify(this.config, null, 2)) } + await fs.rename(this.filename + '.tmp', this.filename) if (!silent) this.app.emit('config') }