Skip to content

Commit

Permalink
address Ivan's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Nov 22, 2022
1 parent 3d01525 commit 8842877
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 80 deletions.
33 changes: 0 additions & 33 deletions packages/cli/src/CredentialTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
LoadedClass,
} from 'n8n-workflow';
import { RESPONSE_ERROR_MESSAGES } from './constants';
import type { ICredentialsTypeData } from './Interfaces';

class CredentialTypesClass implements ICredentialTypes {
constructor(private nodesAndCredentials: INodesAndCredentials) {}
Expand All @@ -19,38 +18,6 @@ class CredentialTypesClass implements ICredentialTypes {
return this.getCredential(credentialType).type;
}

/**
* Returns the credentials data of the given type and its parent types it extends
*/
getCredentialsDataWithParents(type: string): ICredentialsTypeData {
const credentialType = this.getByName(type);

const credentialTypes = this.loadedCredentials;
const credentialTypeData: ICredentialsTypeData = {};
credentialTypeData[type] = {
className: credentialTypes[type].type.constructor.name,
sourcePath: credentialTypes[type].sourcePath,
};

if (credentialType === undefined || credentialType.extends === undefined) {
return credentialTypeData;
}

for (const typeName of credentialType.extends) {
if (credentialTypeData[typeName] !== undefined) {
continue;
}

credentialTypeData[typeName] = {
className: credentialTypes[typeName].type.constructor.name,
sourcePath: credentialTypes[typeName].sourcePath,
};
Object.assign(credentialTypeData, this.getCredentialsDataWithParents(typeName));
}

return credentialTypeData;
}

private getCredential(type: string): LoadedClass<ICredentialType> {
const loadedCredentials = this.loadedCredentials;
if (type in loadedCredentials) {
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
import type {
ExecutionError,
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
Expand All @@ -15,6 +15,7 @@ import {
ITelemetrySettings,
ITelemetryTrackProperties,
IWorkflowBase as IWorkflowBaseWorkflow,
LoadingDetails,
Workflow,
WorkflowActivateMode,
WorkflowExecuteMode,
Expand Down Expand Up @@ -58,10 +59,7 @@ export interface ICustomRequest extends Request {
}

export interface ICredentialsTypeData {
[key: string]: {
className: string;
sourcePath: string;
};
[key: string]: LoadingDetails;
}

export interface ICredentialsOverwrite {
Expand Down
17 changes: 0 additions & 17 deletions packages/cli/src/NodeTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { loadClassInIsolation } from 'n8n-core';
import type {
INode,
INodesAndCredentials,
INodeType,
INodeTypeDescription,
Expand Down Expand Up @@ -68,22 +67,6 @@ class NodeTypesClass implements INodeTypes {
throw new Error(`${RESPONSE_ERROR_MESSAGES.NO_NODE}: ${type}`);
}

/**
* Returns the names of the NodeTypes which are are needed
* to execute the gives nodes
*
*/
private getNeededNodeTypes(nodes: INode[]): Array<{ type: string; version: number }> {
// Check which node-types have to be loaded
const neededNodeTypes: Array<{ type: string; version: number }> = [];
for (const node of nodes) {
if (neededNodeTypes.find((neededNodes) => node.type === neededNodes.type) === undefined) {
neededNodeTypes.push({ type: node.type, version: node.typeVersion });
}
}
return neededNodeTypes;
}

private get loadedNodes() {
return this.nodesAndCredentials.loaded.nodes;
}
Expand Down
6 changes: 1 addition & 5 deletions packages/cli/src/WorkflowHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import {
} from 'n8n-workflow';
import { v4 as uuid } from 'uuid';
import * as Db from '@/Db';
import {
ICredentialsDb,
IWorkflowErrorData,
IWorkflowExecutionDataProcess,
} from '@/Interfaces';
import { ICredentialsDb, IWorkflowErrorData, IWorkflowExecutionDataProcess } from '@/Interfaces';
import { NodeTypes } from '@/NodeTypes';
import { WorkflowRunner } from '@/WorkflowRunner';

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/DirectoryLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ export class LazyPackageDirectoryLoader extends PackageDirectoryLoader {
this.types.nodes = await this.readJSON('dist/types/nodes.json');
this.types.credentials = await this.readJSON('dist/types/credentials.json');

Logger.debug(`Lazy Loading credentials and nodes from ${this.packageJson.name}`, {
Logger.info(`Lazy Loading credentials and nodes from ${this.packageJson.name}`, {
credentials: this.types.credentials?.length ?? 0,
nodes: this.types.nodes?.length ?? 0,
});

return; // We can loads nodes and credentials lazily now
return; // We can load nodes and credentials lazily now
} catch {
Logger.debug("Can't enable lazy-loading");
Logger.info("Can't enable lazy-loading");
await super.loadAll();
}
}
Expand Down
29 changes: 15 additions & 14 deletions packages/nodes-base/scripts/generate-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ const loadClass = (sourcePath) => {
}
};

const writeJSON = async (file, data) => {
const writePromises = [];
const writeJSON = (file, data) => {
const filePath = path.resolve(distDir, file);
await mkdir(path.dirname(filePath), { recursive: true });
const payload = Array.isArray(data)
? `[\n${data.map((entry) => JSON.stringify(entry)).join(',\n')}\n]`
: JSON.stringify(data, null, 2);
await writeFile(filePath, payload, { encoding: 'utf-8' });
mkdir(path.dirname(filePath), { recursive: true }).then(() => {
const payload = Array.isArray(data)
? `[\n${data.map((entry) => JSON.stringify(entry)).join(',\n')}\n]`
: JSON.stringify(data, null, 2);
writePromises.push(writeFile(filePath, payload, { encoding: 'utf-8' }));
});
};

(async () => {
Expand All @@ -42,7 +44,7 @@ const writeJSON = async (file, data) => {
.sync(`dist/${kind}/**/*.${kind === 'nodes' ? 'node' : kind}.js`, {
cwd: packageDir,
})
.filter((filePath) => !/[vV]\d.node.js$/.test(filePath))
.filter((filePath) => !/[vV]\d.node\.js$/.test(filePath))
.map(loadClass)
.filter((data) => !!data)
.reduce((obj, { className, sourcePath, instance }) => {
Expand All @@ -52,24 +54,23 @@ const writeJSON = async (file, data) => {
return obj;
}, {});

await writeJSON(`known/${kind}.json`, known[kind]);
writeJSON(`known/${kind}.json`, known[kind]);
}

await mkdir(path.resolve(distDir, 'icons/nodes'), { recursive: true });
await mkdir(path.resolve(distDir, 'icons/credentials'), { recursive: true });

const loader = new PackageDirectoryLoader(packageDir);
await loader.loadAll();

const credentialTypes = Object.values(loader.credentialTypes).map((data) => data.type);
await writeJSON('types/credentials.json', credentialTypes);
writeJSON('types/credentials.json', credentialTypes);

const nodeTypes = Object.values(loader.nodeTypes)
.map((data) => data.type)
.flatMap((nodeData) => {
const allNodeTypes = NodeHelpers.getVersionedNodeTypeAll(nodeData);
return allNodeTypes.map((element) => ({ ...element.description }));
return allNodeTypes.map((element) => element.description);
});

await writeJSON('types/nodes.json', nodeTypes);
writeJSON('types/nodes.json', nodeTypes);

await Promise.all(writePromises);
})();
6 changes: 3 additions & 3 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1475,14 +1475,14 @@ export interface INodeTypes {
getByNameAndVersion(nodeType: string, version?: number): INodeType | undefined;
}

export type KnownData = {
export type LoadingDetails = {
className: string;
sourcePath: string;
};

export type KnownNodesAndCredentials = {
nodes: Record<string, KnownData>;
credentials: Record<string, KnownData>;
nodes: Record<string, LoadingDetails>;
credentials: Record<string, LoadingDetails>;
};

export interface LoadedClass<T> {
Expand Down

0 comments on commit 8842877

Please sign in to comment.