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 { promisify } from 'util'
|
||||||
import { spawn } from 'child_process'
|
import { promises } from 'fs'
|
||||||
|
import { exec } from 'child_process'
|
||||||
import { v1 as uuidv1 } from 'uuid'
|
import { v1 as uuidv1 } from 'uuid'
|
||||||
|
|
||||||
import { getDirPath, getDocPath, getTexCmd } from './utils'
|
import { getDirPath, getDocPath, getTexCmd } from './utils'
|
||||||
|
|
||||||
function copyToTemp(id: string, texDocument: string): Promise<string> {
|
const execPromise = promisify(exec)
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
|
async function copyToTemp(id: string, texDocument: string): Promise<void> {
|
||||||
const dirPath = getDirPath(id)
|
const dirPath = getDirPath(id)
|
||||||
|
|
||||||
mkdir(dirPath, (err) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const docPath = getDocPath(id)
|
const docPath = getDocPath(id)
|
||||||
writeFile(docPath, texDocument, (err) => {
|
|
||||||
if (err) {
|
await promises.mkdir(dirPath)
|
||||||
reject(err)
|
await promises.writeFile(docPath, texDocument)
|
||||||
}
|
|
||||||
resolve(id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateDoc(id: string) {
|
async function generateDoc(id: string) {
|
||||||
return new Promise((resolve, reject): void => {
|
const { cmd, options } = getTexCmd(id)
|
||||||
const pdflatex = spawn(...getTexCmd(id))
|
const { stdout, stderr } = await execPromise(cmd, options)
|
||||||
pdflatex.stderr?.on('data', (data: string) => {
|
stdout.length && console.log('stdout:', stdout)
|
||||||
console.error('error: ', data)
|
stderr.length && console.error('error: ', stderr)
|
||||||
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`)
|
console.log(`PDF ${id} generated`)
|
||||||
resolve(id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function (texDocument: string) {
|
export default async function (texDocument: string) {
|
||||||
const id = uuidv1()
|
const id = uuidv1()
|
||||||
const id_2 = await copyToTemp(id, texDocument)
|
await copyToTemp(id, texDocument)
|
||||||
return generateDoc(id_2)
|
await generateDoc(id)
|
||||||
|
return id
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { CommonSpawnOptions } from 'node:child_process'
|
import { ExecOptions } from 'node:child_process'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
export function getDirPath(id: string) {
|
export function getDirPath(id: string) {
|
||||||
@@ -13,6 +13,9 @@ export function getPdfPath(id: string) {
|
|||||||
return path.join(getDirPath(id), 'doc.pdf')
|
return path.join(getDirPath(id), 'doc.pdf')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTexCmd(id: string): [command: string, args: ReadonlyArray<string>, options: CommonSpawnOptions] {
|
export function getTexCmd(id: string): { cmd: string; options: ExecOptions } {
|
||||||
return ['pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], { cwd: getDirPath(id) }]
|
return {
|
||||||
|
cmd: `pdflatex -interaction nonstopmode ${getDocPath(id)}`,
|
||||||
|
options: { cwd: getDirPath(id) },
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user