fix: Enable crash journal only in production mode (no-changelog) (#4948)

* consolidate various `NODE_ENV` checks in the `cli` package

* enable crash journal only in production
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2022-12-16 15:27:49 +01:00 committed by GitHub
parent 2a7cb0192a
commit 323bd78067
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 19 deletions

View file

@ -3,6 +3,7 @@ import { mkdir, utimes, open, rm } from 'fs/promises';
import { join, dirname } from 'path';
import { UserSettings } from 'n8n-core';
import { LoggerProxy, sleep } from 'n8n-workflow';
import { inProduction } from '@/constants';
export const touchFile = async (filePath: string): Promise<void> => {
await mkdir(dirname(filePath), { recursive: true });
@ -18,6 +19,8 @@ export const touchFile = async (filePath: string): Promise<void> => {
const journalFile = join(UserSettings.getUserN8nFolderPath(), 'crash.journal');
export const init = async () => {
if (!inProduction) return;
if (existsSync(journalFile)) {
// Crash detected
LoggerProxy.error('Last session crashed');

View file

@ -14,9 +14,8 @@ import type {
IExecutionFlattedDb,
IExecutionResponse,
IWorkflowDb,
} from './Interfaces';
const inDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
} from '@/Interfaces';
import { inDevelopment } from '@/constants';
/**
* Special Error which allows to return also an error code and http status code

View file

@ -3,6 +3,7 @@ import { LoggerProxy } from 'n8n-workflow';
import { getLogger, Logger } from '@/Logger';
import { User } from '@db/entities/User';
import * as Db from '@/Db';
import { inTest } from '@/constants';
export abstract class BaseCommand extends Command {
logger: Logger;
@ -19,7 +20,7 @@ export abstract class BaseCommand extends Command {
}
async finally(): Promise<void> {
if (process.env.NODE_ENV === 'test') return;
if (inTest) return;
this.exit();
}

View file

@ -7,8 +7,7 @@ import { tmpdir } from 'os';
import { mkdtempSync } from 'fs';
import { join } from 'path';
import { schema } from './schema';
const inE2ETests = process.env.E2E_TESTS === 'true';
import { inTest, inE2ETests } from '@/constants';
if (inE2ETests) {
// Skip loading config from env variables in end-to-end tests
@ -36,10 +35,10 @@ config.getEnv = config.get;
if (!inE2ETests) {
// Overwrite default configuration with settings which got defined in
// optional configuration files
const { N8N_CONFIG_FILES, NODE_ENV } = process.env;
const { N8N_CONFIG_FILES } = process.env;
if (N8N_CONFIG_FILES !== undefined) {
const configFiles = N8N_CONFIG_FILES.split(',');
if (NODE_ENV !== 'test') {
if (!inTest) {
console.log(`\nLoading configuration overwrites from:\n - ${configFiles.join('\n - ')}\n`);
}

View file

@ -4,6 +4,12 @@
import { resolve, join, dirname } from 'path';
import { RESPONSE_ERROR_MESSAGES as CORE_RESPONSE_ERROR_MESSAGES, UserSettings } from 'n8n-core';
const { NODE_ENV, E2E_TESTS } = process.env;
export const inProduction = NODE_ENV === 'production';
export const inDevelopment = !NODE_ENV || NODE_ENV === 'development';
export const inTest = NODE_ENV === 'test';
export const inE2ETests = E2E_TESTS === 'true';
export const CLI_DIR = resolve(__dirname, '..');
export const TEMPLATES_DIR = join(CLI_DIR, 'templates');
export const NODES_BASE_DIR = join(CLI_DIR, '..', 'nodes-base');

View file

@ -4,6 +4,7 @@ import { UserSettings } from 'n8n-core';
import type { QueryRunner } from 'typeorm/query-runner/QueryRunner';
import config from '@/config';
import { getLogger } from '@/Logger';
import { inTest } from '@/constants';
const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';
@ -37,10 +38,7 @@ export function loadSurveyFromDisk(): string | null {
let logFinishTimeout: NodeJS.Timeout;
export function logMigrationStart(
migrationName: string,
disableLogging = process.env.NODE_ENV === 'test',
): void {
export function logMigrationStart(migrationName: string, disableLogging = inTest): void {
if (disableLogging) return;
if (!logFinishTimeout) {
@ -52,10 +50,7 @@ export function logMigrationStart(
clearTimeout(logFinishTimeout);
}
export function logMigrationEnd(
migrationName: string,
disableLogging = process.env.NODE_ENV === 'test',
): void {
export function logMigrationEnd(migrationName: string, disableLogging = inTest): void {
if (disableLogging) return;
getLogger().debug(`Finished migration ${migrationName}`);

View file

@ -1,8 +1,6 @@
import { inDevelopment } from '@/constants';
import type { RequestHandler } from 'express';
const { NODE_ENV } = process.env;
const inDevelopment = !NODE_ENV || NODE_ENV === 'development';
export const corsMiddleware: RequestHandler = (req, res, next) => {
if (inDevelopment && 'origin' in req.headers) {
// Allow access also from frontend when developing