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 #26308

(cherry picked from commit 0003420)
  • Loading branch information
alan-agius4 committed Nov 13, 2023
1 parent f7f62c9 commit 15dd71a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

import { BuilderOutput } from '@angular-devkit/architect';
import type { logging } from '@angular-devkit/core';
import fs from 'node:fs/promises';
import path from 'node:path';
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language';
import { withNoProgress, withSpinner, writeResultFiles } from '../../tools/esbuild/utils';
import { deleteOutputDir } from '../../utils/delete-output-dir';
import { shouldWatchRoot } from '../../utils/environment-options';
import { assertIsError } from '../../utils/error';
import { NormalizedCachedOptions } from '../../utils/normalize-cache';

export async function* runEsBuildBuildAction(
Expand Down Expand Up @@ -51,27 +50,8 @@ export async function* runEsBuildBuildAction(
progress,
} = options;

if (writeToFileSystem) {
// Clean output path if enabled
if (deleteOutputPath) {
if (outputPath === workspaceRoot) {
logger.error('Output path MUST not be workspace root directory!');

return;
}

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

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

return;
}
if (deleteOutputPath && writeToFileSystem) {
await deleteOutputDir(workspaceRoot, outputPath);
}

const withProgress: typeof withSpinner = progress ? withSpinner : withNoProgress;
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 { 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 not 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 { deleteOutputDir } from '../../utils';
import { buildApplicationInternal } from '../application';
import { Schema as ApplicationBuilderOptions } from '../application/schema';
import { logBuilderStatusWarnings } from './builder-status-warnings';
Expand Down Expand Up @@ -42,7 +43,12 @@ 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 (deleteOutputPath && infrastructureSettings?.write !== false) {
await deleteOutputDir(context.workspaceRoot, outputPath);
}

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 not 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 15dd71a

Please sign in to comment.