All files snapshot.js

85% Statements 34/40
62.5% Branches 5/8
77.78% Functions 7/9
85% Lines 34/40
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 772x   2x 11x 11x 11x 11x 11x 11x     2x               2x 6x 6x     2x 1x       1x 1x     2x 6x 6x                   2x 6x 6x 6x     2x 13x       13x     2x 6x   6x   6x 1x     6x   6x         2x  
const path = require('path');
 
const Snapshot = function(manifest, fs, browser, snapshotDirectory, applicationPath, validity) {
  this.fs = fs;
  this.browser = browser;
  this.validity = validity;
  this.manifest = manifest;
  this.applicationPath = applicationPath;
  this.snapshotDirectory = snapshotDirectory;
}
 
Snapshot.prototype.getPath = function(pathname) {
  const filename = path.basename(pathname) === ''
    ? `${pathname}index.snapshot`
    : `${pathname}.snapshot`;
 
  return path.join(this.snapshotDirectory, filename);
}
 
Snapshot.prototype.exists = function(pathname) {
  const snapshotPath = this.getPath(pathname);
  return this.manifest.exists(snapshotPath);
}
 
Snapshot.prototype.isValid = function(pathname) {
  Iif (this.validity === 0) {
    return true;
  }
 
  const snapshotPath = this.getPath(pathname);
  return this.manifest.isValid(snapshotPath);
}
 
Snapshot.prototype.getApplication = function() {
  try {
    return this.fs.readFileSync(this.applicationPath);
  } catch (error) {
    console.error(error);
    throw new Error(`
      Could not find a build index.html, please run 'yarn build'
      before running create-snapshot-server.
    `);
  }
}
 
Snapshot.prototype.create = async function(pathname) {
  const html = this.getApplication();
  const browser = new this.browser(pathname, html);
  return await browser.execute();
}
 
Snapshot.prototype.getPath = function(pathname) {
  const filename = path.basename(pathname) === ''
  ? `${pathname}index.snapshot`
  : `${pathname}.snapshot`;
 
  return path.join(this.snapshotDirectory, filename);
}
 
Snapshot.prototype.save = function(pathname, contents) {
  const snapshotPath = this.getPath(pathname);
 
  const dirname = path.dirname(snapshotPath)
 
  if (!this.fs.existsSync(dirname)) {
    this.fs.mkdirSync(dirname);
  }
 
  this.manifest.add(snapshotPath);
 
  return this.fs
    .writeFile(snapshotPath, contents)
    .catch(error => console.error(error));
}
 
module.exports = Snapshot;