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' const execPromise = promisify(exec) async function copyToTemp(id: string, texDocument: string): Promise { const dirPath = getDirPath(id) const docPath = getDocPath(id) await promises.mkdir(dirPath) await promises.writeFile(docPath, texDocument) } async function generateDoc(id: string): Promise { const { cmd, options } = getTexCmd(id) try { const { stdout } = await execPromise(cmd, options) stdout.length && console.log('stdout:', stdout) console.log(`PDF ${id} generated`) } catch (error) { console.error(error.stdout) console.error(error.toString()) throw new Error(error.message) } } export default async function (texDocument: string) { const id = uuidv1() await copyToTemp(id, texDocument) await generateDoc(id) return id }