Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure output directory is presen…
Browse files Browse the repository at this point in the history
…t before writing stats JSON

If an error occurs during a build, it is possible that the output path directory may not be present and then cause the stats JSON file write to fail.

Fixes: angular#20349
  • Loading branch information
clydin committed Mar 22, 2021
1 parent 09daf7a commit 62410b4
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
buildOptimizerLoaderPath,
} from '@angular-devkit/build-optimizer';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { createWriteStream, existsSync } from 'fs';
import { createWriteStream, existsSync, promises as fsPromises } from 'fs';
import * as path from 'path';
import { ScriptTarget } from 'typescript';
import {
Expand All @@ -34,6 +34,7 @@ import {
} from '../../utils/environment-options';
import { findAllNodeModules } from '../../utils/find-up';
import { Spinner } from '../../utils/spinner';
import { addError } from '../../utils/webpack-diagnostics';
import { isWebpackFiveOrHigher, withWebpackFourOrFive } from '../../utils/webpack-version';
import {
DedupeModuleResolvePlugin,
Expand Down Expand Up @@ -294,12 +295,21 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
const data = stats.toJson('verbose');
const statsOutputPath = path.join(stats.compilation.outputOptions.path, 'stats.json');

return new Promise<void>((resolve, reject) =>
stringifyStream(data)
.pipe(createWriteStream(statsOutputPath))
.on('close', resolve)
.on('error', reject),
);
try {
await fsPromises.mkdir(path.dirname(statsOutputPath), { recursive: true });

await new Promise<void>((resolve, reject) =>
stringifyStream(data)
.pipe(createWriteStream(statsOutputPath))
.on('close', resolve)
.on('error', reject),
);
} catch (error) {
addError(
stats.compilation,
`Unable to write stats file: ${error.message || 'unknown error'}`,
);
}
});
}
})(),
Expand Down

0 comments on commit 62410b4

Please sign in to comment.