File size: 2,890 Bytes
aa8b9d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

import { spawn } from 'child_process';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

console.log('πŸš€ Starting development server with LaTeX watching...\n');

// Function to start a process
function startProcess(command, args, cwd, name, color) {
    const process = spawn(command, args, {
        cwd,
        stdio: 'pipe',
        shell: true
    });

    // Color codes
    const colors = {
        reset: '\x1b[0m',
        bright: '\x1b[1m',
        red: '\x1b[31m',
        green: '\x1b[32m',
        yellow: '\x1b[33m',
        blue: '\x1b[34m',
        magenta: '\x1b[35m',
        cyan: '\x1b[36m'
    };

    const prefix = `${colors[color]}[${name}]${colors.reset}`;

    process.stdout.on('data', (data) => {
        const lines = data.toString().split('\n').filter(line => line.trim());
        lines.forEach(line => {
            console.log(`${prefix} ${line}`);
        });
    });

    process.stderr.on('data', (data) => {
        const lines = data.toString().split('\n').filter(line => line.trim());
        lines.forEach(line => {
            console.log(`${prefix} ${colors.red}${line}${colors.reset}`);
        });
    });

    process.on('close', (code) => {
        console.log(`${prefix} ${colors.red}Process exited with code ${code}${colors.reset}`);
    });

    return process;
}

// Initial LaTeX conversion
console.log('πŸ“š Converting LaTeX to MDX initially...');
const initialConvert = spawn('npm', ['run', 'latex:convert'], {
    cwd: join(__dirname, '..'),
    stdio: 'inherit'
});

initialConvert.on('close', (code) => {
    if (code === 0) {
        console.log('βœ… Initial LaTeX conversion completed!\n');

        // Start LaTeX watcher
        const latexWatcher = startProcess(
            'npm',
            ['run', 'latex:watch'],
            join(__dirname, '..'),
            'LaTeX',
            'blue'
        );

        // Start Astro dev server
        const astroServer = startProcess(
            'npm',
            ['run', 'dev'],
            join(__dirname, '..'),
            'Astro',
            'green'
        );

        // Handle graceful shutdown
        process.on('SIGINT', () => {
            console.log('\nπŸ›‘ Shutting down development servers...');
            latexWatcher.kill();
            astroServer.kill();
            process.exit(0);
        });

        console.log('πŸŽ‰ Development environment ready!');
        console.log('   πŸ“ LaTeX files: Watch mode active for scripts/latex-to-mdx/input/');
        console.log('   🌐 Astro server: http://localhost:4321');
        console.log('   πŸ”„ Any change in LaTeX input/ will auto-regenerate MDX\n');

    } else {
        console.error('❌ Initial LaTeX conversion failed!');
        process.exit(1);
    }
});