diff --git a/dev-infra/ng-dev.js b/dev-infra/ng-dev.js index e35e507a94..e8490cf065 100755 --- a/dev-infra/ng-dev.js +++ b/dev-infra/ng-dev.js @@ -434,6 +434,31 @@ function addGithubTokenOption(yargs) { .default('github-token', '', ''); } +/** + * @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 + */ +/** Whether the current environment is in dry run mode. */ +function isDryRun() { + return process.env['DRY_RUN'] !== undefined; +} +/** Error to be thrown when a function or method is called in dryRun mode and shouldn't be. */ +var DryRunError = /** @class */ (function (_super) { + tslib.__extends(DryRunError, _super); + function DryRunError() { + var _this = _super.call(this, 'Cannot call this function in dryRun mode.') || this; + // Set the prototype explicitly because in ES5, the prototype is accidentally lost due to + // a limitation in down-leveling. + // https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work. + Object.setPrototypeOf(_this, DryRunError.prototype); + return _this; + } + return DryRunError; +}(Error)); + /** * @license * Copyright Google LLC All Rights Reserved. @@ -604,6 +629,12 @@ var GitClient = /** @class */ (function () { */ GitClient.prototype.runGraceful = function (args, options) { if (options === void 0) { options = {}; } + /** The git command to be run. */ + var gitCommand = args[0]; + if (isDryRun() && gitCommand === 'push') { + debug("\"git push\" is not able to be run in dryRun mode."); + throw new DryRunError(); + } // To improve the debugging experience in case something fails, we print all executed Git // commands to better understand the git actions occuring. Depending on the command being // executed, this debugging information should be logged at different logging levels. diff --git a/dev-infra/utils/dry-run.ts b/dev-infra/utils/dry-run.ts index 06421a4c26..e1358e4af2 100644 --- a/dev-infra/utils/dry-run.ts +++ b/dev-infra/utils/dry-run.ts @@ -9,7 +9,7 @@ import {Argv} from 'yargs'; /** - * Add a --dry-run flag to the available options for the yargs argv object. When present, sets an + * Add a --dry-run flag to the available options for the yargs argv object. When present, sets an * environment variable noting dry run mode. */ export function addDryRunFlag(args: Argv) { diff --git a/dev-infra/utils/git/index.ts b/dev-infra/utils/git/index.ts index 8c4dbc6e4c..0eb030be52 100644 --- a/dev-infra/utils/git/index.ts +++ b/dev-infra/utils/git/index.ts @@ -11,6 +11,7 @@ import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process'; import {getConfig, getRepoBaseDir, NgDevConfig} from '../config'; import {debug, info, yellow} from '../console'; +import {DryRunError, isDryRun} from '../dry-run'; import {GithubClient} from './github'; import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls'; @@ -89,6 +90,14 @@ export class GitClient { * info failed commands. */ runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns { + /** The git command to be run. */ + const gitCommand = args[0]; + + if (isDryRun() && gitCommand === 'push') { + debug(`"git push" is not able to be run in dryRun mode.`); + throw new DryRunError(); + } + // To improve the debugging experience in case something fails, we print all executed Git // commands to better understand the git actions occuring. Depending on the command being // executed, this debugging information should be logged at different logging levels.