Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Created March 28, 2023 07:47
Show Gist options
  • Save chengjianhua/d5dc5594f33db060b5e0b988ea3f3939 to your computer and use it in GitHub Desktop.
Save chengjianhua/d5dc5594f33db060b5e0b988ea3f3939 to your computer and use it in GitHub Desktop.
Rush Utils
import rushLib from "@microsoft/rush-lib";
import { StringBufferTerminalProvider } from "@rushstack/node-core-library";
import { Terminal } from "@rushstack/node-core-library";
export function getRushConfiguration() {
const config = rushLib.RushConfiguration.loadFromDefaultLocation();
return config;
}
/**
*
* @returns {import("@microsoft/rush-lib").RushConfigurationProject}
*/
export function getMainProject() {
const rushConfig = getRushConfiguration();
const rushCdkProject = rushConfig.projectsByName.get("@byted/ros-cdk");
return rushCdkProject;
}
export async function getChangedProjectNames({ targetBranch } = {}) {
const rushConfiguration = getRushConfiguration();
const terminal = new Terminal(new StringBufferTerminalProvider());
const projectChangeAnalyzer = new rushLib.ProjectChangeAnalyzer(
rushConfiguration
);
const changedProjects = await projectChangeAnalyzer.getChangedProjectsAsync({
targetBranchName: targetBranch || rushConfiguration.repositoryDefaultBranch,
terminal: terminal,
// Lockfile evaluation will expand the set of projects that request change files
// Not enabling, since this would be a breaking change
includeExternalDependencies: false,
// Since install may not have happened, cannot read rush-project.json
enableFiltering: false,
});
/** @type {Map<import("@microsoft/rush-lib").RushConfigurationProject, string>} */
const projectHostMap = _generateHostMap();
/** @type {Set<string>} */
const changedProjectNames = new Set();
for (const changedProject of changedProjects) {
if (
changedProject.shouldPublish &&
!changedProject.versionPolicy?.exemptFromRushChange
) {
/** @type {string | undefined} */
const hostName = projectHostMap.get(changedProject);
if (hostName) {
changedProjectNames.add(hostName);
}
}
}
return Array.from(changedProjectNames);
/**
*
* @returns {Map<import("@microsoft/rush-lib").RushConfigurationProject, string>}
*/
function _generateHostMap() {
/** @type {Map<RushConfigurationProject, string>} */
const hostMap = new Map();
for (const project of rushConfiguration.projects) {
let hostProjectName = project.packageName;
if (project.versionPolicy?.isLockstepped) {
/** @type {import("@microsoft/rush-lib").LockStepVersionPolicy} */
const lockstepPolicy = project.versionPolicy;
hostProjectName = lockstepPolicy.mainProject || project.packageName;
}
hostMap.set(project, hostProjectName);
}
return hostMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment