mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 00:24:07 -08:00
7879239e03
* ➕ Add `@oclif/core` * 📦 Update `package-lock.json` * 📘 Export `Logger` for use as type * ✨ Create `BaseCommand` * 🐛 Prevent DB re-init * ♻️ Refactor `reset` command * 🧪 Fix `reset` test * 👕 Add lint exception Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
58 lines
1.1 KiB
TypeScript
58 lines
1.1 KiB
TypeScript
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 });
|
|
}
|
|
}
|