File size: 2,370 Bytes
f91a684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { spawn } from 'child_process';
import os from 'os';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const isWin = os.platform() === 'win32';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const runnerDir = path.join(__dirname, '.ryp-runner');
const goCacheDir = path.join(runnerDir, 'go-cache');
const goTmpDir = path.join(runnerDir, 'go-tmp');

async function run() {
  if (isWin) {
    process.env.GOROOT = 'C:\\Program Files\\Go';
  }

  fs.mkdirSync(runnerDir, { recursive: true });
  fs.mkdirSync(goCacheDir, { recursive: true });
  fs.mkdirSync(goTmpDir, { recursive: true });

  const exeName = isWin ? '.ryp-runner/server.exe' : '.ryp-runner/server';
  const backendEnv = {
    ...process.env,
    GOCACHE: goCacheDir,
    GOROOT: process.env.GOROOT,
    GOTMPDIR: goTmpDir,
  };

  console.log('Building Go backend...');
  const build = spawn('go', ['build', '-o', exeName, './cmd/server'], {
    cwd: __dirname,
    env: backendEnv,
    stdio: 'inherit',
    shell: false,
  });

  build.on('error', (err) => {
    console.error('Failed to run go build:', err.message);
    process.exit(1);
  });

  build.on('close', (code) => {
    if (code !== 0) {
      console.error(`Go build failed with exit code ${code}`);
      process.exit(code);
    }

    console.log('Starting Go backend server...');
    const serverBin = path.resolve(__dirname, exeName);
    const runServer = spawn(serverBin, [], {
      cwd: __dirname,
      env: backendEnv,
      stdio: 'inherit',
      shell: false,
    });

    runServer.on('error', (err) => {
      console.error('Failed to start backend server:', err.message);
      process.exit(1);
    });

    runServer.on('close', (exitCode) => {
      console.log(`Backend server stopped with code ${exitCode}`);
      process.exit(exitCode ?? 0);
    });

    console.log('Starting Node.js compiler server...');
    const runCompiler = spawn('node', ['server/index.js'], {
      cwd: __dirname,
      env: process.env,
      stdio: 'inherit',
      shell: false,
    });

    runCompiler.on('error', (err) => {
      console.error('Failed to start Node compiler server:', err.message);
    });

    runCompiler.on('close', (exitCode) => {
      console.log(`Node compiler server stopped with code ${exitCode}`);
    });
  });
}

run();