convert renderer to async

This commit is contained in:
Thomas Ruoff
2021-03-05 23:58:44 +01:00
parent e601971a50
commit 9c5ec996d4
2 changed files with 24 additions and 46 deletions

View File

@@ -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
}

View File

@@ -1,4 +1,4 @@
import { CommonSpawnOptions } from 'node:child_process'
import { ExecOptions } from 'node:child_process'
import path from 'path'
export function getDirPath(id: string) {
@@ -13,6 +13,9 @@ export function getPdfPath(id: string) {
return path.join(getDirPath(id), 'doc.pdf')
}
export function getTexCmd(id: string): [command: string, args: ReadonlyArray<string>, options: CommonSpawnOptions] {
return ['pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], { cwd: getDirPath(id) }]
export function getTexCmd(id: string): { cmd: string; options: ExecOptions } {
return {
cmd: `pdflatex -interaction nonstopmode ${getDocPath(id)}`,
options: { cwd: getDirPath(id) },
}
}