Skip to content

Commit

Permalink
fix(clean): report failures to remove item (#5601)
Browse files Browse the repository at this point in the history
* fix(clean) report failures to remove

* Rename  variable

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>

* rename variable

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>

* use let

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>

* Rename varialbe to success for clarity.

* Rename varialbe to success for clarity.

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
  • Loading branch information
jcassidyav and rigor789 committed Oct 27, 2021
1 parent fd3b3a8 commit 3902257
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/commands/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export class CleanCommand implements ICommand {
// ignore
}

await this.$projectCleanupService.clean(pathsToClean);
const success = await this.$projectCleanupService.clean(pathsToClean);

spinner.succeed("Project successfully cleaned.");
if (success) {
spinner.succeed("Project successfully cleaned.");
} else {
spinner.fail(`${"Project unsuccessfully cleaned.".red}`);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,13 @@ interface IProjectCleanupService {
* Clean multiple paths
* @param {string[]} pathsToClean
*/
clean(pathsToClean: string[]): Promise<void>;
clean(pathsToClean: string[]): Promise<boolean>;

/**
* Clean a single path
* @param {string} pathToClean
*/
cleanPath(pathToClean: string): Promise<void>;
cleanPath(pathToClean: string): Promise<boolean>;
}

interface IBackup {
Expand Down
30 changes: 22 additions & 8 deletions lib/services/project-cleanup-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@ export class ProjectCleanupService implements IProjectCleanupService {
this.spinner = this.$terminalSpinnerService.createSpinner();
}

public async clean(pathsToClean: string[]): Promise<void> {
public async clean(pathsToClean: string[]): Promise<boolean> {
let success = true;
for (const pathToClean of pathsToClean) {
await this.cleanPath(pathToClean).catch((error) => {
const isCleaned = await this.cleanPath(pathToClean).catch((error) => {
this.$logger.trace(
`Encountered error while cleaning. Error is: ${error.message}.`,
error
);
return false;
});
success = success && isCleaned;
}
return success;
}

public async cleanPath(pathToClean: string): Promise<void> {
public async cleanPath(pathToClean: string): Promise<boolean> {
this.spinner.clear();

let success = true;
let fileType: string;
if (!pathToClean || pathToClean.trim().length === 0) {
this.$logger.trace("cleanPath called with no pathToClean.");
return;
return success;
}

const filePath = path.resolve(this.$projectHelper.projectDir, pathToClean);
Expand All @@ -48,17 +53,26 @@ export class ProjectCleanupService implements IProjectCleanupService {
if (stat.isDirectory()) {
this.$logger.trace(`Path '${filePath}' is a directory, deleting.`);
this.$fs.deleteDirectorySafe(filePath);
this.spinner.succeed(`Cleaned directory ${displayPath}`);
fileType = "directory";
} else {
this.$logger.trace(`Path '${filePath}' is a file, deleting.`);
this.$fs.deleteFile(filePath);
this.spinner.succeed(`Cleaned file ${displayPath}`);
fileType = "file";
}

success = !this.$fs.exists(filePath);
if (success) {
this.spinner.succeed(`Cleaned ${fileType} ${displayPath}`);
} else {
const message = `Failed to Clean ${fileType}`.red;
this.spinner.fail(`${message} ${displayPath}`);
}
return;
return success;
}
this.$logger.trace(`Path '${filePath}' not found, skipping.`);
// this.spinner.text = `Skipping ${displayPath} because it doesn't exist.`;
// this.spinner.info();
return success;
}
}

Expand Down

0 comments on commit 3902257

Please sign in to comment.