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: #20349
(cherry picked from commit ac4c109)
  • Loading branch information
clydin committed Mar 22, 2021
1 parent cc3cc8d commit 36f28ca
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 { RollupOptions } from 'rollup';
import { ScriptTarget } from 'typescript';
Expand All @@ -36,6 +36,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 @@ -343,12 +344,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 36f28ca

Please sign in to comment.