mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { mkdir, writeFile } from 'fs'
|
|
import { spawn } from 'child_process'
|
|
import { v1 as uuidv1 } from 'uuid'
|
|
|
|
import { getDirPath, getDocPath, getTexCmd } from './utils'
|
|
|
|
function copyToTemp(id: string, texDocument: string): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const dirPath = getDirPath(id)
|
|
|
|
mkdir(dirPath, (err) => {
|
|
if (err) {
|
|
reject(err)
|
|
return
|
|
}
|
|
|
|
const docPath = getDocPath(id)
|
|
writeFile(docPath, texDocument, (err) => {
|
|
if (err) {
|
|
reject(err)
|
|
}
|
|
resolve(id)
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
function generateDoc(id: string) {
|
|
return new Promise((resolve, reject): void => {
|
|
const pdflatex = spawn(...getTexCmd(id))
|
|
pdflatex.stderr?.on('data', (data: string) => {
|
|
console.error('error: ', data)
|
|
reject()
|
|
})
|
|
pdflatex.on('error', (error) => {
|
|
console.error('error', error)
|
|
reject()
|
|
})
|
|
|
|
pdflatex.on('close', (code) => {
|
|
if (code !== null && code > 0) {
|
|
reject(`pdflatex returned with code ${code}`)
|
|
return
|
|
}
|
|
console.log(`PDF ${id} generated`)
|
|
resolve(id)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default async function (texDocument: string) {
|
|
const id = uuidv1()
|
|
const id_2 = await copyToTemp(id, texDocument)
|
|
return generateDoc(id_2)
|
|
}
|