Skip to content

Commit

Permalink
Merge pull request #86 from voidcosmos/fix/windows-service-delete
Browse files Browse the repository at this point in the history
Fix issues related to directory deletion in Windows
  • Loading branch information
NyaGarcia authored Nov 18, 2020
2 parents 660f859 + 9f63af4 commit e8b6453
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,10 @@ export class Controller {
this.printStats();
this.printFoldersSection();
})
.catch(() => {
.catch((e) => {
folder.status = 'error-deleting';
this.printFoldersSection();
this.printError(ERROR_MSG.CANT_DELETE_FOLDER);
this.printError(e.message);
});
}

Expand Down
40 changes: 11 additions & 29 deletions src/services/windows-files.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as PATH from 'path';
import * as fs from 'fs';
import { normalize } from 'path';
import { rmdir } from 'fs';
import * as getSize from 'get-folder-size';

import { FileService, StreamService } from '@core/services';
Expand All @@ -14,7 +14,7 @@ export class WindowsFilesService extends FileService {
}

getFolderSize(path: string): Observable<number> {
return Observable.create(observer => {
return new Observable(observer => {
getSize(path, (err, size) => {
if (err) {
throw err;
Expand All @@ -30,39 +30,21 @@ export class WindowsFilesService extends FileService {

const excludeWords = exclude ? exclude.join(' ') : '';

const binPath = PATH.normalize(`${__dirname}/../bin/windows-find`);
const binPath = normalize(`${__dirname}/../bin/windows-find`);
const args = [path, target, excludeWords];

const child = spawn(binPath, args);
return this.streamService.getStream(child);
}

deleteDir(path: string): Promise<{}> {
deleteDir(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
const files = this.getDirectoryFiles(path);

this.removeDirectoryFiles(path, files);
try {
fs.rmdirSync(path);
} catch (err) {
return reject(err);
}
resolve();
});
}

private getDirectoryFiles(dir: string) {
return fs.readdirSync(dir);
}

private removeDirectoryFiles(dir: string, files: string[]): void {
files.map(file => {
const path = PATH.join(dir, file);
if (fs.statSync(path).isDirectory()) {
this.deleteDir(path);
} else {
fs.unlinkSync(path);
}
rmdir(path, { recursive: true }, err => {
if (err) {
reject(err);
}
resolve(true);
});
});
}
}

0 comments on commit e8b6453

Please sign in to comment.