Skip to content

Commit 994b7af

Browse files
Nick Barnettvladgolubev
authored andcommitted
Replace execSync with exec and make convertTo async
1 parent cdaef67 commit 994b7af

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/cleanup.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import fs from 'fs';
1+
import fs from 'fs/promises';
22
import del from 'del';
33

44
// Removes temp files generated by LibreOffice
5-
export function cleanupTempFiles(): void {
6-
for (const file of fs.readdirSync(`/tmp`)) {
5+
export async function cleanupTempFiles(): Promise<void> {
6+
const files = await fs.readdir(`/tmp`);
7+
for (const file of files) {
78
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
89
try {
910
del.sync([`/tmp/${file}`, `/tmp/${file}/*`], {force: true});

src/convert.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import {execSync} from 'child_process';
1+
import childProcess from 'child_process';
2+
import util from 'util';
23
import {cleanupTempFiles} from './cleanup';
34
import {getConvertedFilePath} from './logs';
45

6+
const exec = util.promisify(childProcess.exec);
7+
58
export const DEFAULT_ARGS = [
69
'--headless',
710
'--invisible',
@@ -13,8 +16,8 @@ export const DEFAULT_ARGS = [
1316
];
1417
const LO_BINARY_PATH = 'libreoffice7.4';
1518

16-
export function convertTo(filename: string, format: string): string {
17-
cleanupTempFiles();
19+
export async function convertTo(filename: string, format: string): Promise<string> {
20+
await cleanupTempFiles();
1821

1922
const argumentsString = DEFAULT_ARGS.join(' ');
2023
const outputFilename = filename.split(/\\ /).join(' ');
@@ -25,13 +28,13 @@ export function convertTo(filename: string, format: string): string {
2528

2629
// due to an unknown issue, we need to run command twice
2730
try {
28-
logs = execSync(cmd);
31+
logs = (await exec(cmd)).stdout;
2932
} catch (e) {
30-
logs = execSync(cmd);
33+
logs = (await exec(cmd)).stdout;
3134
}
3235

33-
execSync(`rm '/tmp/${outputFilename}'`);
34-
cleanupTempFiles();
36+
await exec(`rm '/tmp/${outputFilename}'`);
37+
await cleanupTempFiles();
3538

3639
return getConvertedFilePath(logs.toString());
3740
}

0 commit comments

Comments
 (0)