summaryrefslogtreecommitdiff
path: root/src/main/compilationManager.ts
blob: 8fbd94659dc5f0860d6bb597f6f02a3f398774d9 (plain)
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
// Manages temp directory for Overleaf socket-mode compilation
import { join, basename } from 'path'
import { writeFile, mkdir, rm } from 'fs/promises'
import { existsSync } from 'fs'
import { spawn } from 'child_process'
import { net } from 'electron'

export class CompilationManager {
  private tmpDir: string
  private projectId: string
  private cookie: string
  private docContents = new Map<string, string>() // docPath → content
  private fileRefCache = new Map<string, boolean>() // fileRefPath → downloaded

  constructor(projectId: string, cookie: string) {
    this.projectId = projectId
    this.cookie = cookie
    this.tmpDir = join(require('os').tmpdir(), `lattex-${projectId}`)
  }

  get dir(): string {
    return this.tmpDir
  }

  /** Check if a doc is already stored */
  hasDoc(relativePath: string): boolean {
    return this.docContents.has(relativePath)
  }

  /** Store doc content (called when docs are joined/updated) */
  setDocContent(relativePath: string, content: string) {
    // Strip C1 control characters (U+0080-U+009F) — Overleaf embeds these as
    // range markers for tracked changes / comments. They break pdflatex.
    this.docContents.set(relativePath, content.replace(/[\u0080-\u009F]/g, ''))
  }

  /** Write all doc contents to disk */
  async syncDocs(): Promise<void> {
    await mkdir(this.tmpDir, { recursive: true })
    for (const [relPath, content] of this.docContents) {
      const fullPath = join(this.tmpDir, relPath)
      const dir = fullPath.substring(0, fullPath.lastIndexOf('/'))
      await mkdir(dir, { recursive: true })
      await writeFile(fullPath, content, 'utf-8')
    }
  }

  /** Download a binary file (image, .bst, etc.) from Overleaf */
  async downloadFile(fileRefId: string, relativePath: string): Promise<void> {
    if (this.fileRefCache.has(relativePath)) return

    const fullPath = join(this.tmpDir, relativePath)
    const dir = fullPath.substring(0, fullPath.lastIndexOf('/'))
    await mkdir(dir, { recursive: true })

    return new Promise((resolve, reject) => {
      const url = `https://www.overleaf.com/project/${this.projectId}/file/${fileRefId}`
      const req = net.request(url)
      req.setHeader('Cookie', this.cookie)
      req.setHeader('User-Agent', 'Mozilla/5.0')

      const chunks: Buffer[] = []
      req.on('response', (res) => {
        res.on('data', (chunk) => chunks.push(chunk as Buffer))
        res.on('end', async () => {
          try {
            const { writeFile: wf } = await import('fs/promises')
            await wf(fullPath, Buffer.concat(chunks))
            this.fileRefCache.set(relativePath, true)
            resolve()
          } catch (e) {
            reject(e)
          }
        })
      })
      req.on('error', reject)
      req.end()
    })
  }

  /** Download all binary files in the project */
  async syncBinaries(fileRefs: Array<{ id: string; path: string }>): Promise<void> {
    for (const ref of fileRefs) {
      try {
        await this.downloadFile(ref.id, ref.path)
      } catch (e) {
        console.log(`[CompilationManager] failed to download ${ref.path}:`, e)
      }
    }
  }

  /** Run latexmk compilation */
  async compile(
    mainTexRelPath: string,
    onLog: (data: string) => void
  ): Promise<{ success: boolean; log: string; pdfPath: string }> {
    await this.syncDocs()

    const texPaths = [
      '/Library/TeX/texbin',
      '/usr/local/texlive/2024/bin/universal-darwin',
      '/usr/texbin',
      '/opt/homebrew/bin'
    ]
    const envPath = texPaths.join(':') + ':' + (process.env.PATH || '')

    // Use // suffix for recursive search of ALL subdirectories in the project tree.
    // This ensures .sty, .bst, .cls, images, etc. are always found regardless of nesting.
    const texInputs = `${this.tmpDir}//:`
    const texBase = basename(mainTexRelPath, '.tex')
    const pdfPath = join(this.tmpDir, texBase + '.pdf')

    const args = [
      '-pdf', '-f', '-g', '-bibtex', '-synctex=1',
      '-interaction=nonstopmode', '-file-line-error',
      '-outdir=' + this.tmpDir,
      mainTexRelPath
    ]
    console.log('[compile] cwd:', this.tmpDir)
    console.log('[compile] args:', args.join(' '))
    console.log('[compile] TEXINPUTS:', texInputs)
    console.log('[compile] pdfPath:', pdfPath)
    console.log('[compile] docs synced:', this.docContents.size, 'files:', [...this.docContents.keys()].slice(0, 5))

    return new Promise((resolve) => {
      let log = ''
      const proc = spawn('latexmk', args, {
        cwd: this.tmpDir,
        env: { ...process.env, PATH: envPath, TEXINPUTS: texInputs, BIBINPUTS: texInputs, BSTINPUTS: texInputs }
      })

      proc.stdout.on('data', (data) => {
        const s = data.toString()
        log += s
        onLog(s)
      })

      proc.stderr.on('data', (data) => {
        const s = data.toString()
        log += s
        onLog(s)
      })

      proc.on('close', (code) => {
        resolve({ success: code === 0, log, pdfPath })
      })

      proc.on('error', (err) => {
        resolve({ success: false, log: log + '\n' + err.message, pdfPath })
      })
    })
  }

  /** Clean up temp directory */
  async cleanup(): Promise<void> {
    try {
      if (existsSync(this.tmpDir)) {
        await rm(this.tmpDir, { recursive: true })
      }
    } catch { /* ignore */ }
  }
}