All files / src writeNewResults.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 13/13
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 452x     2x     2x     2x     2x             2x 4x 1x     3x           3x 3x               1x       2x  
import {
  writeFileSync,
} from 'fs';
import {
  isValidLastResults,
} from './TypeGuards/isValidLastResults';
import {
  homedir,
} from 'os';
import {
  join,
} from 'path';
import {
  strings,
} from './strings';
import {
  TUUIDLastResults,
} from './TypeAliases/TUUIDLastResults';
 
export const writeNewResults = (lastResults: TUUIDLastResults) => {
  if (!isValidLastResults(lastResults)) {
    throw new Error(strings.UUID_LAST_RESULTS_INVALID);
  }
 
  const formattedLastResults = {
    clockSequence: Array.prototype.slice.call(lastResults.clockSequence),
    nodeIdentifier: Array.prototype.slice.call(lastResults.nodeIdentifier),
    timestamp: Array.prototype.slice.call(lastResults.timestamp),
  };
 
  try {
    writeFileSync(
      join(homedir(), 'uuid'),
      JSON.stringify(formattedLastResults),
      {
        encoding: 'utf8',
      },
    );
  } catch (e) {
    throw new Error('Could not write results.');
  }
}
 
export default writeNewResults;