Node.js based forum software built for the modern web
fix: don't prompt for plugin upgrades when stdin is not a TTY (#14523)
Enes Uysal091a50dedbbf
diff --git a/src/cli/upgrade-plugins.js b/src/cli/upgrade-plugins.jsindex fa3a7249ef..aaf5d116a9 100644--- a/src/cli/upgrade-plugins.js+++ b/src/cli/upgrade-plugins.js@@ -120,6 +120,25 @@ async function checkPlugins() {return upgradable;}+async function getUpgradeConfirmation(unattended) {+ if (unattended) {+ return true;+ }+ if (!process.stdin.isTTY) {+ return false;+ }+ prompt.message = '';+ prompt.delimiter = '';++ prompt.start();+ const result = await prompt.get({+ name: 'upgrade',+ description: '\nProceed with upgrade (y|n)?',+ type: 'string',+ });+ return ['y', 'Y', 'yes', 'YES'].includes(result.upgrade);+}+async function upgradePlugins(unattended = false) {try {const found = await checkPlugins();@@ -132,20 +151,8 @@ async function upgradePlugins(unattended = false) {console.log(chalk.green('\nAll packages up-to-date!'));return;}- let result = { upgrade: 'y' };- if (!unattended) {- prompt.message = '';- prompt.delimiter = '';-- prompt.start();- result = await prompt.get({- name: 'upgrade',- description: '\nProceed with upgrade (y|n)?',- type: 'string',- });- }- if (['y', 'Y', 'yes', 'YES'].includes(result.upgrade)) {+ if (await getUpgradeConfirmation(unattended)) {console.log('\nUpgrading packages...');const args = packageManagerInstallArgs.concat(found.map(suggestObj => `${suggestObj.name}@${suggestObj.suggested}`));const options = { stdio: 'ignore' };@@ -162,4 +169,5 @@ async function upgradePlugins(unattended = false) {}}+exports.getUpgradeConfirmation = getUpgradeConfirmation;exports.upgradePlugins = upgradePlugins;diff --git a/test/upgrade-plugins.js b/test/upgrade-plugins.jsnew file mode 100644index 0000000000..10bc5265a8--- /dev/null+++ b/test/upgrade-plugins.js@@ -0,0 +1,42 @@+'use strict';++const assert = require('assert');++const prompt = require('prompt');++const { getUpgradeConfirmation } = require('../src/cli/upgrade-plugins');++describe('Upgrade plugins', () => {+ describe('getUpgradeConfirmation()', () => {+ const originalIsTTY = process.stdin.isTTY;+ const originalGet = prompt.get;++ afterEach(() => {+ process.stdin.isTTY = originalIsTTY;+ prompt.get = originalGet;+ });++ it('should confirm without prompting when unattended', async () => {+ prompt.get = async () => assert.fail('prompt should not be called when unattended');+ assert.strictEqual(await getUpgradeConfirmation(true), true);+ });++ it('should decline without prompting when stdin is not a TTY', async () => {+ process.stdin.isTTY = false;+ prompt.get = async () => assert.fail('prompt should not be called without a TTY');+ assert.strictEqual(await getUpgradeConfirmation(false), false);+ });++ it('should prompt and confirm on a yes answer when stdin is a TTY', async () => {+ process.stdin.isTTY = true;+ prompt.get = async () => ({ upgrade: 'y' });+ assert.strictEqual(await getUpgradeConfirmation(false), true);+ });++ it('should prompt and decline on answers other than yes', async () => {+ process.stdin.isTTY = true;+ prompt.get = async () => ({ upgrade: 'n' });+ assert.strictEqual(await getUpgradeConfirmation(false), false);+ });+ });+});