A lightweight proxy API for accessing @microsoft/rush-lib with smart loading and version resolution
—
Version policies, bumping strategies, and coordinated publishing across monorepo projects.
Note: All APIs in this document are re-exported from @microsoft/rush-lib through @rushstack/rush-sdk.
Base version policy system for managing coordinated versioning across monorepo projects.
/**
* Base class for version policies that coordinate versioning across projects
*/
abstract class VersionPolicy {
/** Policy name from configuration */
readonly policyName: string;
/** Type of version policy */
readonly definitionName: VersionPolicyDefinitionName;
/** Projects associated with this policy */
readonly associatedProjects: ReadonlyArray<RushConfigurationProject>;
/** Validate that all projects follow the policy */
abstract validate(terminal: ITerminal): void;
/** Update project versions according to policy */
abstract bump(bumpType?: BumpType, identifier?: string): void;
/** Check if projects are in sync with policy */
abstract isProjectInSync(project: RushConfigurationProject): boolean;
/** Get the next version for a project */
abstract getVersionString(project: RushConfigurationProject): string;
}
/**
* Version policy type enumeration
*/
enum VersionPolicyDefinitionName {
lockStepVersion = "lockStepVersion",
individualVersion = "individualVersion"
}
/**
* Version bump type enumeration
*/
enum BumpType {
prerelease = "prerelease",
patch = "patch",
minor = "minor",
major = "major"
}Version policy where each project manages its own independent version.
/**
* Version policy where each project maintains its own version independently
*/
class IndividualVersionPolicy extends VersionPolicy {
/** Optional locked major version */
readonly lockedMajor?: number;
/** Validate individual project versions */
validate(terminal: ITerminal): void;
/** Bump individual project version */
bump(bumpType?: BumpType, identifier?: string): void;
/** Check if project version is valid */
isProjectInSync(project: RushConfigurationProject): boolean;
/** Get current version for project */
getVersionString(project: RushConfigurationProject): string;
}Usage Examples:
import { IndividualVersionPolicy, BumpType } from "@rushstack/rush-sdk";
// Typically accessed through VersionPolicyConfiguration
const versionPolicy = config.versionPolicyConfiguration
.getVersionPolicy("my-individual-policy") as IndividualVersionPolicy;
// Check locked major version
if (versionPolicy.lockedMajor !== undefined) {
console.log(`Locked to major version: ${versionPolicy.lockedMajor}`);
}
// Validate all projects follow the policy
versionPolicy.validate(terminal);
// Check specific project
const project = config.projectsByName.get("@mycompany/my-package");
if (project) {
const inSync = versionPolicy.isProjectInSync(project);
console.log(`Project in sync: ${inSync}`);
const currentVersion = versionPolicy.getVersionString(project);
console.log(`Current version: ${currentVersion}`);
}
// Bump version (typically done by rush version command)
versionPolicy.bump(BumpType.minor);Version policy where all projects share the same version and are bumped together.
/**
* Version policy where all associated projects share the same version
*/
class LockStepVersionPolicy extends VersionPolicy {
/** Current shared version */
readonly version: string;
/** Next bump type to apply */
readonly nextBump?: BumpType;
/** Main project that drives the version */
readonly mainProject?: RushConfigurationProject;
/** Validate all projects have same version */
validate(terminal: ITerminal): void;
/** Bump all projects to new version */
bump(bumpType?: BumpType, identifier?: string): void;
/** Check if project matches policy version */
isProjectInSync(project: RushConfigurationProject): boolean;
/** Get the shared version string */
getVersionString(project: RushConfigurationProject): string;
/** Update all projects to policy version */
updateProjects(): void;
}Usage Examples:
import { LockStepVersionPolicy, BumpType } from "@rushstack/rush-sdk";
const lockStepPolicy = config.versionPolicyConfiguration
.getVersionPolicy("my-lockstep-policy") as LockStepVersionPolicy;
// Check current shared version
console.log(`Shared version: ${lockStepPolicy.version}`);
if (lockStepPolicy.nextBump) {
console.log(`Next bump: ${lockStepPolicy.nextBump}`);
}
// Validate all projects are in sync
lockStepPolicy.validate(terminal);
// Check if all projects have the correct version
for (const project of lockStepPolicy.associatedProjects) {
const inSync = lockStepPolicy.isProjectInSync(project);
console.log(`${project.packageName} in sync: ${inSync}`);
}
// Bump all projects together
lockStepPolicy.bump(BumpType.minor);
// Update all project package.json files
lockStepPolicy.updateProjects();Management and loading of version policies from Rush configuration.
/**
* Manages version policies defined in rush.json
*/
class VersionPolicyConfiguration {
/** All configured version policies */
readonly versionPolicies: ReadonlyArray<VersionPolicy>;
/** Get version policy by name */
getVersionPolicy(policyName: string): VersionPolicy | undefined;
/** Try to get version policy by name */
tryGetVersionPolicy(policyName: string): VersionPolicy | undefined;
/** Get version policy for a specific project */
getVersionPolicyForProject(project: RushConfigurationProject): VersionPolicy | undefined;
/** Load version policies from configuration */
static loadFromConfiguration(
rushConfiguration: RushConfiguration
): VersionPolicyConfiguration;
/** Validate all version policies */
validate(terminal: ITerminal): void;
/** Update all projects to match their policies */
updateProjectVersions(): void;
}Usage Examples:
import { VersionPolicyConfiguration, RushConfiguration } from "@rushstack/rush-sdk";
const config = RushConfiguration.loadFromDefaultLocation();
const versionConfig = VersionPolicyConfiguration.loadFromConfiguration(config);
// List all version policies
console.log(`Found ${versionConfig.versionPolicies.length} version policies:`);
for (const policy of versionConfig.versionPolicies) {
console.log(` ${policy.policyName} (${policy.definitionName})`);
console.log(` Projects: ${policy.associatedProjects.length}`);
}
// Get specific policy
const myPolicy = versionConfig.getVersionPolicy("my-policy");
if (myPolicy) {
console.log(`Policy type: ${myPolicy.definitionName}`);
}
// Get policy for specific project
const project = config.projectsByName.get("@mycompany/my-package");
if (project) {
const policy = versionConfig.getVersionPolicyForProject(project);
if (policy) {
console.log(`Project uses policy: ${policy.policyName}`);
} else {
console.log("Project has no version policy");
}
}
// Validate all policies
versionConfig.validate(terminal);
// Update all projects to match policies
versionConfig.updateProjectVersions();Utility functions for version string manipulation and validation.
/**
* Utilities for working with semantic versions
*/
class SemVer {
/** Parse version string */
static parse(version: string): SemVer;
/** Check if version string is valid */
static isValid(version: string): boolean;
/** Compare two versions */
static compare(a: string, b: string): number;
/** Major version number */
readonly major: number;
/** Minor version number */
readonly minor: number;
/** Patch version number */
readonly patch: number;
/** Prerelease identifier */
readonly prerelease: ReadonlyArray<string | number>;
/** Build metadata */
readonly build: ReadonlyArray<string>;
/** Increment version by bump type */
inc(bumpType: BumpType, identifier?: string): SemVer;
/** Convert to string */
toString(): string;
/** Check if satisfies range */
satisfies(range: string): boolean;
}
/**
* Version range utilities
*/
class VersionRange {
/** Parse range string */
static parse(range: string): VersionRange;
/** Check if version satisfies range */
static satisfies(version: string, range: string): boolean;
/** Get best matching version from array */
static maxSatisfying(versions: string[], range: string): string | undefined;
}Usage Examples:
import { SemVer, VersionRange, BumpType } from "@rushstack/rush-sdk";
// Parse and manipulate versions
const version = SemVer.parse("1.2.3-beta.1");
console.log(`Major: ${version.major}`); // 1
console.log(`Minor: ${version.minor}`); // 2
console.log(`Patch: ${version.patch}`); // 3
console.log(`Prerelease: ${version.prerelease}`); // ["beta", 1]
// Increment version
const newVersion = version.inc(BumpType.minor);
console.log(`New version: ${newVersion.toString()}`); // "1.3.0"
// Version validation
const isValid = SemVer.isValid("1.2.3-alpha");
console.log(`Valid: ${isValid}`); // true
// Version comparison
const comparison = SemVer.compare("1.2.3", "1.2.4");
console.log(`Comparison: ${comparison}`); // -1
// Range operations
const satisfies = VersionRange.satisfies("1.2.3", "^1.2.0");
console.log(`Satisfies: ${satisfies}`); // true
const best = VersionRange.maxSatisfying(
["1.2.0", "1.2.3", "1.3.0"],
"^1.2.0"
);
console.log(`Best match: ${best}`); // "1.2.3"Install with Tessl CLI
npx tessl i tessl/npm-rushstack--rush-sdk