All files / src/windows windows.js

100% Statements 45/45
100% Branches 8/8
100% Functions 7/7
100% Lines 41/41

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 641x 1x     1x         63x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x     3x   3x   3x 17x   3x 3x 3x 12x 12x 12x 74x   12x     3x 12x       3x        
import F from '../functional';
import { emptyDevice, emptyVolume } from '../utilities';
const { filter } = F;
 
export const COMMAND =
  'wmic logicaldisk get ' +
  'Caption,Description,DeviceID,DriveType,FileSystem,FreeSpace,Name,Size,VolumeName ' +
  '/format:csv';
 
export const parseWindowsProps = (acc, { Caption, Description, DeviceID, DriveType, FileSystem, FreeSpace, Name, Size, VolumeName }) => {
  acc.devices[Name] = acc.devices[Name] ? acc.devices[Name] : emptyDevice();
  acc.devices[Name].id = DeviceID;
  acc.devices[Name].whole = true;
  acc.devices[Name].parent = DeviceID;
  acc.devices[Name].node = Caption;
  acc.devices[Name].name = Name;
  acc.devices[Name].size = parseInt(Size) || 0;
  acc.devices[Name].description = Description;
  acc.devices[Name].removable = DriveType === '2';
 
  const volume = emptyVolume();
  volume.id = DeviceID;
  volume.node = DeviceID;
  volume.name = VolumeName;
  volume.parent = DeviceID;
  volume.mounted = true;
  volume.mountPoint = Name;
  volume.fs = FileSystem;
  volume.space.total = parseInt(Size) || 0;
  volume.space.available = parseInt(FreeSpace) || 0;
  volume.space.used = volume.space.total - volume.space.available;
  acc.devices[Name].volumes = [volume];
 
  return acc;
};
 
export const parseWindows = (parseWindowsProps) => (userFilter) => (data) => {
  // fix double \r\r coming from wmic
  data = data.replace(/\r\r/gi, '\r');
  var lines =
    data.split('\r\n')
      .filter((s) => s.trim());
 
  var columns = lines[0].split(',');
  var result = [];
  for(var i = 1; i < lines.length; i++) {
    var values = lines[i].split(',');
    var obj = {};
    values.map((val, j) => {
      obj[columns[j]] = val;
    });
    result.push(obj);
  }
 
  const { devices } = result.reduce(
    (acc, v) => parseWindowsProps(acc, v),
    { devices: {} },
  );
 
  return {
    devices: filter(userFilter, devices), // apply user filter
  };
};