forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdry-run.ts
43 lines (39 loc) · 1.34 KB
/
dry-run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @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 {Argv} from 'yargs';
/**
* 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<T>(args: Argv<T>) {
return args.option('dry-run' as 'dryRun', {
type: 'boolean',
default: false,
description: 'Whether to do a dry run',
coerce: (dryRun: boolean) => {
if (dryRun) {
process.env['DRY_RUN'] = '1';
}
return dryRun;
}
});
}
/** Whether the current environment is in dry run mode. */
export function isDryRun(): boolean {
return process.env['DRY_RUN'] !== undefined;
}
/** Error to be thrown when a function or method is called in dryRun mode and shouldn't be. */
export class DryRunError extends Error {
constructor() {
super('Cannot call this function in dryRun mode.');
// 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);
}
}