Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make oclif commands testable #3571

Merged
merged 9 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
279 changes: 277 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions packages/cli/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Command } from '@oclif/core';
import { LoggerProxy } from 'n8n-workflow';
import { getLogger, Logger } from '../src/Logger';
import { User } from '../src/databases/entities/User';
import { Db } from '../src';

export abstract class BaseCommand extends Command {
logger: Logger;

/**
* Lifecycle methods
*/

async init(): Promise<void> {
this.logger = getLogger();
LoggerProxy.init(this.logger);

await Db.init();
}

async finally(): Promise<void> {
if (process.env.NODE_ENV === 'test') return;

this.exit();
}

/**
* User Management utils
*/

defaultUserProps = {
firstName: null,
lastName: null,
email: null,
password: null,
resetPasswordToken: null,
};

async getInstanceOwner(): Promise<User> {
const globalRole = await Db.collections.Role.findOneOrFail({
name: 'owner',
scope: 'global',
});

const owner = await Db.collections.User.findOne({ globalRole });

if (owner) return owner;

const user = new User();

Object.assign(user, { ...this.defaultUserProps, globalRole });

await Db.collections.User.save(user);

return Db.collections.User.findOneOrFail({ globalRole });
Comment on lines +49 to +55
Copy link
Contributor

@BHesseldieck BHesseldieck Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we already discussed previously we should remove this here in the future and maybe offer another command that self-heals the DB

}
}
BHesseldieck marked this conversation as resolved.
Show resolved Hide resolved
Loading