2022-11-23 07:20:28 -08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const glob = require('fast-glob');
|
|
|
|
const { LoggerProxy } = require('n8n-workflow');
|
|
|
|
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
|
|
|
|
|
|
|
LoggerProxy.init({
|
|
|
|
log: console.log.bind(console),
|
|
|
|
warn: console.warn.bind(console),
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-30 01:28:18 -08:00
|
|
|
const nodesToTestWith = {};
|
|
|
|
|
|
|
|
const generate = async (kind) => {
|
2022-11-23 07:20:28 -08:00
|
|
|
const data = glob
|
|
|
|
.sync(`dist/${kind}/**/*.${kind === 'nodes' ? 'node' : kind}.js`, {
|
|
|
|
cwd: packageDir,
|
|
|
|
})
|
|
|
|
.map(loadClass)
|
|
|
|
.filter((data) => !!data)
|
|
|
|
.reduce((obj, { className, sourcePath, instance }) => {
|
|
|
|
const name = kind === 'nodes' ? instance.description.name : instance.name;
|
2022-12-19 06:12:04 -08:00
|
|
|
if (!/[vV]\d.node\.js$/.test(sourcePath)) {
|
|
|
|
if (name in obj) console.error('already loaded', kind, name, sourcePath);
|
|
|
|
else obj[name] = { className, sourcePath };
|
|
|
|
}
|
2022-11-30 01:28:18 -08:00
|
|
|
|
|
|
|
if (kind === 'nodes') {
|
|
|
|
const { credentials } = instance.description;
|
|
|
|
if (credentials && credentials.length) {
|
|
|
|
for (const credential of credentials) {
|
|
|
|
nodesToTestWith[credential.name] = nodesToTestWith[credential.name] || [];
|
|
|
|
nodesToTestWith[credential.name].push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (name in nodesToTestWith) {
|
|
|
|
obj[name].nodesToTestWith = nodesToTestWith[name];
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 07:20:28 -08:00
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
LoggerProxy.info(`Detected ${Object.keys(data).length} ${kind}`);
|
2022-11-30 01:28:18 -08:00
|
|
|
await writeJSON(`known/${kind}.json`, data);
|
|
|
|
return data;
|
2022-11-23 07:20:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
(async () => {
|
2022-11-30 01:28:18 -08:00
|
|
|
await generate('nodes');
|
|
|
|
await generate('credentials');
|
2022-11-23 07:20:28 -08:00
|
|
|
})();
|