'use strict';

const fs = require('fs');
const path = require('path');
// eslint-disable-next-line node/no-unpublished-require
const { JSDOM } = require('jsdom');

class DistChecker {
  constructor(distPath) {
    this.distPath = distPath;

    let html = fs.readFileSync(path.join(distPath, 'index.html'), 'utf8');
    this.dom = new JSDOM(html);
    this.scripts = this.dom.window.document.querySelectorAll('script');
    this.links = this.dom.window.document.querySelectorAll('link');
  }

  contains(fileType, token) {
    if (fileType === 'js') {
      for (let element of this.scripts) {
        let src = element.getAttribute('src');
        let content = fs.readFileSync(path.join(this.distPath, src), 'utf8');

        if (content.includes(token)) {
          return true;
        }
      }
    } else if (fileType === 'css') {
      for (let element of this.links) {
        let src = element.getAttribute('href');
        let content = fs.readFileSync(path.join(this.distPath, src), 'utf8');

        if (content.includes(token)) {
          return true;
        }
      }
    }

    return false;
  }
}

module.exports = DistChecker;

@@ -130,17 +130,13 @@
 nst 
-%7B JSDOM %7D
+jsdom
  = r
@@ -153,16 +153,319 @@
 dom');%0A%0A
+class CustomResourceLoader extends jsdom.ResourceLoader %7B%0A  constructor(distPath) %7B%0A    super();%0A    this.distPath = distPath;%0A  %7D%0A%0A  fetch(url) %7B%0A    let content = fs.readFileSync(path.join(this.distPath, url.replace('file://./', '')), 'utf8');%0A    return Promise.resolve(Buffer.from(content));%0A  %7D%0A%7D%0A%0A
 class Di
@@ -532,16 +532,48 @@
 stPath;%0A
+  %7D%0A%0A  _evalHtml(options = %7B%7D) %7B
 %0A    let
@@ -606,16 +606,21 @@
 th.join(
+this.
 distPath
@@ -664,16 +664,22 @@
 m = new 
+jsdom.
 JSDOM(ht
@@ -680,16 +680,25 @@
 DOM(html
+, options
 );%0A    t
@@ -843,35 +843,797 @@
 %0A%0A  
-contains(fileType, token) %7B
+evalScripts(timeout = 15000) %7B%0A    this._evalHtml(%7B%0A      url: %60file://.%60,%0A      runScripts: 'dangerously',%0A      resources: new CustomResourceLoader(this.distPath),%0A    %7D);%0A%0A    // ember-data expects window.crypto to exists however JSDom does not%0A    // impliment this: https://github.com/jsdom/jsdom/issues/1612%0A    this.dom.window.crypto = () =%3E %7B%7D;%0A    return new Promise((resolve, reject) =%3E %7B%0A      // reject if the scripts take longer than 15 seconds to load.%0A      let timeoutId = setTimeout(reject, timeout);%0A%0A      this.dom.window.addEventListener('load', () =%3E %7B%0A        clearTimeout(timeoutId);%0A        return resolve();%0A      %7D);%0A    %7D);%0A  %7D%0A%0A  get window() %7B%0A    return this.dom.window;%0A  %7D%0A%0A  contains(fileType, token) %7B%0A    if (!this.dom) %7B%0A      this._evalHtml(%7B%7D);%0A    %7D%0A
 %0A   

Allow dist-checker to evaluate scripts to test window properties