From 823da1d925f2df18ebdc071347d8fd9d8f583ddd Mon Sep 17 00:00:00 2001 From: Cris Stringfellow <22254235+crislin2046@users.noreply.github.com> Date: Sat, 14 Jan 2023 16:47:40 +0800 Subject: [PATCH] Add gate at start for chrome - closes #139 --- package-lock.json | 12 ++++++++++++ package.json | 1 + src/app.js | 31 ++++++++++++++++++++++++++++++- src/common.js | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 52ebbca..196a4b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "ndx-query": "^1.0.1", "ndx-serializable": "^1.0.0", "node-fetch": "latest", + "ps-list": "^8.1.1", "ukkonen": "^1.4.0", "ws": "latest" }, @@ -2189,6 +2190,17 @@ "node": ">= 0.10" } }, + "node_modules/ps-list": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/ps-list/-/ps-list-8.1.1.tgz", + "integrity": "sha512-OPS9kEJYVmiO48u/B9qneqhkMvgCxT+Tm28VCEJpheTpl8cJ0ffZRRNgS5mrQRTrX5yRTpaJ+hRDeefXYmmorQ==", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", diff --git a/package.json b/package.json index 39c9bb2..7eb6de4 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "ndx-query": "^1.0.1", "ndx-serializable": "^1.0.0", "node-fetch": "latest", + "ps-list": "^8.1.1", "ukkonen": "^1.4.0", "ws": "latest" }, diff --git a/src/app.js b/src/app.js index 925ec1f..4cb00fd 100644 --- a/src/app.js +++ b/src/app.js @@ -1,7 +1,11 @@ import fs from 'fs'; import ChildProcess from 'child_process'; +import readline from 'readline'; +import util from 'util'; +import {stdin as input, stdout as output} from 'process'; import ChromeLauncher from 'chrome-launcher'; +import psList from 'ps-list'; import {DEBUG, sleep, NO_SANDBOX, GO_SECURE} from './common.js'; @@ -56,7 +60,32 @@ async function start() { console.log(`Importing dependencies...`); const {launch:ChromeLaunch} = ChromeLauncher; - await killChrome(); + let chromeOpen = false; + + const list = await psList(); + + chromeOpen = list.some(({name,cmd}) => name.match(/chrome/g) || cmd.match(/chrome/g)); + + if ( chromeOpen ) { + console.info(`Seems Chrome is open`); + if ( DEBUG.askFirst ) { + const rl = readline.createInterface({input, output}); + const question = util.promisify(rl.question).bind(rl); + console.info(`\nIf you don't shut down Chrome and restart it under DiskerNet control + you will not be able to save or serve your archives.\n`); + const answer = await question("Would you like to shutdown Chrome browser now (y/N) ? "); + if ( answer?.match(/^y/i) ) { + await killChrome(); + } else { + console.log(`OK, not shutting it!\n`); + if ( chromeOpen ) { + process.exit(0); + } + } + } else { + await killChrome(); + } + } console.log(`Removing 22120's existing temporary browser cache if it exists...`); if ( fs.existsSync(args.temp_browser_cache()) ) { diff --git a/src/common.js b/src/common.js index 02bc55c..afd4cda 100644 --- a/src/common.js +++ b/src/common.js @@ -4,6 +4,7 @@ import fs from 'fs'; import os from 'os'; export const DEBUG = { + askFirst: true, verboseSlow: false, debug: process.env.DEBUG_22120 || false, checkPred: false