Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mb to gb #18

Merged
merged 7 commits into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ To exit, <kbd>Q</kbd> or <kbd>Ctrl</kbd> + <kbd>c</kbd> if you're brave.
| -D, --delete-all | CURRENTLY DISABLED. Automatically delete all node_modules folders that are found. |
| -e, --show-errors | Show error messages related to the search if any |
| -f, --full | Start searching from the home of the user (example: "/home/user" in linux) |
| -gb | Show folders in Gigabytes instead of Megabytes. |
| -h, --help, ? | Show this help page and exit |
| -v, --version | Show npkill version |

Expand Down
41 changes: 22 additions & 19 deletions src/constants/cli.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ import { ICliOptions } from '../interfaces/cli-options.interface';

export const OPTIONS: ICliOptions[] = [
{
arg: ['-h', '--help', '?'],
description: 'Show this help page, with all options.',
name: 'help',
},
{
arg: ['-v', '--version'],
description: 'Show version.',
name: 'version',
arg: ['-c', '--bg-color'],
description:
'Change row highlight color. Available colors are: blue, cyan, magenta, red, white and yellow. Default is blue.',
name: 'bg-color',
},
{
arg: ['-d', '--directory'],
description:
'Set directory from which to start searching. By default, starting-point is .',
name: 'directory',
},
{
arg: ['-f', '--full'],
description:
'Start searching from the home of the user (example: "/home/user" in linux).',
name: 'full-scan',
},
{
arg: ['-D', '--delete-all'],
description:
Expand All @@ -35,15 +25,28 @@ export const OPTIONS: ICliOptions[] = [
name: 'show-errors',
},
{
arg: ['-c', '--bg-color'],
arg: ['-f', '--full'],
description:
'Change row highlight color. Available colors are: blue, cyan, magenta, red, white and yellow. Default is blue.',
name: 'bg-color',
'Start searching from the home of the user (example: "/home/user" in linux).',
name: 'full-scan',
},
{
arg: ['-gb'],
description: 'Show folder size in Gigabytes',
name: 'gb',
},
{
arg: ['-h', '--help', '?'],
description: 'Show this help page, with all options.',
name: 'help',
},
{
arg: ['-v', '--version'],
description: 'Show version.',
name: 'version',
},
];

export const DEFAULT_COLOR = 'bgBlue';

export const COLORS = {
cyan: 'bgCyan',
magenta: 'bgMagenta',
Expand Down
2 changes: 2 additions & 0 deletions src/constants/main.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const DECIMALS_SIZE = 2;
export const OVERFLOW_CUT_FROM = 8;

export const DEFAULT_CONFIG: IConfig = {
backgroundColor: 'bgBlue',
deleteAll: false,
folderSizeInGb: false,
maxSimultaneousSearch: 6,
showErrors: false,
};
Expand Down
43 changes: 26 additions & 17 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
UI_POSITIONS,
VALID_KEYS,
} from './constants/main.constants';
import { COLORS, DEFAULT_COLOR, OPTIONS } from './constants/cli.constants';
import { COLORS, OPTIONS } from './constants/cli.constants';
import {
ERROR_MSG,
HELP_MSGS,
Expand Down Expand Up @@ -43,9 +43,7 @@ export class Controller {
private nodeFolders: IFolder[] = [];

private cursorPosY: number = MARGINS.ROW_RESULTS_START;
private previousCursorPosY: number = 0;
private scroll: number = 0;
private backgroundColor: string = DEFAULT_COLOR;

private finishSearching$: Subject<boolean> = new Subject<boolean>();

Expand Down Expand Up @@ -95,6 +93,7 @@ export class Controller {
: process.cwd();
if (options['full-scan']) this.folderRoot = this.getUserHomePath();
if (options['show-errors']) this.config.showErrors = true;
if (options['gb']) this.config.folderSizeInGb = true;
if (options['bg-color']) this.setColor(options['bg-color']);
}

Expand Down Expand Up @@ -132,7 +131,7 @@ export class Controller {
}

private setColor(color: string) {
if (this.isValidColor(color)) this.backgroundColor = COLORS[color];
if (this.isValidColor(color)) this.config.backgroundColor = COLORS[color];
}

private isValidColor(color: string) {
Expand Down Expand Up @@ -290,7 +289,15 @@ export class Controller {
});

// Folder size
const folderSize = this.round(folder.size, DECIMALS_SIZE) + ' mb';
let folderSize = `${this.round(folder.size, 3)} gb`;

if (!this.config.folderSizeInGb) {
folderSize = `${this.round(
this.fileService.convertGbToMb(folder.size),
DECIMALS_SIZE,
)} mb`;
}

const folderSizeText = folder.size ? folderSize : '--';
this.printAt(folderSizeText, {
x: this.stdout.columns - MARGINS.FOLDER_SIZE_COLUMN,
Expand Down Expand Up @@ -357,7 +364,7 @@ export class Controller {
const nodeFolder = { path, deleted: false, size: 0 };
this.addNodeFolder(nodeFolder);

this.calculateFolderSize(nodeFolder);
this.calculateFolderStats(nodeFolder);
this.printFoldersSection();
});
}
Expand All @@ -369,16 +376,21 @@ export class Controller {
messages.map(msg => this.printError(msg));
}

private calculateFolderSize(nodeFolder: IFolder): void {
private calculateFolderStats(nodeFolder: IFolder): void {
this.fileService
.getFolderSize(nodeFolder.path)
.subscribe((size: number) => {
nodeFolder.size = +this.round(+size, DECIMALS_SIZE);
.subscribe((size: string) => {
nodeFolder.size = this.transformFolderSize(size);

this.printStats();
this.printFoldersSection();
});
}

private transformFolderSize(size: string): number {
return this.fileService.convertKbToGb(+size);
}

private completeSearch(): void {
this.finishSearching$.next(true);
this.updateStatus(colors.green(INFO_MSGS.SEARCH_COMPLETED));
Expand Down Expand Up @@ -407,15 +419,13 @@ export class Controller {

private moveCursorUp(): void {
if (this.isCursorInUpperTextLimit(this.cursorPosY)) {
this.previousCursorPosY = this.getRealCursorPosY();
this.cursorPosY--;
this.checkCursorScroll();
}
}

private moveCursorDown(): void {
if (this.isCursorInLowerTextLimit(this.cursorPosY)) {
this.previousCursorPosY = this.getRealCursorPosY();
this.cursorPosY++;
this.checkCursorScroll();
}
Expand Down Expand Up @@ -477,12 +487,11 @@ export class Controller {
}

private printStats(): void {
const stats: IStats = this.getStats();
const { totalSpace, spaceReleased } = this.getStats();

const totalSpacePosition = { ...UI_POSITIONS.TOTAL_SPACE };
const spaceReleasedPosition = { ...UI_POSITIONS.SPACE_RELEASED };
const totalSpace = stats.totalSpace.toFixed(DECIMALS_SIZE) + ' mb';
const spaceReleased = stats.spaceReleased.toFixed(DECIMALS_SIZE) + ' mb';

totalSpacePosition.x += INFO_MSGS.TOTAL_SPACE.length;
spaceReleasedPosition.x += INFO_MSGS.SPACE_RELEASED.length;

Expand All @@ -500,8 +509,8 @@ export class Controller {
}, 0);

return {
spaceReleased: this.round(spaceReleased, 2),
totalSpace: this.round(totalSpace, 2),
spaceReleased: `${this.round(spaceReleased, 2)} gb`,
totalSpace: `${this.round(totalSpace, 2)} gb`,
};
}

Expand All @@ -514,7 +523,7 @@ export class Controller {

private printFolderCursor(): void {
this.printAt(
colors[this.backgroundColor](
colors[this.config.backgroundColor](
colors.black(
this.getFolderPathText(
this.nodeFolders[this.cursorPosY - MARGINS.ROW_RESULTS_START],
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface IConfig {
backgroundColor: string;
deleteAll: boolean;
folderSizeInGb: boolean;
maxSimultaneousSearch: number;
showErrors: boolean;
}
5 changes: 4 additions & 1 deletion src/interfaces/file-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Observable } from 'rxjs';

export interface IFileService {
getFileContent(path: string): string;
getFolderSize(path: string): Observable<any>;
listDir(path: string): Observable<{}>;
deleteDir(path: string): Promise<{}>;
convertKbToGb(kb: number): number;
convertBToKb(bytes: number): number;
convertGbToMb(gb: number): number;
getFileContent(path: string): string;
isSafeToDelete(path: string): boolean;
}
4 changes: 2 additions & 2 deletions src/interfaces/stats.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IStats {
spaceReleased: number;
totalSpace: number;
spaceReleased: string;
totalSpace: string;
}
18 changes: 17 additions & 1 deletion src/services/files.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import * as fs from 'fs';

import { DECIMALS_SIZE, TARGET_FOLDER } from '../constants/main.constants';

import { IFileService } from '../interfaces/file-service.interface';
import { Observable } from 'rxjs';
import { TARGET_FOLDER } from '../constants/main.constants';

export abstract class FileService implements IFileService {
abstract getFolderSize(path: string): Observable<any>;
abstract listDir(path: string): Observable<{}>;
abstract deleteDir(path: string): Promise<{}>;

convertKbToGb(kb: number): number {
const factorKbtoGb = 1048576;
return kb / factorKbtoGb;
}

convertBToKb(bytes: number): number {
const factorBtoGb = 1024;
return bytes / factorBtoGb;
}

convertGbToMb(gb: number) {
const factorGbtoMb = 1024;
return gb * factorGbtoMb;
}

getFileContent(path: string): string {
const encoding = 'utf8';
return fs.readFileSync(path, encoding);
Expand Down
2 changes: 1 addition & 1 deletion src/services/linux-files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class LinuxFilesService extends FileService {
}

getFolderSize(path: string): Observable<{}> {
const du = spawn('du', ['-sm', path, '--max-depth', '0']);
const du = spawn('du', ['-s', path]);
const cut = spawn('cut', ['-f', '1']);

du.stdout.pipe(cut.stdin);
Expand Down
7 changes: 1 addition & 6 deletions src/services/windows-files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class WindowsFilesService extends FileService {
if (err) {
throw err;
}
observer.next(this.convertBToMb(size));
observer.next(super.convertBToKb(size));
observer.complete();
});
});
Expand All @@ -44,11 +44,6 @@ export class WindowsFilesService extends FileService {
});
}

private convertBToMb(bytes: number): number {
const factorBtoMb = 1048576;
return bytes / factorBtoMb;
}

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