Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): deleteOutputPath when using `es…
Browse files Browse the repository at this point in the history
…build-builder`

Prior to this change the `deleteOutputPath` was not being used in the esbuild-builder.

Closes angular#26308
  • Loading branch information
alan-agius4 committed Nov 10, 2023
1 parent 49f503b commit adc0db2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { buildApplication } from '../../index';
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';

describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
describe('Option: "deleteOutputPath"', () => {
beforeEach(async () => {
// Application code is not needed for asset tests
await harness.writeFile('src/main.ts', 'console.log("TEST");');

// Add file in output
await harness.writeFile('dist/dummy.txt', '');
});

it(`should delete the output files when 'deleteOutputPath' is true`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: true,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toNotExist();
});

it(`should delete the output files when 'deleteOutputPath' is not set`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: undefined,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toNotExist();
});

it(`should delete the output files when 'deleteOutputPath' is false`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: false,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toExist();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path from 'node:path';
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
import { BuildOutputAsset } from '../../tools/esbuild/bundler-execution-result';
import { emitFilesToDisk } from '../../tools/esbuild/utils';
import { assertIsError } from '../../utils/error';
import { buildApplicationInternal } from '../application';
import { Schema as ApplicationBuilderOptions } from '../application/schema';
import { logBuilderStatusWarnings } from './builder-status-warnings';
Expand Down Expand Up @@ -42,7 +43,30 @@ export async function* buildEsbuildBrowser(
// Inform user of status of builder and options
logBuilderStatusWarnings(userOptions, context);
const normalizedOptions = normalizeOptions(userOptions);
const fullOutputPath = path.join(context.workspaceRoot, normalizedOptions.outputPath);
const { deleteOutputPath, outputPath } = normalizedOptions;
const fullOutputPath = path.join(context.workspaceRoot, outputPath);

if (infrastructureSettings?.write !== false) {
if (deleteOutputPath) {
if (outputPath === context.workspaceRoot) {
context.logger.error('Output path MUST not be workspace root directory!');

return;
}

await fs.rm(fullOutputPath, { force: true, recursive: true, maxRetries: 3 });
}

// Create output directory if needed
try {
await fs.mkdir(fullOutputPath, { recursive: true });
} catch (e) {
assertIsError(e);
context.logger.error('Unable to create output directory: ' + e.message);

return;
}
}

for await (const result of buildApplicationInternal(
normalizedOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { buildEsbuildBrowser } from '../../index';
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';

describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
describe('Option: "deleteOutputPath"', () => {
beforeEach(async () => {
// Application code is not needed for asset tests
await harness.writeFile('src/main.ts', 'console.log("TEST");');

// Add file in output
await harness.writeFile('dist/dummy.txt', '');
});

it(`should delete the output files when 'deleteOutputPath' is true`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: true,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toNotExist();
});

it(`should delete the output files when 'deleteOutputPath' is not set`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: undefined,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toNotExist();
});

it(`should delete the output files when 'deleteOutputPath' is false`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
deleteOutputPath: false,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/dummy.txt').toExist();
});
});
});

0 comments on commit adc0db2

Please sign in to comment.