refactor(core): Delete duplicate code across all commands (#5452)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-02-10 14:59:20 +01:00
committed by GitHub
parent 8494c97821
commit 5194513850
26 changed files with 979 additions and 1345 deletions

View File

@@ -1,16 +1,12 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable no-console */
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import type { IDataObject } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow';
import * as Db from '@/Db';
import { BaseCommand } from '../BaseCommand';
import { getLogger } from '@/Logger';
export class UpdateWorkflowCommand extends Command {
export class UpdateWorkflowCommand extends BaseCommand {
static description = 'Update workflows';
static examples = [
@@ -31,11 +27,7 @@ export class UpdateWorkflowCommand extends Command {
}),
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async run() {
const logger = getLogger();
LoggerProxy.init(logger);
// eslint-disable-next-line @typescript-eslint/no-shadow
const { flags } = this.parse(UpdateWorkflowCommand);
@@ -56,35 +48,32 @@ export class UpdateWorkflowCommand extends Command {
console.info('No update flag like "--active=true" has been set!');
return;
}
if (!['false', 'true'].includes(flags.active)) {
console.info('Valid values for flag "--active" are only "false" or "true"!');
return;
}
updateQuery.active = flags.active === 'true';
try {
await Db.init();
const findQuery: IDataObject = {};
if (flags.id) {
console.info(`Deactivating workflow with ID: ${flags.id}`);
findQuery.id = flags.id;
} else {
console.info('Deactivating all workflows');
findQuery.active = true;
}
await Db.collections.Workflow.update(findQuery, updateQuery);
console.info('Done');
} catch (e) {
console.error('Error updating database. See log messages for details.');
logger.error('\nGOT ERROR');
logger.info('====================================');
logger.error(e.message);
logger.error(e.stack);
this.exit(1);
const findQuery: IDataObject = {};
if (flags.id) {
this.logger.info(`Deactivating workflow with ID: ${flags.id}`);
findQuery.id = flags.id;
} else {
this.logger.info('Deactivating all workflows');
findQuery.active = true;
}
this.exit();
await Db.collections.Workflow.update(findQuery, updateQuery);
this.logger.info('Done');
}
async catch(error: Error) {
this.logger.error('Error updating database. See log messages for details.');
this.logger.error('\nGOT ERROR');
this.logger.error('====================================');
this.logger.error(error.message);
this.logger.error(error.stack!);
}
}