mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 14:37:21 +01:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { promisify } from 'util'
|
|
import { promises } from 'fs'
|
|
import { exec } from 'child_process'
|
|
import { getDirPath, getDocPath, getTexCmd } from './utils'
|
|
|
|
const execPromise = promisify(exec)
|
|
|
|
async function copyToTemp(id: string, texDocument: string): Promise<void> {
|
|
const dirPath = getDirPath(id)
|
|
const docPath = getDocPath(id)
|
|
|
|
try {
|
|
await promises.access(dirPath)
|
|
} catch (_) {
|
|
// well, seems it does not exists, let's create it
|
|
await promises.mkdir(dirPath)
|
|
}
|
|
await promises.writeFile(docPath, texDocument)
|
|
}
|
|
|
|
async function generateDoc(id: string): Promise<void> {
|
|
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 (id: string, texDocument: string) {
|
|
await copyToTemp(id, texDocument)
|
|
await generateDoc(id)
|
|
return id
|
|
}
|