All files / src/platforms windows.ts

100% Statements 43/43
100% Branches 14/14
100% Functions 3/3
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901x   1x 1x         1x             1x   4x 4x 4x   4x   4x 4x 4x 4x   4x   100x   60x 60x 60x   60x   12x 12x 12x   12x 12x   12x 12x   12x 12x         40x   12x   12x   12x 8x     12x               12x   12x 12x 12x 12x 12x             4x     1x  
import {Constants} from '../utils/constants';
 
import Drive from '../classes/drive';
import {Utils} from "../utils/utils";
 
/**
 * Class with Windows specific logic to get disk info.
 */
export class Windows {
 
    /**
     * Execute specific Windows command to get disk info.
     *
     * @return {Drive[]} List of drives and their info.
     */
    public static run(): Drive[] {
 
        const drives: Drive[] = [];
        const buffer = Utils.execute(Constants.WINDOWS_COMMAND);
        const lines = buffer.split('\r\r\n');
 
        let newDiskIteration = false;
 
        let caption: string = '';
        let description: string = '';
        let freeSpace: number = 0;
        let size: number = 0;
 
        lines.forEach((value) => {
 
            if (value !== '') {
 
                const tokens = value.split('=');
                const section = tokens[0];
                const data = tokens[1];
 
                switch (section) {
                    case 'Caption':
                        caption = data;
                        newDiskIteration = true;
                        break;
                    case 'Description':
                        description = data;
                        break;
                    case 'FreeSpace':
                        freeSpace = isNaN(parseFloat(data)) ? 0 : +data;
                        break;
                    case 'Size':
                        size = isNaN(parseFloat(data)) ? 0 : +data;
                        break;
                }
 
            } else {
 
                if (newDiskIteration) {
 
                    const used: number = (size - freeSpace);
 
                    let percent = '0%';
 
                    if (size > 0) {
                        percent = Math.round((used / size) * 100) + '%';
                    }
 
                    const d = new Drive(
                        description,
                        size,
                        used,
                        freeSpace,
                        percent,
                        caption);
 
                    drives.push(d);
 
                    newDiskIteration = false;
                    caption = '';
                    description = '';
                    freeSpace = 0;
                    size = 0;
                }
 
            }
 
        });
 
        return drives;
    }
 
}