All files / packages/design-system/scripts vrt.js

0% Statements 0/42
0% Branches 0/8
0% Functions 0/25
0% Lines 0/38

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                                                                                                                                           
const args = require('yargs').argv;
const packageJSON = require('../package.json');
const fs = require('fs');
const compare = require('@salesforce-ux/instant-vrt/compare');
const path = require('path');
const request = require('request');
const Task = require('data.task');
 
// the indexName is really just for local dev
const branch = process.env.TRAVIS_BRANCH || packageJSON.config.search.indexName;
 
const snapPath = args.path;
 
const snapUrl = branch =>
  `${process.env.VRT_HOST}/snapshot/${branch}?useToken=true`;
 
const download = (url, dest) =>
  new Task((rej, res) =>
    request(url)
      .pipe(fs.createWriteStream(dest))
      .on('error', rej)
      .on('finish', () => res(dest))
  );
 
const stat = filepath =>
  new Task((rej, res) =>
    fs.stat(filepath, (err, r) => (err ? rej(err) : res(r)))
  );
 
const statOrDownload = (url, filepath) =>
  stat(filepath)
    .fold(e => download(url, filepath), x => Task.of(filepath))
    .chain(x => x);
 
const readFile = filepath =>
  new Task((rej, res) =>
    fs.readFile(filepath, 'utf-8', (err, contents) =>
      err ? rej(err) : res(contents)
    )
  );
 
const getSnap = () => readFile(snapPath).map(JSON.parse);
 
const getRef = () =>
  Task.of(path.resolve(__dirname, 'snap.json'))
    .chain(filepath => statOrDownload(snapUrl(branch), filepath))
    .chain(readFile)
    .map(JSON.parse);
 
Task.of(snapContents => refContents =>
  Object.keys(snapContents).map(
    k =>
      refContents[k]
        ? Object.assign({ file: k }, compare(refContents[k], snapContents[k]))
        : { file: k, passed: false } // if it doesn't exist, fail it so we can lint
  )
)
  .ap(getSnap())
  .ap(getRef())
  .map(report =>
    report
      .filter(x => !x.passed)
      .map(x => path.basename(x.file, '.html'))
      .join(',')
  )
  .fork(
    e => console.log('*'), // run all if there's an error
    r => console.log(r)
  );