( filePath: string, fileType: string, convertTo: string, targetPath: string, options?: unknown, execFile: ExecFileFn = execFileOriginal, )
| 135 | }; |
| 136 | |
| 137 | export function convert( |
| 138 | filePath: string, |
| 139 | fileType: string, |
| 140 | convertTo: string, |
| 141 | targetPath: string, |
| 142 | options?: unknown, |
| 143 | execFile: ExecFileFn = execFileOriginal, |
| 144 | ): Promise<string> { |
| 145 | const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath; |
| 146 | |
| 147 | // Build arguments array |
| 148 | const args: string[] = []; |
| 149 | args.push("--headless"); |
| 150 | const [inFilter, outFilter] = getFilters(fileType, convertTo); |
| 151 | |
| 152 | if (inFilter) { |
| 153 | args.push(`--infilter=${inFilter}`); |
| 154 | } |
| 155 | |
| 156 | if (outFilter) { |
| 157 | args.push("--convert-to", `${convertTo}:${outFilter}`, "--outdir", outputPath, filePath); |
| 158 | } else { |
| 159 | args.push("--convert-to", convertTo, "--outdir", outputPath, filePath); |
| 160 | } |
| 161 | |
| 162 | return new Promise((resolve, reject) => { |
| 163 | execFile("soffice", args, (error, stdout, stderr) => { |
| 164 | if (error) { |
| 165 | reject(`error: ${error}`); |
| 166 | } |
| 167 | |
| 168 | if (stdout) { |
| 169 | console.log(`stdout: ${stdout}`); |
| 170 | } |
| 171 | |
| 172 | if (stderr) { |
| 173 | console.error(`stderr: ${stderr}`); |
| 174 | } |
| 175 | |
| 176 | resolve("Done"); |
| 177 | }); |
| 178 | }); |
| 179 | } |
no test coverage detected