From de961e34e023afcf4fa5c0faeeec69aaa6c3c815 Mon Sep 17 00:00:00 2001 From: Giovanni Bassi Date: Tue, 7 Nov 2023 19:06:37 -0300 Subject: [PATCH] Hide private attributes from exports --- .npmignore | 1 + lib/chromedriver.js | 40 +++++++++++++++++++++++++--------------- package-lock.json | 4 ++-- package.json | 2 +- testStart.js | 14 ++++++++++++++ update.js | 4 ++-- 6 files changed, 45 insertions(+), 20 deletions(-) create mode 100755 testStart.js diff --git a/.npmignore b/.npmignore index bc4f15c..7195c76 100644 --- a/.npmignore +++ b/.npmignore @@ -5,6 +5,7 @@ tmp Dockerfile *.sh testInstall.js +testStart.js update.js *.tgz .vscode diff --git a/lib/chromedriver.js b/lib/chromedriver.js index 7d89b3d..d43d37d 100644 --- a/lib/chromedriver.js +++ b/lib/chromedriver.js @@ -3,23 +3,23 @@ const path = require('path'); const tcpPortUsed = require('tcp-port-used'); function getPortFromArgs(args) { let port = 9515; - if (!args) { + if (!args) return port; - } const portRegexp = /--port=(\d*)/; const portArg = args.find(function (arg) { return portRegexp.test(arg); }); - if (portArg) { + if (portArg) port = parseInt(portRegexp.exec(portArg)[1]); - } return port; } process.env.PATH = path.join(__dirname, 'chromedriver') + path.delimiter + process.env.PATH; -exports.path = process.platform === 'win32' ? path.join(__dirname, 'chromedriver', 'chromedriver.exe') : path.join(__dirname, 'chromedriver', 'chromedriver'); -exports.version = '119.0.6045.105'; -exports.start = function (args, returnPromise) { - let command = exports.path; +const crpath = process.platform === 'win32' ? path.join(__dirname, 'chromedriver', 'chromedriver.exe') : path.join(__dirname, 'chromedriver', 'chromedriver'); +const version = '119.0.6045.105'; +let defaultInstance = null; + +function start(args, returnPromise) { + let command = crpath; if (!fs.existsSync(command)) { console.log('Could not find chromedriver in default path: ', command); console.log('Falling back to use global chromedriver bin'); @@ -28,10 +28,9 @@ exports.start = function (args, returnPromise) { const cp = require('child_process').spawn(command, args); cp.stdout.pipe(process.stdout); cp.stderr.pipe(process.stderr); - exports.defaultInstance = cp; - if (!returnPromise) { + defaultInstance = cp; + if (!returnPromise) return cp; - } const port = getPortFromArgs(args); const pollInterval = 100; const timeout = 10000; @@ -39,9 +38,20 @@ exports.start = function (args, returnPromise) { .then(function () { return cp; }); -}; -exports.stop = function () { - if (exports.defaultInstance != null) { - exports.defaultInstance.kill(); +} + +function stop() { + if (defaultInstance != null) + defaultInstance.kill(); + defaultInstance = null; +} + +module.exports = { + path: crpath, + version, + start, + stop, + get defaultInstance() { + return defaultInstance; } }; diff --git a/package-lock.json b/package-lock.json index f318a2d..1351ee2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "chromedriver", - "version": "119.0.0", + "version": "119.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "chromedriver", - "version": "119.0.0", + "version": "119.0.1", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index a7902cd..3dce5c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromedriver", - "version": "119.0.0", + "version": "119.0.1", "keywords": [ "chromedriver", "selenium" diff --git a/testStart.js b/testStart.js new file mode 100755 index 0000000..2c4edf2 --- /dev/null +++ b/testStart.js @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +"use strict"; +const chromedriver = require('./lib/chromedriver'); + +async function run() { + console.log(`Starting chromedriver. Instance: ${JSON.stringify(chromedriver)}`); + await chromedriver.start(null, true); + console.log(`Started Chromedriver. Instance is null: ${chromedriver.defaultInstance === null}.`); + chromedriver.stop(); + console.log(`Stopped Chromedriver. Instance is null: ${chromedriver.defaultInstance === null}.`); +} + +run(); diff --git a/update.js b/update.js index 73ecdfe..62fa16e 100755 --- a/update.js +++ b/update.js @@ -21,14 +21,14 @@ async function getLatest() { } /* Provided a new Chromedriver version such as 77.0.3865.40: - - update the version inside the ./lib/chromedriver helper file e.g. exports.version = '77.0.3865.40'; + - update the version inside the ./lib/chromedriver helper file e.g. const version = '77.0.3865.40'; - bumps package.json version number - add a git tag using the new node-chromedriver version - add a git commit, e.g. Bump version to 77.0.0 */ async function writeUpdate(newVersion, shouldCommit) { const helper = fs.readFileSync('./lib/chromedriver.js', 'utf8'); - const versionExport = 'exports.version'; + const versionExport = 'const version'; const regex = new RegExp(`^.*${versionExport}.*$`, 'gm'); const updated = helper.replace(regex, `${versionExport} = '${newVersion}';`); const currentMajor = semver.major(currentVersionInPackageJson);