Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rush] Update the functionality that runs external lifecycle processes to be async. #4350

Merged
merged 10 commits into from
Jun 3, 2024
2 changes: 1 addition & 1 deletion apps/rush/src/RushVersionSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class RushVersionSelector {
if (installIsValid) {
console.log('Another process performed the installation.');
} else {
Utilities.installPackageInDirectory({
await Utilities.installPackageInDirectoryAsync({
directory: expectedRushPath,
packageName: isLegacyRushVersion ? '@microsoft/rush' : '@microsoft/rush-lib',
version: version,
Expand Down
10 changes: 10 additions & 0 deletions common/changes/@microsoft/rush/main_2023-09-24-06-11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Update the functionality that runs external lifecycle processes to be async.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
4 changes: 3 additions & 1 deletion libraries/rush-lib/config/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"!lib-commonjs/**/__tests__/**",
"!lib-commonjs/**/__fixtures__/**",
"!lib-commonjs/**/__mocks__/**"
]
],

"globalTeardown": "<rootDir>/lib-commonjs/utilities/test/global-teardown.js"
}
26 changes: 8 additions & 18 deletions libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,24 +412,14 @@ export class RushPnpmCommandLineParser {
}

try {
await Utilities.executeCommandAndInspectOutputAsync(
{
command: rushConfiguration.packageManagerToolFilename,
args: this._pnpmArgs,
workingDirectory: process.cwd(),
environment: pnpmEnvironmentMap.toObject(),
keepEnvironment: true
},
onStdoutStreamChunk,
(exitCode: number | null, signal: NodeJS.Signals | null) => {
if (typeof exitCode === 'number') {
process.exitCode = exitCode;
} else {
// Terminated by a signal
process.exitCode = 1;
}
}
);
await Utilities.executeCommandAsync({
command: rushConfiguration.packageManagerToolFilename,
args: this._pnpmArgs,
workingDirectory: process.cwd(),
environment: pnpmEnvironmentMap.toObject(),
keepEnvironment: true,
onStdoutStreamChunk
});
} catch (e) {
this._terminal.writeDebugLine(`Error: ${e}`);
}
Expand Down
56 changes: 34 additions & 22 deletions libraries/rush-lib/src/cli/actions/ChangeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ export class ChangeAction extends BaseRushAction {
}

public async runAsync(): Promise<void> {
const targetBranch: string = await this._getTargetBranchAsync();
// eslint-disable-next-line no-console
console.log(`The target branch is ${this._targetBranch}`);
console.log(`The target branch is ${targetBranch}`);

if (this._verifyParameter.value) {
const errors: string[] = [
Expand Down Expand Up @@ -197,11 +198,11 @@ export class ChangeAction extends BaseRushAction {
const sortedProjectList: string[] = (await this._getChangedProjectNamesAsync()).sort();
if (sortedProjectList.length === 0) {
this._logNoChangeFileRequired();
this._warnUnstagedChanges();
await this._warnUnstagedChangesAsync();
return;
}

this._warnUnstagedChanges();
await this._warnUnstagedChangesAsync();

const inquirer: typeof InquirerType = await import('inquirer');
const promptModule: InquirerType.PromptModule = inquirer.createPromptModule();
Expand Down Expand Up @@ -277,7 +278,7 @@ export class ChangeAction extends BaseRushAction {
interactiveMode = true;

const existingChangeComments: Map<string, string[]> = ChangeFiles.getChangeComments(
this._getChangeFiles()
await this._getChangeFilesAsync()
);
changeFileData = await this._promptForChangeFileDataAsync(
promptModule,
Expand Down Expand Up @@ -310,7 +311,7 @@ export class ChangeAction extends BaseRushAction {
}
if (this._commitChangesFlagParameter.value || this._commitChangesMessageStringParameter.value) {
if (changefiles && changefiles.length !== 0) {
this._stageAndCommitGitChanges(
await this._stageAndCommitGitChangesAsync(
changefiles,
this._commitChangesMessageStringParameter.value ||
this.rushConfiguration.gitChangefilesCommitMessage ||
Expand Down Expand Up @@ -340,15 +341,16 @@ export class ChangeAction extends BaseRushAction {
private async _verifyAsync(): Promise<void> {
const changedPackages: string[] = await this._getChangedProjectNamesAsync();
if (changedPackages.length > 0) {
this._validateChangeFile(changedPackages);
await this._validateChangeFileAsync(changedPackages);
} else {
this._logNoChangeFileRequired();
}
}

private get _targetBranch(): string {
private async _getTargetBranchAsync(): Promise<string> {
if (!this._targetBranchName) {
this._targetBranchName = this._targetBranchParameter.value || this._git.getRemoteDefaultBranch();
this._targetBranchName =
this._targetBranchParameter.value || (await this._git.getRemoteDefaultBranchAsync());
}

return this._targetBranchName;
Expand All @@ -358,7 +360,7 @@ export class ChangeAction extends BaseRushAction {
const projectChangeAnalyzer: ProjectChangeAnalyzer = new ProjectChangeAnalyzer(this.rushConfiguration);
const changedProjects: Set<RushConfigurationProject> =
await projectChangeAnalyzer.getChangedProjectsAsync({
targetBranchName: this._targetBranch,
targetBranchName: await this._getTargetBranchAsync(),
terminal: this._terminal,
shouldFetch: !this._noFetchParameter.value,
// Lockfile evaluation will expand the set of projects that request change files
Expand All @@ -382,19 +384,28 @@ export class ChangeAction extends BaseRushAction {
return Array.from(changedProjectNames);
}

private _validateChangeFile(changedPackages: string[]): void {
const files: string[] = this._getChangeFiles();
private async _validateChangeFileAsync(changedPackages: string[]): Promise<void> {
const files: string[] = await this._getChangeFilesAsync();
ChangeFiles.validate(files, changedPackages, this.rushConfiguration);
}

private _getChangeFiles(): string[] {
private async _getChangeFilesAsync(): Promise<string[]> {
const repoRoot: string = getRepoRoot(this.rushConfiguration.rushJsonFolder);
const relativeChangesFolder: string = path.relative(repoRoot, this.rushConfiguration.changesFolder);
return this._git
.getChangedFiles(this._targetBranch, this._terminal, true, relativeChangesFolder)
.map((relativePath) => {
return path.join(repoRoot, relativePath);
});
const targetBranch: string = await this._getTargetBranchAsync();
const changedFiles: string[] = await this._git.getChangedFilesAsync(
targetBranch,
this._terminal,
true,
relativeChangesFolder
);

const result: string[] = [];
for (const changedFile of changedFiles) {
result.push(path.join(repoRoot, changedFile));
}

return result;
}

/**
Expand Down Expand Up @@ -630,9 +641,10 @@ export class ChangeAction extends BaseRushAction {
return email;
}

private _warnUnstagedChanges(): void {
private async _warnUnstagedChangesAsync(): Promise<void> {
try {
if (this._git.hasUnstagedChanges()) {
const hasUnstagedChanges: boolean = await this._git.hasUnstagedChangesAsync();
if (hasUnstagedChanges) {
// eslint-disable-next-line no-console
console.log(
'\n' +
Expand Down Expand Up @@ -738,14 +750,14 @@ export class ChangeAction extends BaseRushAction {
console.log('No changes were detected to relevant packages on this branch. Nothing to do.');
}

private _stageAndCommitGitChanges(pattern: string[], message: string): void {
private async _stageAndCommitGitChangesAsync(pattern: string[], message: string): Promise<void> {
try {
Utilities.executeCommand({
await Utilities.executeCommandAsync({
command: 'git',
args: ['add', ...pattern],
workingDirectory: this.rushConfiguration.changesFolder
});
Utilities.executeCommand({
await Utilities.executeCommandAsync({
command: 'git',
args: ['commit', ...pattern, '-m', message],
workingDirectory: this.rushConfiguration.changesFolder
Expand Down