1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const tcpPortUsed = require('tcp-port-used');
|
4 | function getPortFromArgs(args) {
|
5 | let port = 9515;
|
6 | if (!args) {
|
7 | return port;
|
8 | }
|
9 | const portRegexp = /--port=(\d*)/;
|
10 | const portArg = args.find(function (arg) {
|
11 | return portRegexp.test(arg);
|
12 | });
|
13 | if (portArg) {
|
14 | port = parseInt(portRegexp.exec(portArg)[1]);
|
15 | }
|
16 | return port;
|
17 | }
|
18 | process.env.PATH = path.join(__dirname, 'chromedriver') + path.delimiter + process.env.PATH;
|
19 | exports.path = process.platform === 'win32' ? path.join(__dirname, 'chromedriver', 'chromedriver.exe') : path.join(__dirname, 'chromedriver', 'chromedriver');
|
20 | exports.version = '91.0.4472.101';
|
21 | exports.start = function (args, returnPromise) {
|
22 | let command = exports.path;
|
23 | if (!fs.existsSync(command)) {
|
24 | console.log('Could not find chromedriver in default path: ', command);
|
25 | console.log('Falling back to use global chromedriver bin');
|
26 | command = process.platform === 'win32' ? 'chromedriver.exe' : 'chromedriver';
|
27 | }
|
28 | const cp = require('child_process').spawn(command, args);
|
29 | cp.stdout.pipe(process.stdout);
|
30 | cp.stderr.pipe(process.stderr);
|
31 | exports.defaultInstance = cp;
|
32 | if (!returnPromise) {
|
33 | return cp;
|
34 | }
|
35 | const port = getPortFromArgs(args);
|
36 | const pollInterval = 100;
|
37 | const timeout = 10000;
|
38 | return tcpPortUsed.waitUntilUsed(port, pollInterval, timeout)
|
39 | .then(function () {
|
40 | return cp;
|
41 | });
|
42 | };
|
43 | exports.stop = function () {
|
44 | if (exports.defaultInstance != null) {
|
45 | exports.defaultInstance.kill();
|
46 | }
|
47 | };
|