tangled
alpha
login
or
join now
dunkirk.sh
/
zera
5
fork
atom
the home site for me: also iteration 3 or 4 of my site
5
fork
atom
overview
issues
pulls
pipelines
feat: sync just changed files
dunkirk.sh
2 months ago
7ceabef6
ae524368
verified
This commit was signed with the committer's
known signature
.
dunkirk.sh
SSH Key Fingerprint:
SHA256:DqcG0RXYExE26KiWo3VxJnsxswN1QNfTBvB+bdSpk80=
+47
-17
1 changed file
expand all
collapse all
unified
split
scripts
dev.ts
+47
-17
scripts/dev.ts
···
1
1
#!/usr/bin/env bun
2
2
3
3
import { watch } from 'fs';
4
4
-
import { existsSync } from 'fs';
4
4
+
import { existsSync, mkdirSync, copyFileSync, unlinkSync, rmSync } from 'fs';
5
5
import { spawn } from 'child_process';
6
6
+
import { dirname, join } from 'path';
6
7
7
8
let zolaProcess: any = null;
8
8
-
let isRebuilding = false;
9
9
10
10
function cleanup() {
11
11
if (zolaProcess) {
12
12
zolaProcess.kill();
13
13
}
14
14
+
rmSync('.zola-build', { recursive: true, force: true });
14
15
process.exit(0);
15
16
}
16
17
17
18
process.on('SIGINT', cleanup);
18
19
process.on('SIGTERM', cleanup);
19
20
20
20
-
async function buildShadow() {
21
21
-
if (isRebuilding) return;
22
22
-
isRebuilding = true;
23
23
-
24
24
-
if (zolaProcess) {
25
25
-
zolaProcess.kill();
26
26
-
zolaProcess = null;
21
21
+
function ensureDir(filePath: string) {
22
22
+
const dir = dirname(filePath);
23
23
+
if (!existsSync(dir)) {
24
24
+
mkdirSync(dir, { recursive: true });
27
25
}
26
26
+
}
28
27
28
28
+
function copyFile(src: string, dest: string) {
29
29
+
ensureDir(dest);
30
30
+
copyFileSync(src, dest);
31
31
+
}
32
32
+
33
33
+
function deleteFile(dest: string) {
34
34
+
if (existsSync(dest)) {
35
35
+
unlinkSync(dest);
36
36
+
}
37
37
+
}
38
38
+
39
39
+
async function initialSync() {
29
40
await Bun.$`rm -rf .zola-build`.quiet();
30
41
await Bun.$`mkdir -p .zola-build`.quiet();
31
42
await Bun.$`cp -r content .zola-build/`.quiet();
···
39
50
40
51
await Bun.$`cp config.toml .zola-build/`.quiet();
41
52
await Bun.$`bun run scripts/preprocess.ts .zola-build/content`.quiet();
53
53
+
}
54
54
+
55
55
+
async function handleFileChange(dir: string, filename: string) {
56
56
+
const srcPath = join(dir, filename);
57
57
+
const destPath = join('.zola-build', dir, filename);
58
58
+
59
59
+
if (existsSync(srcPath)) {
60
60
+
copyFile(srcPath, destPath);
61
61
+
if (dir === 'content' && filename.endsWith('.md')) {
62
62
+
await Bun.$`bun run scripts/preprocess.ts ${destPath}`.quiet();
63
63
+
}
64
64
+
} else {
65
65
+
deleteFile(destPath);
66
66
+
}
67
67
+
}
68
68
+
69
69
+
async function handleConfigChange() {
70
70
+
copyFile('config.toml', '.zola-build/config.toml');
71
71
+
}
72
72
+
73
73
+
async function startServer() {
74
74
+
await initialSync();
42
75
43
76
zolaProcess = spawn('zola', ['serve', '--force', '--interface', '0.0.0.0', '--output-dir', '../public'], {
44
77
cwd: '.zola-build',
···
48
81
zolaProcess.on('error', (err: Error) => {
49
82
console.error('Failed to start Zola:', err);
50
83
});
51
51
-
52
52
-
isRebuilding = false;
53
84
}
54
85
55
55
-
await buildShadow();
86
86
+
await startServer();
56
87
57
88
const watchDirs = ['content', 'templates', 'sass', 'static', 'syntaxes'];
58
89
59
90
for (const dir of watchDirs) {
60
91
if (existsSync(dir)) {
61
61
-
watch(dir, { recursive: true }, async (event, filename) => {
92
92
+
watch(dir, { recursive: true }, (event, filename) => {
62
93
if (filename && !filename.includes('.zola-build')) {
63
63
-
await buildShadow();
94
94
+
handleFileChange(dir, filename);
64
95
}
65
96
});
66
97
}
67
98
}
68
99
69
69
-
watch('config.toml', async () => {
70
70
-
await buildShadow();
100
100
+
watch('config.toml', () => {
101
101
+
handleConfigChange();
71
102
});
72
72
-