Skip to content

Commit

Permalink
fix(bot): we now delete old file when overwriting a bot (#11371)
Browse files Browse the repository at this point in the history
* fix(bot): fix unmounting bot before writing files when overwriting

* fix(bot): we now delete old file when overwriting a bot
  • Loading branch information
laurentlp committed Feb 1, 2022
1 parent ea97ccb commit b0bf68e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/bp/src/core/bots/bot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import path from 'path'
import replace from 'replace-in-file'
import tmp from 'tmp'
import { VError } from 'verror'
import { findDeletedFiles } from './utils'

const BOT_DIRECTORIES = ['actions', 'flows', 'entities', 'content-elements', 'intents', 'qna']
const BOT_CONFIG_FILENAME = 'bot.config.json'
Expand Down Expand Up @@ -288,6 +289,16 @@ export class BotService {

const folder = await this._validateBotArchive(tmpDir.name)

// Check for deleted file upon overwriting
if (allowOverwrite) {
const files = await this.ghostService.forBot(botId).directoryListing('/')
const deletedFiles = await findDeletedFiles(files, folder)

for (const file of deletedFiles) {
await this.ghostService.forBot(botId).deleteFile('/', file)
}
}

if (await this.botExists(botId)) {
await this.unmountBot(botId)
}
Expand Down
19 changes: 19 additions & 0 deletions packages/bp/src/core/bots/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { existsSync } from 'fs'
import { join } from 'path'

/**
* Finds files that were deleted in a given folder.
* @param files A list of files (relative path)
* @param folder A folder to walk through and test if files were deleted from it
* @returns A list of deleted files (relative path)
*/
export const findDeletedFiles = async (files: string[], folder: string): Promise<string[]> => {
const deletedFiles: string[] = []
for (const f of files) {
const path = join(folder, f)
if (!existsSync(path)) {
deletedFiles.push(f)
}
}
return deletedFiles
}

0 comments on commit b0bf68e

Please sign in to comment.