mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
convert renderer to async
This commit is contained in:
@@ -1,55 +1,30 @@
|
||||
import { mkdir, writeFile } from 'fs'
|
||||
import { spawn } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
import { promises } from 'fs'
|
||||
import { exec } 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)
|
||||
const execPromise = promisify(exec)
|
||||
|
||||
mkdir(dirPath, (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
async function copyToTemp(id: string, texDocument: string): Promise<void> {
|
||||
const dirPath = getDirPath(id)
|
||||
const docPath = getDocPath(id)
|
||||
|
||||
const docPath = getDocPath(id)
|
||||
writeFile(docPath, texDocument, (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
}
|
||||
resolve(id)
|
||||
})
|
||||
})
|
||||
})
|
||||
await promises.mkdir(dirPath)
|
||||
await promises.writeFile(docPath, texDocument)
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
async function generateDoc(id: string) {
|
||||
const { cmd, options } = getTexCmd(id)
|
||||
const { stdout, stderr } = await execPromise(cmd, options)
|
||||
stdout.length && console.log('stdout:', stdout)
|
||||
stderr.length && console.error('error: ', stderr)
|
||||
console.log(`PDF ${id} generated`)
|
||||
}
|
||||
|
||||
export default async function (texDocument: string) {
|
||||
const id = uuidv1()
|
||||
const id_2 = await copyToTemp(id, texDocument)
|
||||
return generateDoc(id_2)
|
||||
await copyToTemp(id, texDocument)
|
||||
await generateDoc(id)
|
||||
return id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user