From c717fce42495a93e366a482b0458f930c25dfb61 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Thu, 4 May 2017 19:30:32 +0100 Subject: [PATCH] feat(@angular/cli): add flag to not delete output path Fix #5925 Fix #6193 --- packages/@angular/cli/commands/build.ts | 7 +++++++ packages/@angular/cli/models/build-options.ts | 2 +- packages/@angular/cli/tasks/build.ts | 4 +++- packages/@angular/cli/tasks/serve.ts | 4 +++- tests/e2e/tests/build/delete-output-path.ts | 21 +++++++++++++++++++ tests/e2e/tests/build/fail-build.ts | 11 ---------- 6 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 tests/e2e/tests/build/delete-output-path.ts delete mode 100644 tests/e2e/tests/build/fail-build.ts diff --git a/packages/@angular/cli/commands/build.ts b/packages/@angular/cli/commands/build.ts index b1263e704435..e7b1bf4febb6 100644 --- a/packages/@angular/cli/commands/build.ts +++ b/packages/@angular/cli/commands/build.ts @@ -118,6 +118,13 @@ export const baseBuildCommandOptions: any = [ type: String, aliases: ['a'], description: 'Specifies app name or index to use.' + }, + { + name: 'delete-output-path', + type: Boolean, + default: true, + aliases: ['dop'], + description: 'Delete output path before build.' } ]; diff --git a/packages/@angular/cli/models/build-options.ts b/packages/@angular/cli/models/build-options.ts index bed860355c94..74fad4cf6eb4 100644 --- a/packages/@angular/cli/models/build-options.ts +++ b/packages/@angular/cli/models/build-options.ts @@ -17,5 +17,5 @@ export interface BuildOptions { outputHashing?: string; poll?: number; app?: string; - + deleteOutputPath?: boolean; } diff --git a/packages/@angular/cli/tasks/build.ts b/packages/@angular/cli/tasks/build.ts index cdbc1f6cbd18..c984679c5c09 100644 --- a/packages/@angular/cli/tasks/build.ts +++ b/packages/@angular/cli/tasks/build.ts @@ -27,7 +27,9 @@ export default Task.extend({ if (config.project && config.project.ejected) { throw new SilentError('An ejected project cannot use the build command anymore.'); } - rimraf.sync(path.resolve(project.root, outputPath)); + if (runTaskOptions.deleteOutputPath) { + rimraf.sync(path.resolve(project.root, outputPath)); + } const webpackConfig = new NgCliWebpackConfig(runTaskOptions, app).buildConfig(); const webpackCompiler = webpack(webpackConfig); diff --git a/packages/@angular/cli/tasks/serve.ts b/packages/@angular/cli/tasks/serve.ts index a7d1c7bdacac..7804d1549cc3 100644 --- a/packages/@angular/cli/tasks/serve.ts +++ b/packages/@angular/cli/tasks/serve.ts @@ -31,7 +31,9 @@ export default Task.extend({ if (projectConfig.project && projectConfig.project.ejected) { throw new SilentError('An ejected project cannot use the build command anymore.'); } - rimraf.sync(path.resolve(this.project.root, outputPath)); + if (serveTaskOptions.deleteOutputPath) { + rimraf.sync(path.resolve(this.project.root, outputPath)); + } const serveDefaults = { // default deployUrl to '' on serve to prevent the default from .angular-cli.json diff --git a/tests/e2e/tests/build/delete-output-path.ts b/tests/e2e/tests/build/delete-output-path.ts new file mode 100644 index 000000000000..9a5b658f8e7e --- /dev/null +++ b/tests/e2e/tests/build/delete-output-path.ts @@ -0,0 +1,21 @@ +import {ng} from '../../utils/process'; +import {expectToFail} from '../../utils/utils'; +import {deleteFile, expectFileToExist} from '../../utils/fs'; +import {getGlobalVariable} from '../../utils/env'; + +export default function() { + // Skip this in ejected tests. + if (getGlobalVariable('argv').eject) { + return Promise.resolve(); + } + + return ng('build') + // This is supposed to fail since there's a missing file + .then(() => deleteFile('src/app/app.component.ts')) + // The build fails but we don't delete the output of the previous build. + .then(() => expectToFail(() => ng('build', '--no-delete-output-path'))) + .then(() => expectFileToExist('dist')) + // By default, output path is always cleared. + .then(() => expectToFail(() => ng('build'))) + .then(() => expectToFail(() => expectFileToExist('dist'))); +} diff --git a/tests/e2e/tests/build/fail-build.ts b/tests/e2e/tests/build/fail-build.ts deleted file mode 100644 index 4dd01b1e7eb4..000000000000 --- a/tests/e2e/tests/build/fail-build.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {ng} from '../../utils/process'; -import {expectToFail} from '../../utils/utils'; -import {deleteFile, expectFileToExist} from '../../utils/fs'; - -export default function() { - return deleteFile('src/app/app.component.ts') - // This is supposed to fail since there's a missing file - .then(() => expectToFail(() => ng('build'))) - // Failed builds don't leave behind dist/ - .then(() => expectToFail(() => expectFileToExist('dist/'))); -}