All files / src/macOS macOS.js

100% Statements 102/102
100% Branches 67/67
100% Functions 49/49
100% Lines 90/90

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 2761x 1x       1x   29x   1x 28x   1x   1x   1x   1x     5x               12x       7x             425x     224x     19x     41x 41x     41x       6x 224x     1x 56x 73x   56x       1x 4x       28x         28x         28x         28x         10x         29x         28x         5x         9x         19x         10x         10x         1x         10x         9x         4x           4x 4x 2x               5x 5x 3x             4x         10x         32x         10x       4x   718x 90x 90x       628x 213x 213x 3x   213x       415x 15x 3x 3x 132x       15x 15x         400x           4x 6x 6x   55x     6x   54x   1955x 1955x   54x 54x   54x 1380x       54x         6x   72x     39x     54x 18x 18x       6x        
import F from '../functional';
import { hasSubstr, getYesNo, emptyDevice, emptyVolume } from '../utilities';
const { each } = F;
const { filter } = F.R;
 
export const COMMAND = 'diskutil info -all';
 
export const getMacOSBytes = (str) => parseInt(str.match(/\((\d+) Bytes\)/)[1]);
 
export const macOSFS = (fs) => {
  switch(fs) {
  case 'ExFAT':
    return 'ExFAT';
  case 'MS-DOS':
    return 'FAT';
  case 'MS-DOS FAT12':
    return 'FAT12';
  case 'MS-DOS FAT16':
    return 'FAT16';
  case 'MS-DOS FAT32':
  case 'fat32':
    return 'FAT32';
  case 'HFS+':
  case 'Case-sensitive HFS+':
  case 'hfsx':
  case 'Case-sensitive Journaled HFS+':
  case 'jhfsx':
  case 'Journaled HFS+':
  case 'jhfs+':
    return 'HFS+';
  case 'Free Space':
  case 'free':
  default:
    return null;
  }
};
 
/* lower order functions for parseMacOS */
 
// Tells you whether this is a volume or a device
export const nodeType = (node) => node.space ? 'volume' : 'device';
 
// Gets target device / volume for parseMacOSToProps
export const getPropsTarget = (acc, [devid, id]) => id ? acc.devices[devid].volumes[id] : acc.devices[devid];
 
// Adds an empty device to accumulator
export const addEmptyDevice = (acc) => (id) => { acc.devices[id] = emptyDevice(); };
 
// Adds an empty volume to its parent device
export const addEmptyVolumeToDevice = (device) => (id) => {
  device.volumes = device.volumes
    ? device.volumes
    : {};
  device.volumes[id] = emptyVolume();
};
 
// Adds an empty device / volume based off wether a volume id is provided
export const addEmptyNode = (addEmptyDevice, addEmptyVolumeToDevice) =>
  (acc, [devid, id]) => id ? addEmptyVolumeToDevice(acc.devices[devid])(id) : addEmptyDevice(acc)(devid);
 
// Finds the device id as well as the volume id (where applicable) from the provided input lines
export const parseNodeId = (acc, lines) => {
  const id = lines.find((l) => l.match('Device Identifier')).match(/:\s+(.*)/)[1];
  const devid = Object.keys(acc.devices).find((dev) => id.match(`^${dev}`));
 
  return devid ? [ devid, id ] : [ id, undefined ];
};
 
