Completed
Push — master ( 5ea146...3327fa )
by Elbert
36s
created

driver.js ➔ ... ➔ ???   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 2
nop 1
dl 0
loc 78
rs 8.9019
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict';
2
3
const driver = options => {
4
  const Wappalyzer = require('./wappalyzer');
5
  const request = require('request');
0 ignored issues
show
Unused Code introduced by
The constant request seems to be never used. Consider removing it.
Loading history...
6
  const fs = require('fs');
7
  const Browser = require('zombie', {
8
    userAgent: options.userAgent
9
  });
10
11
  const json = JSON.parse(fs.readFileSync(__dirname + '/apps.json'));
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
12
13
  return {
14
    quiet: true,
15
16
    analyze: url => {
17
      const wappalyzer = new Wappalyzer();
18
19
      wappalyzer.apps = json.apps;
20
      wappalyzer.categories = json.categories;
21
22
      return new Promise((resolve, reject) => {
23
        wappalyzer.driver.log = (message, source, type) => {
24
          if ( type === 'error' ) {
25
            return reject(message);
26
          }
27
28
          if ( !driver.quiet ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if !driver.quiet is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
29
            console.log('[wappalyzer ' + type + ']', '[' + source + ']', message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
          }
31
        };
32
33
        wappalyzer.driver.displayApps = detected => {
34
          var apps = [];
35
36
          Object.keys(detected).forEach(appName => {
37
            const app = detected[appName];
38
39
            var categories = [];
40
41
            app.props.cats.forEach(id => {
42
              var category = {};
43
44
              category[id] = wappalyzer.categories[id].name;
45
46
              categories.push(category)
47
            });
48
49
            apps.push({
50
              name: app.name,
51
              confidence: app.confidenceTotal.toString(),
52
              version: app.version,
53
              icon: app.props.icon || 'default.svg',
54
              website: app.props.website,
55
              categories
56
            });
57
          });
58
59
          resolve(apps);
60
        };
61
62
        const browser = new Browser();
63
64
        browser.visit(url, error => {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
65
          if ( !browser.resources['0'].response ) {
66
            return reject('No response from server');
67
          }
68
69
          browser.wait(options.maxWait)
70
            .then(() => {
71
              wappalyzer.driver.document = browser.document;
72
73
              const headers = {};
74
75
              browser.resources['0'].response.headers._headers.forEach(header => {
76
                headers[header[0]] = header[1];
77
              });
78
79
              const vars = Object.getOwnPropertyNames(browser.window);
80
              const html = browser.html();
81
82
              const hostname = wappalyzer.parseUrl(url).hostname;
83
84
              wappalyzer.analyze(hostname, url, {
85
                headers,
86
                html,
87
                env: vars
88
              });
89
            })
90
            .catch(error => reject(error));
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
91
        });
92
      });
93
    }
94
  };
95
};
96
97
module.exports = driver;
98