Skip to content

Commit

Permalink
feat: add -y flag to upgrade, closes NodeBB#13023
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Jan 6, 2025
1 parent 238a3ed commit 4f682a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ program
.option('--log-level <level>', 'Default logging level to use', 'info')
.option('--config <value>', 'Specify a config file', 'config.json')
.option('-d, --dev', 'Development mode, including verbose logging', false)
.option('-l, --log', 'Log subprocess output to console', false);
.option('-l, --log', 'Log subprocess output to console', false)
.option('-y, --unattended', 'Answer yes to any prompts, like plugin upgrades', false);

// provide a yargs object ourselves
// otherwise yargs will consume `--help` or `help`
Expand Down Expand Up @@ -294,6 +295,7 @@ program
].join('\n')}`);
})
.action((scripts, options) => {
options.unattended = program.opts().unattended;
if (program.opts().dev) {
process.env.NODE_ENV = 'development';
global.env = 'development';
Expand All @@ -308,7 +310,8 @@ program
.alias('upgradePlugins')
.description('Upgrade plugins')
.action(() => {
require('./upgrade-plugins').upgradePlugins((err) => {
const { unattended } = program.opts();
require('./upgrade-plugins').upgradePlugins(unattended, (err) => {
if (err) {
throw err;
}
Expand Down
24 changes: 13 additions & 11 deletions src/cli/upgrade-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async function checkPlugins() {
return upgradable;
}

async function upgradePlugins() {
async function upgradePlugins(unattended = false) {
try {
const found = await checkPlugins();
if (found && found.length) {
Expand All @@ -132,16 +132,18 @@ async function upgradePlugins() {
console.log(chalk.green('\nAll packages up-to-date!'));
return;
}

prompt.message = '';
prompt.delimiter = '';

prompt.start();
const result = await prompt.get({
name: 'upgrade',
description: '\nProceed with upgrade (y|n)?',
type: 'string',
});
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)) {
console.log('\nUpgrading packages...');
Expand Down
10 changes: 5 additions & 5 deletions src/cli/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const steps = {
},
plugins: {
message: 'Checking installed plugins for updates...',
handler: async function () {
handler: async function (options) {
await require('../database').init();
await upgradePlugins();
await upgradePlugins(options.unattended);
},
},
schema: {
Expand All @@ -45,14 +45,14 @@ const steps = {
},
};

async function runSteps(tasks) {
async function runSteps(tasks, options) {
try {
for (let i = 0; i < tasks.length; i++) {
const step = steps[tasks[i]];
if (step && step.message && step.handler) {
process.stdout.write(`\n${chalk.bold(`${i + 1}. `)}${chalk.yellow(step.message)}`);
/* eslint-disable-next-line */
await step.handler();
await step.handler(options);
}
}
const message = 'NodeBB Upgrade Complete!';
Expand Down Expand Up @@ -95,7 +95,7 @@ async function runUpgrade(upgrades, options) {
options.plugins || options.schema || options.build) {
tasks = tasks.filter(key => options[key]);
}
await runSteps(tasks);
await runSteps(tasks, options);
return;
}

Expand Down

0 comments on commit 4f682a3

Please sign in to comment.