Skip to content

Commit

Permalink
fix(cli/printer.js): ensure writeFile creates filePath directories if…
Browse files Browse the repository at this point in the history
… they don't exist

Signed-off-by: Nathanael Bracy <[email protected]>
  • Loading branch information
servusdei2018 committed May 10, 2024
1 parent 369979f commit 9492cba
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cli/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import fs from 'fs';
import path from 'path';

import log from 'lighthouse-logger';

Expand Down Expand Up @@ -58,13 +59,17 @@ function writeToStdout(output) {
*/
function writeFile(filePath, output, outputMode) {
return new Promise((resolve, reject) => {
// TODO: make this mkdir to the filePath.
fs.writeFile(filePath, output, (err) => {
if (err) {
fs.mkdir(path.dirname(filePath), { recursive: true }, (err) => {
if (err && err.code !== 'EEXIST') {
return reject(err);
}
log.log('Printer', `${OutputMode[outputMode]} output written to ${filePath}`);
resolve();
fs.writeFile(filePath, output, (err) => {
if (err) {
return reject(err);
}
log.log('Printer', `${LH.OutputMode[outputMode]} output written to ${filePath}`);
resolve();
});
});
});
}
Expand Down

0 comments on commit 9492cba

Please sign in to comment.