forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeManager.ts
More file actions
128 lines (112 loc) · 4.39 KB
/
ChangeManager.ts
File metadata and controls
128 lines (112 loc) · 4.39 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { IPackageJson } from '@rushstack/node-core-library';
import { IChangeInfo } from '../api/ChangeManagement';
import { IChangelog } from '../api/Changelog';
import { RushConfiguration } from '../api/RushConfiguration';
import { RushConfigurationProject } from '../api/RushConfigurationProject';
import { VersionPolicyConfiguration } from '../api/VersionPolicyConfiguration';
import { PublishUtilities, IChangeInfoHash } from './PublishUtilities';
import { ChangeFiles } from './ChangeFiles';
import { PrereleaseToken } from './PrereleaseToken';
import { ChangelogGenerator } from './ChangelogGenerator';
/**
* The class manages change files and controls how changes logged by change files
* can be applied to package.json and change logs.
*/
export class ChangeManager {
private _prereleaseToken: PrereleaseToken;
private _orderedChanges: IChangeInfo[];
private _allPackages: Map<string, RushConfigurationProject>;
private _allChanges: IChangeInfoHash;
private _changeFiles: ChangeFiles;
private _rushConfiguration: RushConfiguration;
private _lockStepProjectsToExclude: Set<string> | undefined;
public constructor(
rushConfiguration: RushConfiguration,
lockStepProjectsToExclude?: Set<string> | undefined
) {
this._rushConfiguration = rushConfiguration;
this._lockStepProjectsToExclude = lockStepProjectsToExclude;
}
/**
* Load changes from change files
* @param changesPath - location of change files
* @param prereleaseToken - prerelease token
* @param includeCommitDetails - whether commit details need to be included in changes
*/
public load(
changesPath: string,
prereleaseToken: PrereleaseToken = new PrereleaseToken(),
includeCommitDetails: boolean = false
): void {
this._allPackages = this._rushConfiguration.projectsByName;
this._prereleaseToken = prereleaseToken;
this._changeFiles = new ChangeFiles(changesPath);
this._allChanges = PublishUtilities.findChangeRequests(
this._allPackages,
this._rushConfiguration,
this._changeFiles,
includeCommitDetails,
this._prereleaseToken,
this._lockStepProjectsToExclude
);
this._orderedChanges = PublishUtilities.sortChangeRequests(this._allChanges);
}
public hasChanges(): boolean {
return this._orderedChanges && this._orderedChanges.length > 0;
}
public get changes(): IChangeInfo[] {
return this._orderedChanges;
}
public get allPackages(): Map<string, RushConfigurationProject> {
return this._allPackages;
}
public validateChanges(versionConfig: VersionPolicyConfiguration): void {
Object
.keys(this._allChanges)
.filter((key) => {
const projectInfo: RushConfigurationProject | undefined = this._rushConfiguration.getProjectByName(key);
if (projectInfo) {
if (projectInfo.versionPolicy) {
const changeInfo: IChangeInfo = this._allChanges[key];
projectInfo.versionPolicy.validate(changeInfo.newVersion!, key);
}
}
});
}
/**
* Apply changes to package.json
* @param shouldCommit - If the value is true, package.json will be updated.
* If the value is false, package.json and change logs will not be updated. It will only do a dry-run.
*/
public apply(shouldCommit: boolean): Map<string, IPackageJson> | undefined {
if (!this.hasChanges()) {
return;
}
// Apply all changes to package.json files.
const updatedPackages: Map<string, IPackageJson> = PublishUtilities.updatePackages(
this._allChanges,
this._allPackages,
this._rushConfiguration,
shouldCommit,
this._prereleaseToken,
this._lockStepProjectsToExclude);
return updatedPackages;
}
public updateChangelog(shouldCommit: boolean): void {
// Do not update changelog or delete the change files for prerelease.
// Save them for the official release.
if (!this._prereleaseToken.hasValue) {
// Update changelogs.
const updatedChangelogs: IChangelog[] = ChangelogGenerator.updateChangelogs(
this._allChanges,
this._allPackages,
this._rushConfiguration,
shouldCommit
);
// Remove the change request files only if "-a" was provided.
this._changeFiles.deleteAll(shouldCommit, updatedChangelogs);
}
}
}