Skip to content

Commit

Permalink
fix(core): Load config schema after process.env has been overwritten …
Browse files Browse the repository at this point in the history
…(no-changelog)
  • Loading branch information
netroy committed Nov 7, 2023
1 parent 2f66568 commit b6f79a2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
15 changes: 10 additions & 5 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import type { ServeStaticOptions } from 'serve-static';
import type { FindManyOptions, FindOptionsWhere } from 'typeorm';
import { Not, In } from 'typeorm';

import { LoadMappingOptions, LoadNodeParameterOptions, LoadNodeListSearch } from 'n8n-core';
import {
LoadMappingOptions,
LoadNodeParameterOptions,
LoadNodeListSearch,
InstanceSettings,
} from 'n8n-core';

import type {
INodeCredentials,
Expand All @@ -46,7 +51,6 @@ import { getSharedWorkflowIds } from '@/WorkflowHelpers';
import { workflowsController } from '@/workflows/workflows.controller';
import {
EDITOR_UI_DIST_DIR,
GENERATED_STATIC_DIR,
inDevelopment,
inE2ETests,
N8N_VERSION,
Expand Down Expand Up @@ -959,11 +963,12 @@ export class Server extends AbstractServer {
);
}

const { staticCacheDir } = Container.get(InstanceSettings);
if (frontendService) {
const staticOptions: ServeStaticOptions = {
cacheControl: false,
setHeaders: (res: express.Response, path: string) => {
const isIndex = path === pathJoin(GENERATED_STATIC_DIR, 'index.html');
const isIndex = path === pathJoin(staticCacheDir, 'index.html');
const cacheControl = isIndex
? 'no-cache, no-store, must-revalidate'
: 'max-age=86400, immutable';
Expand All @@ -989,7 +994,7 @@ export class Server extends AbstractServer {

this.app.use(
'/',
express.static(GENERATED_STATIC_DIR),
express.static(staticCacheDir),
express.static(EDITOR_UI_DIST_DIR, staticOptions),
);

Expand All @@ -999,7 +1004,7 @@ export class Server extends AbstractServer {
next();
});
} else {
this.app.use('/', express.static(GENERATED_STATIC_DIR));
this.app.use('/', express.static(staticCacheDir));
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import * as Db from '@/Db';
import * as GenericHelpers from '@/GenericHelpers';
import { Server } from '@/Server';
import { EDITOR_UI_DIST_DIR, GENERATED_STATIC_DIR, LICENSE_FEATURES } from '@/constants';
import { EDITOR_UI_DIST_DIR, LICENSE_FEATURES } from '@/constants';
import { eventBus } from '@/eventbus';
import { BaseCommand } from './BaseCommand';
import { InternalHooks } from '@/InternalHooks';
Expand Down Expand Up @@ -169,10 +169,11 @@ export class Start extends BaseCommand {
}

const closingTitleTag = '</title>';
const { staticCacheDir } = this.instanceSettings;
const compileFile = async (fileName: string) => {
const filePath = path.join(EDITOR_UI_DIST_DIR, fileName);
if (/(index\.html)|.*\.(js|css)/.test(filePath) && existsSync(filePath)) {
const destFile = path.join(GENERATED_STATIC_DIR, fileName);
const destFile = path.join(staticCacheDir, fileName);
await mkdir(path.dirname(destFile), { recursive: true });
const streams = [
createReadStream(filePath, 'utf-8'),
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import convict from 'convict';
import dotenv from 'dotenv';
import { readFileSync } from 'fs';
import { setGlobalState } from 'n8n-workflow';
import { schema } from './schema';
import { inTest, inE2ETests } from '@/constants';

if (inE2ETests) {
Expand All @@ -25,6 +24,8 @@ if (inE2ETests) {
dotenv.config();
}

// Load schema after process.env has been overwritten
import { schema } from './schema';
const config = convict(schema, { args: [] });

// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readFileSync } from 'fs';
import { resolve, join, dirname } from 'path';
import { Container } from 'typedi';
import type { n8n } from 'n8n-core';
import { InstanceSettings } from 'n8n-core';
import { jsonParse } from 'n8n-workflow';

const { NODE_ENV, E2E_TESTS } = process.env;
Expand All @@ -17,10 +15,6 @@ export const CUSTOM_API_CALL_KEY = '__CUSTOM_API_CALL__';
export const CLI_DIR = resolve(__dirname, '..');
export const TEMPLATES_DIR = join(CLI_DIR, 'templates');
export const NODES_BASE_DIR = dirname(require.resolve('n8n-nodes-base'));
export const GENERATED_STATIC_DIR = join(
Container.get(InstanceSettings).userHome,
'.cache/n8n/public',
);
export const EDITOR_UI_DIST_DIR = join(dirname(require.resolve('n8n-editor-ui')), 'dist');

export function getN8nPackageJson() {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from 'n8n-workflow';
import { InstanceSettings } from 'n8n-core';

import { GENERATED_STATIC_DIR, LICENSE_FEATURES } from '@/constants';
import { LICENSE_FEATURES } from '@/constants';
import { CredentialsOverwrites } from '@/CredentialsOverwrites';
import { CredentialTypes } from '@/CredentialTypes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
Expand Down Expand Up @@ -205,8 +205,9 @@ export class FrontendService {
async generateTypes() {
this.overwriteCredentialsProperties();

const { staticCacheDir } = this.instanceSettings;
// pre-render all the node and credential types as static json files
await mkdir(path.join(GENERATED_STATIC_DIR, 'types'), { recursive: true });
await mkdir(path.join(staticCacheDir, 'types'), { recursive: true });
const { credentials, nodes } = this.loadNodesAndCredentials.types;
this.writeStaticJSON('nodes', nodes);
this.writeStaticJSON('credentials', credentials);
Expand Down Expand Up @@ -303,7 +304,8 @@ export class FrontendService {
}

private writeStaticJSON(name: string, data: INodeTypeBaseDescription[] | ICredentialType[]) {
const filePath = path.join(GENERATED_STATIC_DIR, `types/${name}.json`);
const { staticCacheDir } = this.instanceSettings;
const filePath = path.join(staticCacheDir, `types/${name}.json`);
const stream = createWriteStream(filePath, 'utf-8');
stream.write('[\n');
data.forEach((entry, index) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/InstanceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ type Settings = ReadOnlySettings & WritableSettings;

@Service()
export class InstanceSettings {
readonly userHome = this.getUserHome();
private readonly userHome = this.getUserHome();

/** The path to the n8n folder in which all n8n related data gets saved */
readonly n8nFolder = path.join(this.userHome, '.n8n');

/** The path to the folder where all generated static assets are copied to */
readonly staticCacheDir = path.join(this.userHome, '.cache/n8n/public');

/** The path to the folder containing custom nodes and credentials */
readonly customExtensionDir = path.join(this.n8nFolder, 'custom');

Expand Down

0 comments on commit b6f79a2

Please sign in to comment.