Skip to content

Commit cacc8b3

Browse files
josephperrottdevversion
authored andcommitted
refactor: move to using assertions in place to determine the types of a configuration for the release config
1 parent 3461d01 commit cacc8b3

File tree

14 files changed

+66
-42
lines changed

14 files changed

+66
-42
lines changed

ng-dev/commit-message/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export interface CommitMessageConfig {
1717
}
1818

1919
/** Assert the provided config contains a `CommitMessageConfig`. */
20-
export function assertValidCommitMessageConfig<T>(config: T & Partial<{commitMessage: CommitMessageConfig}>): asserts config is T & {commitMessage: CommitMessageConfig} {
20+
export function assertValidCommitMessageConfig<T>(
21+
config: T & Partial<{commitMessage: CommitMessageConfig}>,
22+
): asserts config is T & {commitMessage: CommitMessageConfig} {
2123
if (config.commitMessage === undefined) {
2224
throw new ConfigValidationError(`No configuration defined for "commitMessage"`);
2325
}

ng-dev/commit-message/validate.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function expectValidationResult(
3636
// TODO(josephperrott): Clean up tests to test script rather than for
3737
// specific commit messages we want to use.
3838
describe('validate-commit-message.js', () => {
39-
let getConfigSpy: jasmine.Spy
39+
let getConfigSpy: jasmine.Spy;
4040
beforeEach(() => {
4141
getConfigSpy = spyOn(configUtils, 'getConfig').and.returnValue(config);
4242
});

ng-dev/commit-message/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { getConfig } from '../utils/config';
9+
import {getConfig} from '../utils/config';
1010
import {error} from '../utils/console';
1111

1212
import {assertValidCommitMessageConfig, COMMIT_TYPES, ScopeRequirement} from './config';

ng-dev/release/build/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ts_library(
3030
deps = [
3131
":build",
3232
"//ng-dev/release/config",
33+
"//ng-dev/utils",
3334
],
3435
)
3536

ng-dev/release/build/build-worker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* supports stdout JSON output that should be parsable and not polluted from other stdout messages.
1414
*/
1515

16-
import {getReleaseConfig} from '../config/index';
16+
import {getConfig} from '../../utils/config';
17+
import {assertValidReleaseConfig} from '../config/index';
1718

1819
// Start the release package building.
1920
main(process.argv[2] === 'true');
@@ -24,8 +25,9 @@ async function main(stampForRelease: boolean) {
2425
throw Error('This script needs to be invoked as a NodeJS worker.');
2526
}
2627

27-
const config = getReleaseConfig();
28-
const builtPackages = await config.buildPackages(stampForRelease);
28+
const config = getConfig();
29+
assertValidReleaseConfig(config);
30+
const builtPackages = await config.release.buildPackages(stampForRelease);
2931

3032
// Transfer the built packages back to the parent process.
3133
process.send(builtPackages);

ng-dev/release/build/build.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as releaseConfig from '../config/index';
9+
import * as configUtils from '../../utils/config';
10+
import {BuiltPackage} from '../config';
1011
import {ReleaseBuildCommandModule} from './cli';
1112
import * as index from './index';
1213

@@ -31,9 +32,12 @@ describe('ng-dev release build', () => {
3132

3233
/** Invokes the build command handler. */
3334
async function invokeBuild({json}: {json?: boolean} = {}) {
34-
spyOn(releaseConfig, 'getReleaseConfig').and.returnValue({
35-
npmPackages,
36-
buildPackages,
35+
spyOn(configUtils, 'getConfig').and.returnValue({
36+
release: {
37+
npmPackages,
38+
buildPackages,
39+
releaseNotes: {},
40+
},
3741
});
3842
await ReleaseBuildCommandModule.handler({json: !!json, stampForRelease: true, $0: '', _: []});
3943
}
@@ -52,7 +56,7 @@ describe('ng-dev release build', () => {
5256
expect(writeSpy).toHaveBeenCalledTimes(1);
5357

5458
const jsonText = writeSpy.calls.mostRecent().args[0] as string;
55-
const parsed = JSON.parse(jsonText) as releaseConfig.BuiltPackage[];
59+
const parsed = JSON.parse(jsonText) as BuiltPackage[];
5660

5761
expect(parsed).toEqual([
5862
{name: '@angular/pkg1', outputPath: 'dist/pkg1'},

ng-dev/release/build/cli.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import {Arguments, Argv, CommandModule} from 'yargs';
1010

1111
import {getConfig} from '../../utils/config';
12-
import {error, green, info, red, warn, yellow} from '../../utils/console';
13-
import {BuiltPackage, getReleaseConfig} from '../config/index';
12+
import {error, green, info, red} from '../../utils/console';
13+
import {assertValidReleaseConfig} from '../config/index';
1414

1515
import {buildReleaseOutput} from './index';
1616

@@ -30,7 +30,9 @@ function builder(argv: Argv): Argv<ReleaseBuildOptions> {
3030

3131
/** Yargs command handler for building a release. */
3232
async function handler(args: Arguments<ReleaseBuildOptions>) {
33-
const {npmPackages} = getReleaseConfig();
33+
const config = getConfig();
34+
assertValidReleaseConfig(config);
35+
const {npmPackages} = config.release;
3436
let builtPackages = await buildReleaseOutput(true);
3537

3638
// If package building failed, print an error and exit with an error code.

ng-dev/release/config/index.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {assertNoErrors, getConfig} from '../../utils/config';
9+
import {assertNoErrors, ConfigValidationError, getConfig} from '../../utils/config';
1010

1111
/** Interface describing a built package. */
1212
export interface BuiltPackage {
@@ -49,25 +49,26 @@ export interface ReleaseNotesConfig {
4949
export type DevInfraReleaseConfig = {release: ReleaseConfig};
5050

5151
/** Retrieve and validate the config as `ReleaseConfig`. */
52-
export function getReleaseConfig(
53-
config: Partial<DevInfraReleaseConfig> = getConfig(),
54-
): ReleaseConfig {
52+
export function assertValidReleaseConfig<T>(
53+
config: T & Partial<DevInfraReleaseConfig>,
54+
): asserts config is T & DevInfraReleaseConfig {
5555
// List of errors encountered validating the config.
5656
const errors: string[] = [];
5757

5858
if (config.release === undefined) {
59-
errors.push(`No configuration defined for "release"`);
59+
throw new ConfigValidationError('No configuration provided for `release`');
6060
}
61-
if (config.release?.npmPackages === undefined) {
61+
62+
if (config.release.npmPackages === undefined) {
6263
errors.push(`No "npmPackages" configured for releasing.`);
6364
}
64-
if (config.release?.buildPackages === undefined) {
65+
if (config.release.buildPackages === undefined) {
6566
errors.push(`No "buildPackages" function configured for releasing.`);
6667
}
67-
if (config.release?.releaseNotes === undefined) {
68+
if (config.release.releaseNotes === undefined) {
6869
errors.push(`No "releaseNotes" configured for releasing.`);
6970
}
70-
71-
assertNoErrors(errors);
72-
return config.release!;
71+
if (errors.length) {
72+
throw new ConfigValidationError('Invalid `release` configuration', errors);
73+
}
7374
}

ng-dev/release/info/cli.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
import {CommandModule} from 'yargs';
1010

11-
import {info} from '../../utils/console';
1211
import {GitClient} from '../../utils/git/git-client';
13-
import {getReleaseConfig} from '../config/index';
12+
import {assertValidReleaseConfig} from '../config/index';
1413
import {fetchActiveReleaseTrains} from '../versioning/active-release-trains';
1514
import {printActiveReleaseTrains} from '../versioning/print-active-trains';
1615
import {getNextBranchName, ReleaseRepoWithApi} from '../versioning';
16+
import {getConfig} from '../../utils/config';
1717

1818
/** Yargs command handler for printing release information. */
1919
async function handler() {
2020
const git = GitClient.get();
2121
const nextBranchName = getNextBranchName(git.config.github);
2222
const repo: ReleaseRepoWithApi = {api: git.github, ...git.remoteConfig, nextBranchName};
2323
const releaseTrains = await fetchActiveReleaseTrains(repo);
24+
const config = getConfig();
25+
assertValidReleaseConfig(config);
2426

2527
// Print the active release trains.
26-
await printActiveReleaseTrains(releaseTrains, getReleaseConfig());
28+
await printActiveReleaseTrains(releaseTrains, config.release);
2729
}
2830

2931
/** CLI command module for retrieving release information. */

ng-dev/release/notes/release-notes.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import {CommitFromGitLog} from '../../commit-message/parse';
1111

1212
import {promptInput} from '../../utils/console';
1313
import {GitClient} from '../../utils/git/git-client';
14-
import {DevInfraReleaseConfig, getReleaseConfig, ReleaseNotesConfig} from '../config/index';
14+
import {assertValidReleaseConfig, ReleaseNotesConfig} from '../config/index';
1515
import {RenderContext} from './context';
1616

1717
import changelogTemplate from './templates/changelog';
1818
import githubReleaseTemplate from './templates/github-release';
1919
import {getCommitsForRangeWithDeduping} from './commits/get-commits-in-range';
20+
import {getConfig} from '../../utils/config';
2021

2122
/** Release note generation. */
2223
export class ReleaseNotes {
@@ -87,9 +88,11 @@ export class ReleaseNotes {
8788
return this.renderContext;
8889
}
8990

90-
// These methods are used for access to the utility functions while allowing them
91+
// This method is used for access to the utility functions while allowing them
9192
// to be overwritten in subclasses during testing.
92-
protected getReleaseConfig(config?: Partial<DevInfraReleaseConfig>) {
93-
return getReleaseConfig(config);
93+
protected getReleaseConfig() {
94+
const config = getConfig();
95+
assertValidReleaseConfig(config);
96+
return config.release;
9497
}
9598
}

0 commit comments

Comments
 (0)