EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
pluginBuilder.js
Go to the documentation of this file.
1const path = require('path');
2const fs = require('fs');
3const pool = require('./db');
4const PLUGIN_SRC = path.resolve(__dirname, '../../agent/plugins');
5const PLUGIN_DEST = path.resolve(__dirname, '../../agent/plugins');
6// pluginBuilder.js - Redis worker for plugin build/check jobs
7
8const { execSync } = require('child_process');
9const Redis = require('ioredis');
10const redisConfig = require('../config/redis');
11const redis = new Redis(redisConfig);
12
13
14const PLUGIN_HASHES_PATH = path.resolve(__dirname, 'pluginHashes.json');
15/**
16 *
17 */
18function getPluginHashes() {
19 try {
20 return JSON.parse(fs.readFileSync(PLUGIN_HASHES_PATH, 'utf8'));
21 } catch {
22 return {};
23 }
24}
25/**
26 *
27 * @param hashes
28 */
29function savePluginHashes(hashes) {
30 fs.writeFileSync(PLUGIN_HASHES_PATH, JSON.stringify(hashes, null, 2));
31}
32
33/**
34 *
35 * @param filePath
36 */
37function hashFile(filePath) {
38 if (!fs.existsSync(filePath)) return null;
39 const content = fs.readFileSync(filePath);
40 return require('crypto').createHash('sha256').update(content).digest('hex');
41}
42
43/**
44 *
45 */
46function buildAllWindowsPlugins() {
47 }
48 const hashes = getPluginHashes();
49 const srcFiles = fs.readdirSync(PLUGIN_SRC).filter(f => f.endsWith('.js'));
50 let changed = false;
51 // Build plugins
52 for (const file of srcFiles) {
53 const pluginName = file.replace('.js', '');
54 const srcPath = path.join(PLUGIN_SRC, file);
55 const destPath = path.join(PLUGIN_DEST, 'windows', pluginName + '.exe');
56 const newHash = hashFile(srcPath);
57 if (hashes[file] !== newHash || !fs.existsSync(destPath)) {
58 try {
59 execSync(`npx pkg ${srcPath} --output ${destPath} --targets node18-win-x64`);
60 console.log(`[PLUGIN BUILDER] Built ${pluginName}.exe for Windows.`);
61 hashes[file] = newHash;
62 changed = true;
63 } catch (err) {
64 console.error(`[PLUGIN BUILDER] Failed to build ${pluginName}.exe:`, err.message);
65 }
66 } else {
67 console.log(`[PLUGIN BUILDER] No change for ${pluginName}.js, skipping build.`);
68 }
69 // Seed plugin info in database
70 const version = '1.0.0';
71 const description = `Auto-seeded plugin for ${pluginName}`;
72 pool.query(
73 `INSERT INTO plugins (name, description, version, type) VALUES ($1, $2, $3, $4)
74 ON CONFLICT (name) DO UPDATE SET description = EXCLUDED.description, version = EXCLUDED.version, type = EXCLUDED.type`,
75 [`${pluginName}.exe`, description, version, 'windows']
76 ).then(() => {
77 console.log(`[PLUGIN BUILDER] Seeded DB: ${pluginName}.exe`);
78 }).catch((err) => {
79 console.error(`[PLUGIN BUILDER] DB seed error for ${pluginName}.exe:`, err.message);
80 });
81 }
82 // Build agent binary as plugin (use EverydayTechAgent-win-x64.exe)
83 const agentSrc = path.resolve(__dirname, '../../agent/agent.js');
84 const agentDest = path.resolve(__dirname, '../../agent/plugins/agent/EverydayTechAgent-win-x64.exe');
85 const agentHash = hashFile(agentSrc);
86 if (hashes['agent.js'] !== agentHash || !fs.existsSync(agentDest)) {
87 try {
88 execSync(`npx pkg ${agentSrc} --output ${agentDest} --targets node18-win-x64`);
89 console.log(`[PLUGIN BUILDER] Built EverydayTechAgent-win-x64.exe for Windows.`);
90 hashes['EverydayTechAgent-win-x64.exe'] = agentHash;
91 changed = true;
92 } catch (err) {
93 console.error(`[PLUGIN BUILDER] Failed to build EverydayTechAgent-win-x64.exe:`, err.message);
94 }
95 } else {
96 console.log(`[PLUGIN BUILDER] No change for agent.js, skipping agent build.`);
97 }
98 // Seed agent plugin info in database
99 pool.query(
100 `INSERT INTO plugins (name, description, version, type) VALUES ($1, $2, $3, $4)
101 ON CONFLICT (name) DO UPDATE SET description = EXCLUDED.description, version = EXCLUDED.version, type = EXCLUDED.type`,
102 ['EverydayTechAgent-win-x64.exe', 'Auto-seeded agent plugin', '1.0.0', 'agent']
103 ).then(() => {
104 console.log('[PLUGIN BUILDER] Seeded DB: EverydayTechAgent-win-x64.exe');
105 }).catch((err) => {
106 console.error('[PLUGIN BUILDER] DB seed error for EverydayTechAgent-win-x64.exe:', err.message);
107 });
108 if (changed) savePluginHashes(hashes);
109
110
111redis.subscribe('plugin-build-jobs', (err, count) => {
112 if (err) console.error('[PLUGIN BUILDER] Redis subscribe error:', err);
113 else console.log(`[PLUGIN BUILDER] Subscribed to plugin-build-jobs (${count})`);
114});
115
116redis.on('message', (channel, message) => {
117 if (channel !== 'plugin-build-jobs') return;
118 try {
119 const job = JSON.parse(message);
120 const { os } = job;
121 console.log(`[PLUGIN BUILDER] Received job for OS: ${os}`);
122 if (os === 'windows') {
123 buildAllWindowsPlugins();
124 }
125 // Add similar logic for other OS if needed
126 } catch (err) {
127 console.error('[PLUGIN BUILDER] Job error:', err.message);
128 }
129});
130
131// To run: node backend/services/pluginBuilder.js