2022-11-23 07:20:28 -08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const glob = require('fast-glob');
|
2023-11-13 03:11:16 -08:00
|
|
|
const uniq = require('lodash/uniq');
|
|
|
|
const { LoggerProxy, getCredentialsForNode } = require('n8n-workflow');
|
2022-11-23 07:20:28 -08:00
|
|
|
const { packageDir, writeJSON } = require('./common');
|
2022-11-24 08:36:57 -08:00
|
|
|
const { loadClassInIsolation } = require('../dist/ClassLoader');
|
2022-11-23 07:20:28 -08:00
|
|
|
|
2023-10-20 09:26:33 -07:00
|
|
|
LoggerProxy.init(console);
|
2022-11-23 07:20:28 -08:00
|
|
|
|
|
|
|
const loadClass = (sourcePath) => {
|
|
|
|
try {
|
|
|
|
const [className] = path.parse(sourcePath).name.split('.');
|
2022-11-24 08:36:57 -08:00
|
|
|
const filePath = path.resolve(packageDir, sourcePath);
|
|
|
|
const instance = loadClassInIsolation(filePath, className);
|
2022-11-23 07:20:28 -08:00
|
|
|
return { instance, sourcePath, className };
|
|
|
|
} catch (e) {
|
2022-11-24 08:36:57 -08:00
|
|
|
LoggerProxy.warn(`Failed to load ${sourcePath}: ${e.message}`);
|
2022-11-23 07:20:28 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-13 03:11:16 -08:00
|
|
|
const generateKnownNodes = async () => {
|
|
|
|
const nodeClasses = glob
|
|
|
|
.sync('dist/nodes/**/*.node.js', { cwd: packageDir })
|
|
|
|
.map(loadClass)
|
|
|
|
// Ignore node versions
|
|
|
|
.filter((nodeClass) => nodeClass && !/[vV]\d.node\.js$/.test(nodeClass.sourcePath));
|
|
|
|
|
|
|
|
const nodes = {};
|
|
|
|
const nodesByCredential = {};
|
|
|
|
|
|
|
|
for (const { className, sourcePath, instance } of nodeClasses) {
|
|
|
|
const nodeName = instance.description.name;
|
|
|
|
nodes[nodeName] = { className, sourcePath };
|
|
|
|
|
|
|
|
for (const credential of getCredentialsForNode(instance)) {
|
|
|
|
if (!nodesByCredential[credential.name]) {
|
|
|
|
nodesByCredential[credential.name] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
nodesByCredential[credential.name].push(nodeName);
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 01:28:18 -08:00
|
|
|
|
2023-11-13 03:11:16 -08:00
|
|
|
LoggerProxy.info(`Detected ${Object.keys(nodes).length} nodes`);
|
|
|
|
await writeJSON('known/nodes.json', nodes);
|
|
|
|
return { nodes, nodesByCredential };
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateKnownCredentials = async (nodesByCredential) => {
|
|
|
|
const credentialClasses = glob
|
|
|
|
.sync(`dist/credentials/**/*.credentials.js`, { cwd: packageDir })
|
2022-11-23 07:20:28 -08:00
|
|
|
.map(loadClass)
|
2023-11-13 03:11:16 -08:00
|
|
|
.filter((data) => !!data);
|
|
|
|
|
|
|
|
for (const { instance } of credentialClasses) {
|
|
|
|
if (Array.isArray(instance.extends)) {
|
|
|
|
for (const extendedCredential of instance.extends) {
|
|
|
|
nodesByCredential[extendedCredential] = [
|
|
|
|
...(nodesByCredential[extendedCredential] ?? []),
|
|
|
|
...(nodesByCredential[instance.name] ?? []),
|
|
|
|
];
|
2022-12-19 06:12:04 -08:00
|
|
|
}
|
2023-11-13 03:11:16 -08:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 01:28:18 -08:00
|
|
|
|
2023-11-13 03:11:16 -08:00
|
|
|
const credentials = credentialClasses.reduce(
|
|
|
|
(credentials, { className, sourcePath, instance }) => {
|
|
|
|
const credentialName = instance.name;
|
|
|
|
const credential = {
|
|
|
|
className,
|
|
|
|
sourcePath,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Array.isArray(instance.extends)) {
|
|
|
|
credential.extends = instance.extends;
|
2023-07-10 08:57:26 -07:00
|
|
|
}
|
|
|
|
|
2023-11-13 03:11:16 -08:00
|
|
|
if (nodesByCredential[credentialName]) {
|
|
|
|
credential.supportedNodes = Array.from(new Set(nodesByCredential[credentialName]));
|
2022-11-30 01:28:18 -08:00
|
|
|
}
|
2023-07-10 08:57:26 -07:00
|
|
|
|
2023-11-13 03:11:16 -08:00
|
|
|
credentials[credentialName] = credential;
|
|
|
|
|
|
|
|
return credentials;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
LoggerProxy.info(`Detected ${Object.keys(credentials).length} credentials`);
|
|
|
|
await writeJSON('known/credentials.json', credentials);
|
|
|
|
return credentials;
|
2022-11-23 07:20:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
(async () => {
|
2023-11-13 03:11:16 -08:00
|
|
|
const { nodesByCredential } = await generateKnownNodes();
|
|
|
|
await generateKnownCredentials(nodesByCredential);
|
2022-11-23 07:20:28 -08:00
|
|
|
})();
|