-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1288 lines (1141 loc) · 55.2 KB
/
Copy pathserver.js
File metadata and controls
1288 lines (1141 loc) · 55.2 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// server.js - Backend dla WordPress Git Publisher
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const fs = require('fs').promises;
const path = require('path');
const { execSync } = require('child_process');
const axios = require('axios');
const ejs = require('ejs');
const app = express();
const PORT = process.env.PORT || 3001;
// Enhanced CORS configuration
const corsOptions = {
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
optionsSuccessStatus: 200 // Some legacy browsers choke on 204
};
// Middleware
app.use(cors(corsOptions));
app.use(express.json());
// Configure EJS with minimal settings
app.engine('html', (filePath, data, callback) => {
// Create config object with 'it' as the root object
const config = {
it: {
API_URL: process.env.API_URL || `http://localhost:${process.env.PORT || 3001}`,
OLLAMA_BASE_URL: process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
DEFAULT_MODEL: process.env.DEFAULT_MODEL || 'llama2',
WORDPRESS_URL: process.env.WORDPRESS_URL || '',
WORDPRESS_USERNAME: process.env.WORDPRESS_USERNAME || '',
WORDPRESS_TOKEN: process.env.WORDPRESS_TOKEN || '',
NODE_ENV: process.env.NODE_ENV || 'development'
},
...data // Allow route-specific data to override
};
console.log('Rendering template with config:', {
...config.it,
WORDPRESS_TOKEN: config.it.WORDPRESS_TOKEN ? '***' : 'not set'
});
ejs.renderFile(filePath, config, {
root: path.join(__dirname, 'views'),
cache: false,
compileDebug: true,
_with: false,
strict: false,
delimiter: '%',
async: false,
localsName: 'it' // Use 'it' as the locals variable name
}, (err, str) => {
if (err) {
console.error('EJS render error:', err);
return callback(err);
}
callback(null, str);
});
});
// Set the view engine to use .html files
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
// Log the views directory for debugging
console.log('Views directory:', path.join(__dirname, 'views'));
// Serve static files from the 'public' directory
app.use(express.static('public', {
setHeaders: function (res, path) {
if (path.endsWith('.css')) {
res.setHeader('Content-Type', 'text/css');
} else if (path.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript');
}
}
}));
// Helper function to parse .env file
function parseEnvFile(content) {
return content.split('\n').reduce((acc, line) => {
const [key, ...values] = line.split('=');
if (key && key.trim() !== '' && !key.startsWith('#')) {
acc[key.trim()] = values.join('=').trim();
}
return acc;
}, {});
}
// Helper function to create the HTML template
function createHtmlTemplate(envVars, host) {
const safeEnv = {
WORDPRESS_URL: envVars.WORDPRESS_URL || '',
WORDPRESS_USERNAME: envVars.WORDPRESS_USERNAME || '',
WORDPRESS_TOKEN: envVars.WORDPRESS_TOKEN || '',
OLLAMA_BASE_URL: envVars.OLLAMA_BASE_URL || 'http://localhost:11434',
DEFAULT_MODEL: envVars.DEFAULT_MODEL || 'llama2'
};
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Git2WP - WordPress Git Publisher</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body { padding: 20px; background-color: #f8f9fa; }
.settings-section { margin-bottom: 30px; }
.card { margin-bottom: 20px; box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075); border: none; border-radius: 0.5rem; }
.card-header { font-weight: 600; border-bottom: 1px solid rgba(0,0,0,.125); background-color: #f8f9fa; }
.loading { opacity: 0.7; pointer-events: none; }
.form-label { font-weight: 500; color: #495057; }
.btn-primary { background-color: #0d6efd; border: none; }
.btn-primary:hover { background-color: #0b5ed7; }
.repo-card { transition: transform 0.2s; cursor: pointer; }
.repo-card:hover { transform: translateY(-2px); }
.repo-org { color: #6c757d; font-size: 0.9em; }
.repo-desc { color: #6c757d; font-size: 0.9em; }
.repo-meta { font-size: 0.8em; color: #6c757d; }
.repo-badge { font-size: 0.75em; margin-right: 5px; }
.spinner-border { width: 1rem; height: 1rem; border-width: 0.15em; }
.status-indicator { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 5px; }
.status-active { background-color: #198754; }
.status-inactive { background-color: #6c757d; }
.nav-tabs .nav-link { color: #6c757d; }
.nav-tabs .nav-link.active { font-weight: 600; color: #0d6efd; }
.log-entry { font-family: monospace; font-size: 0.85em; margin-bottom: 2px; padding: 2px 5px; border-radius: 3px; }
.log-entry.debug { color: #6c757d; }
.log-entry.info { color: #0d6efd; }
.log-entry.success { color: #198754; }
.log-entry.warning { color: #ffc107; }
.log-entry.error { color: #dc3545; background-color: #fff5f5; }
</head>
<body>
<div class="container">
<h1 class="my-4">Git2WP - WordPress Git Publisher</h1>
<!-- Status Bar -->
<div class="alert alert-info d-flex align-items-center" role="alert" id="status-bar" style="display: none;">
<div class="spinner-border spinner-border-sm me-2" role="status"></div>
<div id="status-message">Ładowanie...</div>
</div>
<div class="row">
<!-- Left Sidebar -->
<div class="col-md-4">
<!-- Settings Panel -->
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><i class="fas fa-cog me-2"></i>Ustawienia</h5>
</div>
<div class="card-body">
<form id="settings-form">
<div class="mb-3">
<label class="form-label">WordPress URL</label>
<input type="text" class="form-control" name="WORDPRESS_URL" value="${safeEnv.WORDPRESS_URL}" required>
</div>
<div class="mb-3">
<label class="form-label">WordPress Username</label>
<input type="text" class="form-control" name="WORDPRESS_USERNAME" value="${safeEnv.WORDPRESS_USERNAME}" required>
</div>
<div class="mb-3">
<label class="form-label">WordPress Token</label>
<div class="input-group">
<input type="password" class="form-control" name="WORDPRESS_TOKEN" value="${safeEnv.WORDPRESS_TOKEN}" required>
<button class="btn btn-outline-secondary" type="button" id="toggleToken">
<i class="fas fa-eye"></i>
</button>
</div>
</div>
<div class="mb-3">
<label class="form-label">Ollama URL</label>
<input type="url" class="form-control" name="OLLAMA_BASE_URL" value="${safeEnv.OLLAMA_BASE_URL}" required>
</div>
<div class="mb-3">
<label class="form-label">Domyślny model</label>
<input type="text" class="form-control" name="DEFAULT_MODEL" value="${safeEnv.DEFAULT_MODEL}">
</div>
<button type="submit" class="btn btn-primary">
<i class="fas fa-save me-1"></i> Zapisz ustawienia
</button>
</form>
</div>
</div>
<!-- Actions Panel -->
<div class="card">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><i class="fas fa-play-circle me-2"></i>Akcje</h5>
</div>
<div class="card-body">
<div class="d-grid gap-2">
<button id="scan-repos-btn" class="btn btn-outline-primary">
<i class="fas fa-sync-alt me-1"></i> Skanuj repozytoria
</button>
<button id="publish-changes-btn" class="btn btn-success" disabled>
<i class="fas fa-upload me-1"></i> Opublikuj zmiany
</button>
</div>
</div>
</div>
</div>
<!-- Main Content -->
<div class="col-md-8">
<!-- Repository List -->
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0"><i class="fas fa-code-branch me-2"></i>Repozytoria</h5>
<div class="text-muted small" id="repo-stats">
<span id="repo-count">0</span> repozytoriów
</div>
</div>
<div class="card-body p-0">
<div class="list-group list-group-flush" id="repo-list">
<div class="text-center p-4 text-muted">
<i class="fas fa-folder-open fa-2x mb-2"></i>
<p class="mb-0">Kliknij przycisk "Skanuj repozytoria" aby załadować listę repozytoriów</p>
</div>
</div>
</div>
</div>
<!-- Logs Panel -->
<div class="card mt-4">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" id="logs-tab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="logs-tab" data-bs-toggle="tab" data-bs-target="#logs-content" type="button" role="tab">
<i class="fas fa-terminal me-1"></i> Logi
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="preview-tab" data-bs-toggle="tab" data-bs-target="#preview-content" type="button" role="tab">
<i class="fas fa-eye me-1"></i> Podgląd
</button>
</li>
</ul>
</div>
<div class="card-body p-0">
<div class="tab-content" id="logs-tab-content">
<div class="tab-pane fade show active p-3" id="logs-content" role="tabpanel" style="max-height: 300px; overflow-y: auto;">
<div id="log-entries">
<div class="log-entry info">Witaj w Git2WP! Rozpocznij od skanowania repozytoriów.</div>
</div>
</div>
<div class="tab-pane fade p-3" id="preview-content" role="tabpanel">
<div id="article-preview" class="p-2">
<p class="text-muted text-center my-4">Wybierz repozytorium, aby zobaczyć podgląd zmian</p>
</div>
</div>
</div>
</div>
</div>
<div class="card mt-3">
<div class="card-header bg-success text-white">
<h5 class="mb-0"><i class="fas fa-rocket me-2"></i>Akcje</h5>
</div>
<div class="card-body">
<button id="scan-repos" class="btn btn-outline-primary w-100 mb-2">
<i class="fas fa-search me-2"></i>Skanuj repozytoria
</button>
<button id="publish-btn" class="btn btn-success w-100" disabled>
<i class="fas fa-paper-plane me-2"></i>Opublikuj zmiany
</button>
</div>
</div>
</div>
<!-- Main Content -->
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h5 class="mb-0">Podgląd artykułu</h5>
</div>
<div class="card-body">
<div id="article-preview" class="border p-3 bg-light" style="min-height: 200px;">
Wybierz repozytorium i kliknij "Skanuj repozytoria" aby rozpocząć.
</div>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
<h5 class="mb-0">Logi</h5>
</div>
<div class="card-body p-0">
<pre id="logs" class="p-3 mb-0 bg-dark text-white" style="min-height: 200px; max-height: 300px; overflow-y: auto;"></pre>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// UI Elements
const settingsForm = document.getElementById('settings-form');
const scanReposBtn = document.getElementById('scan-repos-btn');
const publishBtn = document.getElementById('publish-changes-btn');
const repoList = document.getElementById('repo-list');
const repoCount = document.getElementById('repo-count');
const logEntries = document.getElementById('log-entries');
const statusBar = document.getElementById('status-bar');
const statusMessage = document.getElementById('status-message');
const toggleTokenBtn = document.getElementById('toggleToken');
const tokenInput = document.querySelector('input[name="WORDPRESS_TOKEN"]');
const articlePreview = document.getElementById('article-preview');
// API URL
const API_URL = '/api';
// Toggle token visibility
if (toggleTokenBtn && tokenInput) {
toggleTokenBtn.addEventListener('click', function() {
const type = tokenInput.type === 'password' ? 'text' : 'password';
tokenInput.type = type;
const icon = this.querySelector('i');
icon.className = type === 'password' ? 'fas fa-eye' : 'fas fa-eye-slash';
// Focus back on the input
tokenInput.focus();
});
}
// Show status message
function showStatus(message, type = 'info') {
statusMessage.textContent = message;
statusBar.className = 'alert alert-' + type + ' d-flex align-items-center';
statusBar.style.display = 'flex';
// Auto-hide success messages after 5 seconds
if (type === 'success') {
setTimeout(() => {
statusBar.style.display = 'none';
}, 5000);
}
}
// Add log entry
function addLogEntry(message, type = 'info') {
const entry = document.createElement('div');
entry.className = 'log-entry ' + type;
const timestamp = new Date().toLocaleTimeString();
entry.innerHTML = '[' + timestamp + '] ' + message;
logEntries.prepend(entry);
// Auto-scroll to top
logEntries.scrollTop = 0;
}
// Toggle loading state
function setLoading(loading) {
if (loading) {
document.body.classList.add('loading');
if (scanReposBtn) {
scanReposBtn.disabled = true;
scanReposBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1" role="status"></span> Skanowanie...';
}
} else {
document.body.classList.remove('loading');
if (scanReposBtn) {
scanReposBtn.disabled = false;
scanReposBtn.innerHTML = '<i class="fas fa-sync-alt me-1"></i> Skanuj repozytoria';
}
}
}
// Save settings
if (settingsForm) {
settingsForm.addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(settingsForm);
const data = {};
formData.forEach((value, key) => data[key] = value);
try {
setLoading(true);
showStatus('Zapisywanie ustawień...', 'info');
const response = await fetch(API_URL + '/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await response.json();
if (result.success) {
showStatus('Ustawienia zostały zapisane pomyślnie', 'success');
addLogEntry('Zaktualizowano ustawienia aplikacji', 'success');
} else {
throw new Error(result.error || 'Nie udało się zapisać ustawień');
}
} catch (error) {
console.error('Error saving settings:', error);
showStatus(`Błąd: ${error.message}`, 'danger');
addLogEntry(`Błąd podczas zapisywania ustawień: ${error.message}`, 'error');
} finally {
setLoading(false);
}
});
}
// Display repositories in the UI
function displayRepositories(repositories) {
if (!repositories || !repositories.length) {
repoList.innerHTML = `
<div class="text-center p-4 text-muted">
<i class="fas fa-exclamation-circle fa-2x mb-2"></i>
<p class="mb-0">Nie znaleziono żadnych repozytoriów</p>
</div>`;
repoCount.textContent = '0';
return;
}
repoList.innerHTML = repositories.map(repo => `
<div class="list-group-item list-group-item-action repo-card" data-repo-path="${repo.path}">
<div class="d-flex w-100 justify-content-between">
<h6 class="mb-1">
<i class="fas fa-code-branch me-2"></i>${repo.name}
<span class="badge bg-secondary repo-badge">${repo.organization}</span>
</h6>
<small class="text-muted">${new Date(repo.lastModified).toLocaleDateString()}</small>
</div>
<div class="mb-1">
<small class="repo-desc">${repo.lastCommit?.message || 'Brak wiadomości commit'}</small>
</div>
<div class="d-flex justify-content-between align-items-center">
<div class="repo-meta">
<span class="me-2">
<i class="fas fa-code-branch"></i> ${repo.currentBranch || 'main'}
</span>
<span>
<i class="fas fa-user"></i> ${repo.lastCommit?.author || 'Brak danych'}
</span>
</div>
<span class="badge bg-primary">
${repo.lastCommit?.hash ? repo.lastCommit.hash.substring(0, 7) : 'N/A'}
</span>
</div>
</div>
`).join('');
repoCount.textContent = repositories.length;
// Add click handlers to repo cards
document.querySelectorAll('.repo-card').forEach(card => {
card.addEventListener('click', function() {
// Remove active class from all cards
document.querySelectorAll('.repo-card').forEach(c => c.classList.remove('active'));
// Add active class to clicked card
this.classList.add('active');
// Enable publish button when a repo is selected
if (publishBtn) {
publishBtn.disabled = false;
}
// Update article preview
const repoName = this.querySelector('h6').textContent.trim();
const repoPath = this.dataset.repoPath;
updateArticlePreview(repoName, repoPath);
addLogEntry(`Wybrano repozytorium: ${repoName}`, 'info');
});
});
}
// Update article preview
async function updateArticlePreview(repoName, repoPath) {
articlePreview.innerHTML = `
<div class="text-center py-4">
<div class="spinner-border text-primary mb-3" role="status">
<span class="visually-hidden">Ładowanie...</span>
</div>
<p>Ładowanie podglądu dla: <strong>${repoName}</strong></p>
</div>`;
try {
// Here you would fetch the actual article content
// For now, we'll simulate a delay and show a placeholder
await new Promise(resolve => setTimeout(resolve, 1000));
articlePreview.innerHTML = `
<h4>${repoName}</h4>
<p class="text-muted">Ścieżka: ${repoPath}</p>
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i>
Tutaj będzie wyświetlany podgląd artykułu wygenerowanego na podstawie zmian w repozytorium.
</div>`;
} catch (error) {
console.error('Error loading article preview:', error);
articlePreview.innerHTML = `
<div class="alert alert-danger">
<i class="fas fa-exclamation-triangle me-2"></i>
Wystąpił błąd podczas ładowania podglądu: ${error.message}
</div>`;
}
}
// Scan repositories
if (scanReposBtn) {
scanReposBtn.addEventListener('click', async function() {
try {
setLoading(true);
showStatus('Skanowanie repozytoriów...', 'info');
addLogEntry('Rozpoczęto skanowanie repozytoriów', 'info');
const response = await fetch(API_URL + '/scan-repositories');
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || 'Nie udało się przeskanować repozytoriów');
}
displayRepositories(result.repositories);
showStatus(`Znaleziono ${result.repositories.length} repozytoriów`, 'success');
addLogEntry(`Zakończono skanowanie. Znaleziono ${result.repositories.length} repozytoriów`, 'success');
} catch (error) {
console.error('Error scanning repositories:', error);
showStatus(`Błąd: ${error.message}`, 'danger');
addLogEntry(`Błąd podczas skanowania repozytoriów: ${error.message}`, 'error');
} finally {
setLoading(false);
}
});
}
// Publish changes
if (publishBtn) {
publishBtn.addEventListener('click', async function() {
const selectedRepo = document.querySelector('.repo-card.active');
if (!selectedRepo) {
showStatus('Wybierz repozytorium do opublikowania', 'warning');
return;
}
const repoPath = selectedRepo.dataset.repoPath;
const repoName = selectedRepo.querySelector('h6').textContent.trim();
try {
setLoading(true);
showStatus(`Publikowanie zmian dla ${repoName}...`, 'info');
addLogEntry(`Rozpoczęto publikowanie zmian dla ${repoName}`, 'info');
// TODO: Implement actual publish functionality
await new Promise(resolve => setTimeout(resolve, 1500));
showStatus(`Pomyślnie opublikowano zmiany dla ${repoName}`, 'success');
addLogEntry(`Zakończono publikowanie zmian dla ${repoName}`, 'success');
} catch (error) {
console.error('Error publishing changes:', error);
showStatus(`Błąd podczas publikowania zmian: ${error.message}`, 'danger');
addLogEntry(`Błąd podczas publikowania zmian: ${error.message}`, 'error');
} finally {
setLoading(false);
}
});
}
// Initialize tooltips
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
// Add initial log entry
addLogEntry('Aplikacja została załadowana. Rozpocznij od skanowania repozytoriów.', 'info');
</script>
</body>
</html>`;
}
// Serve the main page with all settings
app.get('/', (req, res) => {
try {
// Read the .env file
const envContent = fs.readFileSync('.env', 'utf8');
const envVars = parseEnvFile(envContent);
// Send the main HTML with the settings
const html = createHtmlTemplate(envVars, req.headers.host);
res.send(html);
} catch (error) {
console.error('Error serving main page:', error);
res.status(500).send('Wystąpił błąd podczas ładowania strony');
}
});
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded({ extended: true }));
// Logging helper
const log = (message, type = 'info') => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] [${type.toUpperCase()}] ${message}`);
};
// Log all requests
app.use((req, res, next) => {
log(`[${req.method}] ${req.originalUrl}`, 'debug');
next();
});
// Handle preflight requests
app.options('*', cors(corsOptions));
// Git helper functions
class GitScanner {
constructor() {
this.gitPath = '';
}
setGitPath(gitPath) {
this.gitPath = gitPath;
}
/**
* Get commit history from the Git repository
* @returns {Promise<Array>} Array of commit objects
*/
async getCommitHistory(limit = 50) {
try {
// Change to the repository directory
process.chdir(this.gitPath);
// Execute git log command to get commit history
const command = `git log --pretty=format:'%H|%an|%ae|%ad|%s' --date=iso -n ${limit}`;
const output = execSync(command, { encoding: 'utf8' });
// Parse the git log output
const commits = output.trim().split('\n')
.filter(line => line.trim() !== '')
.map(line => {
const [hash, author, email, date, ...messageParts] = line.split('|');
const message = messageParts.join('|');
return {
hash,
author,
email,
date: new Date(date.trim()),
message: message.trim()
};
});
return commits;
} catch (error) {
console.error('Error getting commit history:', error);
throw new Error(`Failed to get commit history: ${error.message}`);
}
}
}
// Ollama helper class
class OllamaClient {
constructor(baseUrl = 'http://localhost:11434') {
this.baseUrl = baseUrl.trim();
if (this.baseUrl.endsWith('/')) {
this.baseUrl = this.baseUrl.slice(0, -1);
}
log(`OllamaClient initialized with base URL: ${this.baseUrl}`, 'debug');
}
// ... (rest of OllamaClient class implementation)
}
// WordPress helper class
class WordPressClient {
constructor(url, username, token) {
this.baseUrl = url;
this.username = username;
this.token = token;
// Log the initialization (without showing the actual token for security)
console.log(`WordPress client initialized for: ${url}`);
console.log(`Using username: ${username}`);
console.log('Token is ' + (token ? 'set' : 'not set'));
}
/**
* Test WordPress connection
* @returns {Promise<Object>} Connection test result
*/
async testConnection() {
try {
const response = await axios.get(`${this.baseUrl}/wp-json/wp/v2/posts`, {
auth: {
username: this.username,
password: this.token
},
params: {
per_page: 1,
page: 1
},
headers: {
'Content-Type': 'application/json'
}
});
return {
success: true,
postsCount: response.headers['x-wp-total'] || 'unknown',
message: 'Successfully connected to WordPress'
};
} catch (error) {
console.error('WordPress connection error:', error.response?.data || error.message);
return {
success: false,
error: error.response?.data?.message || error.message || 'Failed to connect to WordPress'
};
}
}
/**
* Create a new post in WordPress
* @param {string} title - Post title
* @param {string} content - Post content
* @param {string} status - Post status (draft, publish, etc.)
* @returns {Promise<Object>} Created post data
*/
async createPost(title, content, status = 'draft') {
try {
const response = await axios.post(
`${this.baseUrl}/wp-json/wp/v2/posts`,
{
title,
content,
status
},
{
auth: {
username: this.username,
password: this.token
},
headers: {
'Content-Type': 'application/json'
}
}
);
return {
success: true,
postId: response.data.id,
link: response.data.link,
message: 'Post created successfully'
};
} catch (error) {
console.error('Error creating WordPress post:', error.response?.data || error.message);
return {
success: false,
error: error.response?.data?.message || error.message || 'Failed to create post'
};
}
}
}
// Initialize services
const gitScanner = new GitScanner();
// API Routes
// Test WordPress connection
app.post('/api/wordpress/test', async (req, res) => {
const { url, username, password } = req.body;
if (!url || !username || !password) {
return res.status(400).json({
success: false,
error: 'Missing required fields (url, username, password)'
});
}
try {
const wordpress = new WordPressClient(url, username, password);
const result = await wordpress.testConnection();
if (result.success) {
res.json({
success: true,
message: result.message,
postsCount: result.postsCount
});
} else {
res.status(500).json({
success: false,
error: result.error || 'Failed to connect to WordPress'
});
}
} catch (error) {
console.error('Error testing WordPress connection:', error);
res.status(500).json({
success: false,
error: error.message || 'Internal server error'
});
}
});
// Publish to WordPress
app.post('/api/wordpress/publish', async (req, res) => {
const { url, username, password, title, content, status = 'draft' } = req.body;
if (!url || !username || !password || !title || !content) {
return res.status(400).json({
success: false,
error: 'Missing required fields (url, username, password, title, content)'
});
}
try {
const wordpress = new WordPressClient(url, username, password);
const result = await wordpress.createPost(title, content, status);
if (result.success) {
res.json({
success: true,
message: result.message,
postId: result.postId,
link: result.link
});
} else {
res.status(500).json({
success: false,
error: result.error || 'Failed to publish to WordPress'
});
}
} catch (error) {
console.error('Error publishing to WordPress:', error);
res.status(500).json({
success: false,
error: error.message || 'Internal server error'
});
}
});
// Configuration endpoint
app.get('/api/config', async (req, res) => {
try {
const config = {
API_URL: process.env.API_URL || `http://localhost:${PORT}`,
OLLAMA_BASE_URL: process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
DEFAULT_MODEL: process.env.DEFAULT_MODEL || 'llama2',
WORDPRESS_URL: process.env.WORDPRESS_URL || '',
WORDPRESS_USERNAME: process.env.WORDPRESS_USERNAME || '',
// Don't send token to frontend
NODE_ENV: process.env.NODE_ENV || 'development'
};
res.json({ success: true, config });
} catch (error) {
log(`Error getting config: ${error.message}`, 'error');
res.status(500).json({ success: false, error: 'Failed to get configuration' });
}
});
app.post('/api/config', async (req, res) => {
try {
const { updates } = req.body;
if (!updates || typeof updates !== 'object') {
return res.status(400).json({ success: false, error: 'Invalid updates' });
}
// Read current .env content
const envPath = path.join(__dirname, '.env');
let envContent = '';
try {
envContent = await fs.readFile(envPath, 'utf8');
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
// Update .env file
const lines = envContent.split('\n');
for (const [key, value] of Object.entries(updates)) {
const index = lines.findIndex(line => line.startsWith(`${key}=`));
const newLine = `${key}=${value}`;
if (index >= 0) {
lines[index] = newLine;
} else {
lines.push(newLine);
}
}
// Write back to .env
await fs.writeFile(envPath, lines.join('\n'));
// Reload environment
Object.assign(process.env, updates);
res.json({ success: true, config: updates });
} catch (error) {
log(`Error updating config: ${error.message}`, 'error');
res.status(500).json({ success: false, error: 'Failed to update configuration' });
}
});
// Scan Git repository
app.post('/api/scan-git', async (req, res) => {
try {
const { repoPath } = req.body;
if (!repoPath) {
return res.status(400).json({
success: false,
error: 'Path to Git repository is required'
});
}
// Set the Git repository path
gitScanner.setGitPath(repoPath);
// Get commit history (you'll need to implement this method in GitScanner)
const commitHistory = await gitScanner.getCommitHistory();
res.json({
success: true,
data: {
path: repoPath,
commits: commitHistory,
lastScanned: new Date().toISOString()
}
});
} catch (error) {
console.error('Error scanning Git repository:', error);
res.status(500).json({
success: false,
error: 'Failed to scan Git repository',
details: error.message
});
}
});
// API Routes
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Get Git configuration
app.get('/api/config/git', (req, res) => {
try {
res.json({
success: true,
gitPath: process.env.GIT_PATH || '',
defaultBranch: process.env.DEFAULT_BRANCH || 'main',
lastSync: new Date().toISOString()
});
} catch (error) {
console.error('Error getting Git config:', error);
res.status(500).json({
success: false,
error: 'Failed to load Git configuration'
});
}
});
// Helper function to get repository details
async function getRepoDetails(repoPath) {
try {
const gitPath = path.join(repoPath, '.git');
const gitStat = await fs.stat(gitPath);
if (!gitStat.isDirectory()) {
return null;
}
// Get last commit info using git command
const getLastCommit = async () => {
try {
const { execSync } = require('child_process');
const cmd = `cd ${repoPath} && git log -1 --pretty=format:'%h|%an|%ae|%ad|%s' --date=iso`;
const output = execSync(cmd).toString().trim();
if (!output) return null;
const [hash, author, email, date, message] = output.split('|');
return { hash, author, email, date, message };
} catch (error) {
console.error(`Error getting commit info for ${repoPath}:`, error);
return null;
}
};
// Get branch info
const getCurrentBranch = async () => {
try {
const { execSync } = require('child_process');
const cmd = `cd ${repoPath} && git rev-parse --abbrev-ref HEAD`;