All files / src/platforms windows.ts

100% Statements 55/55
84.21% Branches 16/19
100% Functions 3/3
100% Lines 54/54

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x   1x 1x 1x         1x             1x   8x 8x   8x 8x 8x   1x 1x   1x 1x   6x 5x   1x     8x   8x   8x   8x 8x 8x 8x   8x   200x   120x 120x 120x   120x   24x 24x 24x   24x 24x   24x 24x   24x 24x         80x   24x   24x   24x 24x     24x               24x   24x 24x 24x 24x 24x             8x     1x  
import {Constants} from '../utils/constants';
 
import Drive from '../classes/drive';
import {Utils} from "../utils/utils";
import iconv from 'iconv-lite';
 
/**
 * 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[] = [];
        let buffer = Utils.execute(Constants.WINDOWS_COMMAND);
        
        const cp = Utils.chcp();
        let encoding = '';
        switch (cp) {
            case '65000': // UTF-7
                encoding = 'UTF-7';
                break;
            case '65001': // UTF-8
                encoding = 'UTF-8';
                break;   
            default: // Other Encoding
                if (/^-?[\d.]+(?:e-?\d+)?$/.test(cp)) {
                    encoding = 'cp' + cp;
                } else {
                    encoding = cp;
                }
        }
        buffer = iconv.encode(iconv.decode(buffer, encoding),'UTF-8');
 
        const lines = buffer.toString().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%';
 
                    Eif (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;
    }
 
}