n8n
Version:
n8n Workflow Automation Tool
94 lines • 4.63 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeTypes = void 0;
const n8n_core_1 = require("n8n-core");
const n8n_workflow_1 = require("n8n-workflow");
const typedi_1 = require("typedi");
const LoadNodesAndCredentials_1 = require("./LoadNodesAndCredentials");
const path_1 = require("path");
const promises_1 = require("fs/promises");
const unrecognized_node_type_error_1 = require("./errors/unrecognized-node-type.error");
let NodeTypes = class NodeTypes {
constructor(loadNodesAndCredentials) {
this.loadNodesAndCredentials = loadNodesAndCredentials;
loadNodesAndCredentials.addPostProcessor(async () => this.applySpecialNodeParameters());
}
getWithSourcePath(nodeTypeName, version) {
const nodeType = this.getNode(nodeTypeName);
if (!nodeType) {
throw new n8n_workflow_1.ApplicationError('Unknown node type', { tags: { nodeTypeName } });
}
const { description } = n8n_workflow_1.NodeHelpers.getVersionedNodeType(nodeType.type, version);
return { description: { ...description }, sourcePath: nodeType.sourcePath };
}
getByName(nodeType) {
return this.getNode(nodeType).type;
}
getByNameAndVersion(nodeType, version) {
return n8n_workflow_1.NodeHelpers.getVersionedNodeType(this.getNode(nodeType).type, version);
}
applySpecialNodeParameters() {
for (const nodeTypeData of Object.values(this.loadNodesAndCredentials.loadedNodes)) {
const nodeType = n8n_workflow_1.NodeHelpers.getVersionedNodeType(nodeTypeData.type);
n8n_workflow_1.NodeHelpers.applySpecialNodeParameters(nodeType);
}
}
getKnownTypes() {
return this.loadNodesAndCredentials.knownNodes;
}
getNode(type) {
const { loadedNodes, knownNodes } = this.loadNodesAndCredentials;
if (type in loadedNodes) {
return loadedNodes[type];
}
if (type in knownNodes) {
const { className, sourcePath } = knownNodes[type];
const loaded = (0, n8n_core_1.loadClassInIsolation)(sourcePath, className);
n8n_workflow_1.NodeHelpers.applySpecialNodeParameters(loaded);
loadedNodes[type] = { sourcePath, type: loaded };
return loadedNodes[type];
}
throw new unrecognized_node_type_error_1.UnrecognizedNodeTypeError(type);
}
async getNodeTranslationPath({ nodeSourcePath, longNodeType, locale, }) {
const nodeDir = (0, path_1.dirname)(nodeSourcePath);
const maxVersion = await this.getMaxVersion(nodeDir);
const nodeType = longNodeType.replace('n8n-nodes-base.', '');
return maxVersion
? (0, path_1.join)(nodeDir, `v${maxVersion}`, 'translations', locale, `${nodeType}.json`)
: (0, path_1.join)(nodeDir, 'translations', locale, `${nodeType}.json`);
}
async getMaxVersion(dir) {
const entries = await (0, promises_1.readdir)(dir, { withFileTypes: true });
const dirnames = entries.reduce((acc, cur) => {
if (this.isVersionedDirname(cur))
acc.push(cur.name);
return acc;
}, []);
if (!dirnames.length)
return null;
return Math.max(...dirnames.map((d) => parseInt(d.charAt(1), 10)));
}
isVersionedDirname(dirent) {
if (!dirent.isDirectory())
return false;
const ALLOWED_VERSIONED_DIRNAME_LENGTH = [2, 3];
return (ALLOWED_VERSIONED_DIRNAME_LENGTH.includes(dirent.name.length) &&
dirent.name.toLowerCase().startsWith('v'));
}
};
exports.NodeTypes = NodeTypes;
exports.NodeTypes = NodeTypes = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [LoadNodesAndCredentials_1.LoadNodesAndCredentials])
], NodeTypes);
//# sourceMappingURL=NodeTypes.js.map
;