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,23 +1,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable no-restricted-syntax */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-loop-func */
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import type { INode, INodeCredentialsDetails } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow';
import fs from 'fs';
import glob from 'fast-glob';
import { UserSettings } from 'n8n-core';
import type { EntityManager } from 'typeorm';
import { v4 as uuid } from 'uuid';
import { getLogger } from '@/Logger';
import config from '@/config';
import * as Db from '@/Db';
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
@@ -28,9 +15,7 @@ import { setTagsForImport } from '@/TagHelpers';
import type { ICredentialsDb, IWorkflowToImport } from '@/Interfaces';
import { disableAutoGeneratedIds } from '@db/utils/commandHelpers';
import { replaceInvalidCredentials } from '@/WorkflowHelpers';
const FIX_INSTRUCTION =
'Please fix the database by running ./packages/cli/bin/n8n user-management:reset';
import { BaseCommand, UM_FIX_INSTRUCTION } from '../BaseCommand';
function assertHasWorkflowsToImport(workflows: unknown): asserts workflows is IWorkflowToImport[] {
if (!Array.isArray(workflows)) {
@@ -50,7 +35,7 @@ function assertHasWorkflowsToImport(workflows: unknown): asserts workflows is IW
}
}
export class ImportWorkflowsCommand extends Command {
export class ImportWorkflowsCommand extends BaseCommand {
static description = 'Import workflows';
static examples = [
@@ -74,118 +59,74 @@ export class ImportWorkflowsCommand extends Command {
}),
};
ownerWorkflowRole: Role;
private ownerWorkflowRole: Role;
transactionManager: EntityManager;
private transactionManager: EntityManager;
async init() {
disableAutoGeneratedIds(WorkflowEntity);
await super.init();
}
async run(): Promise<void> {
const logger = getLogger();
LoggerProxy.init(logger);
// eslint-disable-next-line @typescript-eslint/no-shadow
const { flags } = this.parse(ImportWorkflowsCommand);
if (!flags.input) {
console.info('An input file or directory with --input must be provided');
this.logger.info('An input file or directory with --input must be provided');
return;
}
if (flags.separate) {
if (fs.existsSync(flags.input)) {
if (!fs.lstatSync(flags.input).isDirectory()) {
console.info('The argument to --input must be a directory');
this.logger.info('The argument to --input must be a directory');
return;
}
}
}
try {
disableAutoGeneratedIds(WorkflowEntity);
await this.initOwnerWorkflowRole();
const user = flags.userId ? await this.getAssignee(flags.userId) : await this.getOwner();
await Db.init();
const credentials = await Db.collections.Credentials.find();
const tags = await Db.collections.Tag.find();
await this.initOwnerWorkflowRole();
const user = flags.userId ? await this.getAssignee(flags.userId) : await this.getOwner();
let totalImported = 0;
// Make sure the settings exist
await UserSettings.prepareUserSettings();
const credentials = await Db.collections.Credentials.find();
const tags = await Db.collections.Tag.find();
if (flags.separate) {
let { input: inputPath } = flags;
let totalImported = 0;
if (flags.separate) {
let { input: inputPath } = flags;
if (process.platform === 'win32') {
inputPath = inputPath.replace(/\\/g, '/');
}
const files = await glob('*.json', {
cwd: inputPath,
absolute: true,
});
totalImported = files.length;
console.info(`Importing ${totalImported} workflows...`);
await Db.getConnection().transaction(async (transactionManager) => {
this.transactionManager = transactionManager;
for (const file of files) {
const workflow = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
if (credentials.length > 0) {
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentials);
if (!node.id) {
// eslint-disable-next-line no-param-reassign
node.id = uuid();
}
});
}
if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) {
await setTagsForImport(transactionManager, workflow, tags);
}
await this.storeWorkflow(workflow, user);
}
});
this.reportSuccess(totalImported);
process.exit();
if (process.platform === 'win32') {
inputPath = inputPath.replace(/\\/g, '/');
}
const workflows = JSON.parse(fs.readFileSync(flags.input, { encoding: 'utf8' }));
assertHasWorkflowsToImport(workflows);
totalImported = workflows.length;
const files = await glob('*.json', {
cwd: inputPath,
absolute: true,
});
totalImported = files.length;
this.logger.info(`Importing ${totalImported} workflows...`);
await Db.getConnection().transaction(async (transactionManager) => {
this.transactionManager = transactionManager;
for (const workflow of workflows) {
let oldCredentialFormat = false;
for (const file of files) {
const workflow = jsonParse<IWorkflowToImport>(
fs.readFileSync(file, { encoding: 'utf8' }),
);
if (credentials.length > 0) {
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentials);
if (!node.id) {
// eslint-disable-next-line no-param-reassign
node.id = uuid();
}
if (!node.credentials?.id) {
oldCredentialFormat = true;
}
});
}
if (oldCredentialFormat) {
try {
await replaceInvalidCredentials(workflow as unknown as WorkflowEntity);
} catch (error) {
console.log(error);
}
}
if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) {
await setTagsForImport(transactionManager, workflow, tags);
}
@@ -196,15 +137,58 @@ export class ImportWorkflowsCommand extends Command {
this.reportSuccess(totalImported);
process.exit();
} catch (error) {
console.error('An error occurred while importing workflows. See log messages for details.');
if (error instanceof Error) logger.error(error.message);
this.exit(1);
}
const workflows = jsonParse<IWorkflowToImport[]>(
fs.readFileSync(flags.input, { encoding: 'utf8' }),
);
assertHasWorkflowsToImport(workflows);
totalImported = workflows.length;
await Db.getConnection().transaction(async (transactionManager) => {
this.transactionManager = transactionManager;
for (const workflow of workflows) {
let oldCredentialFormat = false;
if (credentials.length > 0) {
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentials);
if (!node.id) {
// eslint-disable-next-line no-param-reassign
node.id = uuid();
}
if (!node.credentials?.id) {
oldCredentialFormat = true;
}
});
}
if (oldCredentialFormat) {
try {
await replaceInvalidCredentials(workflow as unknown as WorkflowEntity);
} catch (error) {
this.logger.error('Failed to replace invalid credential', error as Error);
}
}
if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) {
await setTagsForImport(transactionManager, workflow, tags);
}
await this.storeWorkflow(workflow, user);
}
});
this.reportSuccess(totalImported);
}
async catch(error: Error) {
this.logger.error('An error occurred while importing workflows. See log messages for details.');
this.logger.error(error.message);
}
private reportSuccess(total: number) {
console.info(`Successfully imported ${total} ${total === 1 ? 'workflow.' : 'workflows.'}`);
this.logger.info(`Successfully imported ${total} ${total === 1 ? 'workflow.' : 'workflows.'}`);
}
private async initOwnerWorkflowRole() {
@@ -213,7 +197,7 @@ export class ImportWorkflowsCommand extends Command {
});
if (!ownerWorkflowRole) {
throw new Error(`Failed to find owner workflow role. ${FIX_INSTRUCTION}`);
throw new Error(`Failed to find owner workflow role. ${UM_FIX_INSTRUCTION}`);
}
this.ownerWorkflowRole = ownerWorkflowRole;
@@ -224,7 +208,7 @@ export class ImportWorkflowsCommand extends Command {
await this.transactionManager.upsert(
SharedWorkflow,
{
workflowId: result.identifiers[0].id,
workflowId: result.identifiers[0].id as string,
userId: user.id,
roleId: this.ownerWorkflowRole.id,
},
@@ -247,7 +231,7 @@ export class ImportWorkflowsCommand extends Command {
(await Db.collections.User.findOneBy({ globalRoleId: ownerGlobalRole?.id }));
if (!owner) {
throw new Error(`Failed to find owner. ${FIX_INSTRUCTION}`);
throw new Error(`Failed to find owner. ${UM_FIX_INSTRUCTION}`);
}
return owner;