// Maps received line to a property on the node
export const parseMacOSToProps = (macOSFS, getMacOSBytes) => {
  const PROPERTY_MAP = {
    'Device Identifier': {
      target: 'dual',
      key: 'id',
      mapper: (node, value) => { node.id = value; },
    },
    'Device Node': {
      target: 'dual',
      key: 'node',
      mapper: (node, value) => { node.node = value; },
    },
    'Whole': {
      target: 'dual',
      key: 'whole',
      mapper: (node, value) => { node.whole = getYesNo(value); },
    },
    'Part of Whole': {
      target: 'dual',
      key: 'parent',
      mapper: (node, value) => { node.parent = value; },
    },
    'Device / Media Name': {
      target: 'dual',
      key: 'description',
      mapper: (node, value) => { node.description = value; },
    },
    'Volume Name': {
      target: 'dual',
      key: 'name',
      mapper: (node, value) => { node.name = hasSubstr(value, 'Not applicable') ? null : value; },
    },
    'Mounted': {
      target: 'dual',
      key: 'mounted',
      mapper: (node, value) => { node.mounted = !hasSubstr(value, 'Not applicable'); },
    },
    'Mount Point': {
      target: 'dual',
      key: 'mountPoint',
      mapper: (node, value) => { node.mountPoint = hasSubstr(value, 'Not applicable') ? null : value; },
    },
    'File System Personality': {
      target: 'volume',
      key: 'fs',
      mapper: (node, value) => { node.fs = macOSFS(value); },
    },
    'Partition Type': {
      target: 'volume',
      key: 'partitionType',
      mapper: (node, value) => { node.partitionType = value; },
    },
    'Protocol': {
      target: 'device',
      key: 'protocol',
      mapper: (node, value) => { node.protocol = value; },
    },
    'Disk Size': {
      target: 'device',
      key: 'size',
      mapper: (node, value) => { node.size = getMacOSBytes(value); },
    },
    'Total Size': {
      target: 'device',
      key: 'size',
      mapper: (node, value) => { node.size = node.size || getMacOSBytes(value); },
    },
    'Device Block Size': {
      target: 'device',
      key: 'blockSize',
      mapper: (node, value) => { node.blockSize = parseInt(value.match(/\d+/)[0]); },
    },
    'Volume Total Space': {
      target: 'volume',
      key: 'space.total',
      mapper: (node, value) => { node.space.total = getMacOSBytes(value); },
    },
    'Volume Used Space': {
      target: 'volume',
      key: 'space.used',
      mapper: (node, value) => { node.space.used = getMacOSBytes(value); },
    },
    'Volume Available Space': {
      target: 'volume',
      key: 'space.available',
      mapper: (node, value) => {
        node.space.available = getMacOSBytes(value);
        if(node.space.total !== null && node.space.used === null){
          node.space.used = node.space.total - node.space.available;
        }
      },
    },
    'Volume Free Space': {
      target: 'volume',
      key: 'space.available',
      mapper: (node, value) => {
        node.space.available = getMacOSBytes(value);
        if(node.space.total !== null && node.space.used === null){
          node.space.used = node.space.total - node.space.available;
        }
      },
    },
    'Allocation Block Size': {
      target: 'volume',
      key: 'blockSize',
      mapper: (node, value) => { node.blockSize = parseInt(value.match(/\d+/)[0]); },
    },
    'Read-Only Media': {
      target: 'device',
      key: 'readOnly',
      mapper: (node, value) => { node.readOnly = getYesNo(value); },
    },
    'Read-Only Volume': {
      target: 'dual',
      key: 'readOnly',
      mapper: (node, value) => { node.readOnly = hasSubstr(value, 'Not applicable (not mounted)') ? null : getYesNo(value); },
    },
    'Removable Media': {
      target: 'device',
      key: 'removable',
      mapper: (node, value) => { node.removable = value === 'Fixed'; },
    },
  };
 
  return (node, key, value) => {
    // Property ought to be mapped on current node
    if(PROPERTY_MAP[key] && PROPERTY_MAP[key].target === nodeType(node)){
      PROPERTY_MAP[key].mapper(node, value);
      return node;
    }
 
    // Property ought to be mapped on both device and volume
    if(PROPERTY_MAP[key] && PROPERTY_MAP[key].target === 'dual'){
      PROPERTY_MAP[key].mapper(node, value);
      if(node.volumes && node.volumes[node.id]){
        PROPERTY_MAP[key].mapper(node.volumes[node.id], value);
      }
      return node;
    }
 
    // Property ought to be mapped on volume but current node is a device => dual
    if(PROPERTY_MAP[key] && PROPERTY_MAP[key].target === 'volume' && nodeType(node) === 'device'){
      if(!(node.volumes && node.volumes[node.id])){
        addEmptyVolumeToDevice(node)(node.id);
        each(
          ({ target, key }) => { if(target === 'dual') node.volumes[node.id][key] = node[key]; },
          PROPERTY_MAP
        );
      }
      PROPERTY_MAP[key].mapper(node.volumes[node.id], value);
      return node;
    }
 
    // Property ought to be mapped on device but current node is a volume
    // or property is not in PROPERTY_MAP
    return node;
  };
};
 
 
// Parses output of COMMAND to an object
export const parseMacOS = (getPropsTarget, addEmptyNode, parseNodeId, parseMacOSToProps) =>
  (userFilter) => (output) => {
    const entries = output
      .split(/\*{10}/) // Split per entry
      .filter((s) => s.trim()); // Remove empty blocks
 
    // reduce input entries to device / volume objects and return accumulator
    const accumulator = entries.reduce(
      (acc, entry) => {
        const lines = entry
          .split('\n') // Split by line
          .map((s) => s.trim()) // Trim whitespace
          .filter((s) => s); // Remove empty lines
 
        const identifiers = parseNodeId(acc, lines);
        addEmptyNode(acc, identifiers);
 
        lines.reduce(
          (target, line) => parseMacOSToProps(target, ...line.split(/:\s+/)),
          getPropsTarget(acc, identifiers)
        );
 
        return acc;
      },
      { devices: {} }
    );
 
    accumulator.devices =
      Object.entries(accumulator.devices)
        .map(([k, dev]) => [k, {
          ...dev,
          volumes: dev.volumes
            ? Object.keys(dev.volumes).map((k) => dev.volumes[k])
            : [],
        }])
        .reduce((acc, [k, dev]) => {
          acc[k] = dev;
          return acc;
        }, {});
 
    // Filter devices according to userFilter
    return ({
      devices: filter(userFilter, accumulator.devices),
    });
  };