Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,19 @@ describe('dialog operations', () => {
expect(dialogFileLength(proj.files)).toEqual(dialogsFilesCount - 1);
});
});

describe('deleteAllFiles', () => {
const locationRef: LocationRef = {
storageId: 'default',
path: copyDir,
};

it('should copy and then delete successfully', async () => {
const newBotProject = await proj.copyTo(locationRef);
await newBotProject.init();
const project: { [key: string]: any } = newBotProject.getProject();
expect(project.files.length).toBe(10);
await newBotProject.deleteAllFiles();
expect(fs.existsSync(copyDir)).toBe(false);
});
});

This file was deleted.

24 changes: 4 additions & 20 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ export class BotProject {

public async deleteAllFiles(): Promise<boolean> {
try {
await this.deleteFilesFromBottomToUp(this.dir);
await this.fileStorage.rmDir(this.dir);
await this.fileStorage.rmrfDir(this.dir);
const projectId = await BotProjectService.getProjectIdByPath(this.dir);
if (projectId) {
await this.removeLocalRuntimeData(projectId);
Expand Down Expand Up @@ -356,31 +355,16 @@ export class BotProject {
}
}

private async deleteFilesFromBottomToUp(path) {
const files = await this.fileStorage.readDir(path);

for (let i = 0; i < files.length; i++) {
const curPath = Path.join(path, files[i]);
const childStat = await this.fileStorage.stat(curPath);
if (childStat.isDir && curPath.startsWith(this.dir)) {
await this.deleteFilesFromBottomToUp(curPath);
await this.fileStorage.rmDir(curPath);
} else {
await this.fileStorage.removeFile(curPath);
}
}
}

Comment thread
boydc2014 marked this conversation as resolved.
Outdated
private _cleanUp = async (relativePath: string) => {
const absolutePath = `${this.dir}/${relativePath}`;
const dirPath = Path.dirname(absolutePath);
await this._removeEmptyFolderFromBottomToUp(dirPath);
await this._removeEmptyFolderFromBottomToUp(dirPath, this.dataDir);
};

private _removeEmptyFolderFromBottomToUp = async (folderPath: string) => {
private _removeEmptyFolderFromBottomToUp = async (folderPath: string, prefix: string) => {
let currentFolder = folderPath;
//make sure the folder to delete is in current project
while (currentFolder.startsWith(this.dataDir)) {
while (currentFolder.startsWith(prefix)) {
await this._removeEmptyFolder(currentFolder);
currentFolder = Path.dirname(currentFolder);
}
Expand Down
Loading