-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.js
More file actions
391 lines (324 loc) · 13 KB
/
config.js
File metadata and controls
391 lines (324 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const path = require('path');
const toml = require('toml');
const fs = require('fs').promises;
const yaml = require('js-yaml');
// Add at the top with other state variables
let globalDeviceData = {};
// Add the initialization function
async function initializeDeviceData() {
try {
globalDeviceData = JSON.parse(
await fs.readFile(path.join(process.cwd(), 'device.json'), 'utf8')
);
} catch (err) {
console.warn('Warning: No device.json found, using empty device data');
globalDeviceData = {};
}
}
// Helper function to check if a directory is a plugin directory
async function isPluginDirectory(dir) {
try {
const stats = await fs.stat(dir);
if (!stats.isDirectory()) return false;
// Check for settings.yml instead of config.toml
const files = await fs.readdir(dir);
return files.includes('settings.yml') && files.includes('views');
} catch (err) {
return false;
}
}
// Move getPluginInfoWithTokens outside the config object
async function getPluginInfoWithTokens(pluginId) {
const pluginInfo = await this.getPluginInfo(pluginId);
const envVars = await this.readEnvVars(pluginId);
// Deep clone the plugin settings to avoid modifying the original
const settings = JSON.parse(JSON.stringify(pluginInfo.trmnl.plugin_settings));
// Replace tokens in urls if they exist
if (settings.polling_url) {
settings.polling_url = await this.replaceTokensInUrl(
settings.polling_url,
settings,
envVars,
this.deviceData
);
}
//write settings.polling_headers to console
console.log('Original polling headers:', settings.polling_headers);
// If no polling headers, return empty object
if (!settings.polling_headers) {
return {
trmnl: {
plugin_settings: {
...settings,
headers: {}
}
}
};
}
//polling_headers is a string, replace all {{ placeholder }} with the envVars
const replaced_headers_string = settings.polling_headers.replace(/\{\{\s*([^}]+)\s*\}\}/g, (match, p1) => {
const trimmedKey = p1.trim();
return envVars[trimmedKey] || envVars[trimmedKey.toUpperCase()] || match;
});
console.log('Replaced headers string:', replaced_headers_string);
//convert the string to an array of strings and filter out empty lines
const headers_array = replaced_headers_string.split('\n').filter(line => line.trim());
//convert the array of header strings to an object
const headers = headers_array.reduce((acc, header) => {
const [key, value] = header.split(':').map(part => part.trim());
if (key && value) {
acc[key] = value;
}
return acc;
}, {});
console.log('Final headers object:', headers);
return {
trmnl: {
plugin_settings: {
...settings,
headers
}
}
};
}
// Environment-based configuration
const config = {
// Paths
PLUGINS_PATH: process.env.PLUGINS_PATH || path.join(process.cwd(), '_plugins'),
CACHE_PATH: process.env.CACHE_PATH || path.join(process.cwd(), 'cache'),
// Feature flags
ENABLE_IMAGE_GENERATION: process.env.ENABLE_IMAGE_GENERATION !== 'false', // Default to false
DEBUG_MODE: process.env.DEBUG_MODE === 'true',
USE_CACHE: process.env.USE_CACHE === 'true' || false,
ADMIN_MODE: process.env.ADMIN_MODE === 'true' || false,
// Rate limiting
MAX_REQUESTS_PER_5_MIN: parseInt(process.env.MAX_REQUESTS_PER_5_MIN || '400', 10),
// Server config
PORT: parseInt(process.env.PORT || '3000', 10),
// Browser config
BROWSER_LAUNCH_CONFIG: {
puppeteer: {
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage', // Docker specific
'--disable-gpu' // Optional: helps in certain environments
]
}
},
// Derived paths
get FONTS_PATH() {
return path.join(this.CDN_PATH, '/fonts');
},
get CSS_PATH() {
return path.join(this.CDN_PATH, '/css/latest');
},
get JS_PATH() {
return path.join(this.CDN_PATH, '/js/latest');
},
// CDN_PATH is used to proxy to the CDN if USE_CACHE is false
// it is used to set a relative path if USE_CACHE is true
get CDN_PATH() {
if (this.USE_CACHE) {
return 'https://usetrmnl.com';
} else {
return "";
}
},
// Helper function to get plugin path
getPluginPath(pluginId) {
return pluginId === '.' ? this.PLUGINS_PATH : path.join(this.PLUGINS_PATH, pluginId);
},
// Display settings
DISPLAY: {
width: 800,
height: 480,
delay: 1000 // delay in ms before taking screenshot
},
// Image generation options
//IMAGE_MAGICK_SWICTHES: '-dither FloydSteinberg -remap pattern:gray50 -depth 1 -strip',
//exec(`convert ${tempPng} -dither FloydSteinberg -monochrome -depth 1 -strip -define bmp:format=bmp3 ${tempBmp2}`);
//exec(`convert ${tempPng} -dither FloydSteinberg -monochrome -depth 1 -strip -compress RLE -define bmp:format=bmp3 ${tempBmp3}`);
IMAGE_MAGICK_SWICTHES: '-dither FloydSteinberg -monochrome -depth 1 -strip -compress RLE -define bmp:format=bmp3',
// ImageMagick binary path - defaults to convert
IMAGE_MAGICK_BIN: process.env.IMAGE_MAGICK_BIN || 'convert',
get PLUGINS() {
return getAvailablePlugins();
},
// this method is used to get the plugin info from the settings.yml file
async getPluginInfo(pluginId) {
const pluginPath = this.getPluginPath(pluginId);
const configContent = await fs.readFile(path.join(pluginPath, 'settings.yml'), 'utf8');
const settings = yaml.load(configContent);
return {
trmnl: {
plugin_settings: {
...settings,
custom_fields_values: settings.custom_fields_values || {}
}
}
};
},
// Add this method to the config object
getSamplePath(pluginId) {
return pluginId === '.'
? path.join(this.PLUGINS_PATH, 'sample.json')
: path.join(this.PLUGINS_PATH, pluginId, 'sample.json');
},
get deviceData() {
return globalDeviceData;
},
async initialize() {
await initializeDeviceData();
}
};
async function getAvailablePlugins() {
try {
const pluginsPath = config.PLUGINS_PATH;
const entries = await fs.readdir(pluginsPath, { withFileTypes: true });
// Filter for directories that contain a settings.yml file
const plugins = [];
// Skip special directories
const skipDirs = ['node_modules', 'public', 'design-system', 'scripts'];
for (const entry of entries) {
if (entry.isDirectory() && !skipDirs.includes(entry.name) && !entry.name.startsWith('.')) {
const pluginPath = path.join(pluginsPath, entry.name);
const settingsPath = path.join(pluginPath, 'settings.yml');
try {
// Check if settings.yml exists
await fs.access(settingsPath);
// Read and parse settings.yml
const settingsContent = await fs.readFile(settingsPath, 'utf8');
const settings = yaml.load(settingsContent);
plugins.push({
id: entry.name,
name: settings.name || entry.name,
description: settings.description || ''
});
} catch (err) {
console.warn(`Skipping ${entry.name}: No valid settings.yml found`);
}
}
}
console.log(`Found ${plugins.length} plugins:`, plugins.map(p => p.name).join(', ')); // Debug log
// Add "All Plugins" option if there are multiple plugins
if (plugins.length > 1) {
plugins.unshift({
id: 'all',
name: 'All Plugins',
description: 'Show all available plugins'
});
}
return plugins;
} catch (error) {
console.error('Error getting available plugins:', error);
return [];
}
}
config.getAvailablePlugins = getAvailablePlugins;
config.isPluginDirectory = isPluginDirectory;
config.getPluginInfo = config.getPluginInfo;
// Add these methods to the config object
function getNestedValue(obj, path) {
return path.split('.').reduce((current, key) => {
return current && current[key];
}, obj);
}
// Update replaceTokensInUrl to use settings directly
async function replaceTokensInUrl(url, settings, envVars, deviceData) {
if (!url) return url;
// Replace any {{placeholder}} in the URL with environment variables first
Object.entries(envVars || {}).forEach(([key, value]) => {
const placeholder = `{{ ${key} }}`;
if (url.includes(placeholder)) {
url = url.replace(placeholder, value);
}
});
//replace any remaining tokens such as {{ days }} with the value from the custom_fields_values object
Object.entries(settings.custom_fields_values || {}).forEach(([key, value]) => {
const placeholder = `{{ ${key} }}`;
if (url.includes(placeholder)) {
url = url.replace(placeholder, value);
}
});
// Replace tokens with fully qualified paths
const tokenRegex = /\{\{\s*([^}]+)\s*\}\}/g;
url = url.replace(tokenRegex, (match, path) => {
// Check env vars first
if (envVars && envVars[path.trim()]) return envVars[path.trim()];
// Try to get value from plugin settings or device data
const data = {
trmnl: {
plugin_settings: {
...settings,
custom_fields_values: settings.custom_fields_values || {}
},
...deviceData
}
};
const value = getNestedValue(data, path.trim());
return value !== undefined ? value : match; // Keep original token if path not found
});
return url;
}
// Make sure these are properly added to the config object
config.getNestedValue = getNestedValue;
config.replaceTokensInUrl = replaceTokensInUrl;
// Update readEnvVars to properly clean header values
async function readEnvVars(pluginId) {
const pluginPath = this.getPluginPath(pluginId);
let envVars = {};
try {
const env = await fs.readFile(path.join(pluginPath, '.env'), 'utf8');
envVars = env.split('\n').reduce((acc, line) => {
line = line.trim();
if (!line || line.startsWith('#')) return acc;
// Split only on the first '=' to handle values that contain '='
const splitIndex = line.indexOf('=');
if (splitIndex === -1) return acc;
const key = line.slice(0, splitIndex).trim();
const value = line.slice(splitIndex + 1).trim();
if (key && value) {
// Handle multi-line headers by joining with commas
if (key === 'HEADERS') {
try {
// Parse and stringify to validate and clean the JSON
acc[key] = JSON.stringify(JSON.parse(value));
} catch (e) {
console.warn(`Warning: Invalid JSON in HEADERS env var for plugin ${pluginId}`);
}
} else {
acc[key] = value;
acc[key.toUpperCase()] = value;
}
}
return acc;
}, {});
} catch (err) {
console.warn(`Warning: No .env file found for plugin ${pluginId}`);
}
return envVars;
}
// Add readEnvVars to the config object
config.readEnvVars = readEnvVars;
// Add all functions to config object first
config.getPluginInfoWithTokens = getPluginInfoWithTokens;
config.getNestedValue = getNestedValue;
config.replaceTokensInUrl = replaceTokensInUrl;
config.readEnvVars = readEnvVars;
config.isPluginDirectory = isPluginDirectory;
config.getAvailablePlugins = getAvailablePlugins;
// Then bind all methods to config
config.getPluginInfoWithTokens = config.getPluginInfoWithTokens.bind(config);
config.getNestedValue = config.getNestedValue.bind(config);
config.replaceTokensInUrl = config.replaceTokensInUrl.bind(config);
config.readEnvVars = config.readEnvVars.bind(config);
config.getPluginInfo = config.getPluginInfo.bind(config);
config.getPluginPath = config.getPluginPath.bind(config);
config.isPluginDirectory = config.isPluginDirectory.bind(config);
config.getAvailablePlugins = config.getAvailablePlugins.bind(config);
// Call initialize when the module loads
config.initialize().catch(err => {
console.error('Error initializing config:', err);
});
module.exports = config;