feat(build-infrastructure): Add setVersion API (#22919)
This change adds a `setVersion` API that acts on several packages. [AB#21855](https://dev.azure.com/fluidframework/235294da-091d-4c29-84fc-cdfc3d90890b/_workitems/edit/21855) --------- Co-authored-by: Alex Villarreal <716334+alexvy86@users.noreply.github.com> Co-authored-by: Joshua Smithrud <54606601+Josmithr@users.noreply.github.com>
This commit is contained in:
Родитель
621aed69cf
Коммит
4488bcb285
|
@ -6,6 +6,7 @@
|
|||
|
||||
import type { Opaque } from 'type-fest';
|
||||
import type { PackageJson as PackageJson_2 } from 'type-fest';
|
||||
import { SemVer } from 'semver';
|
||||
import type { SetRequired } from 'type-fest';
|
||||
import { SimpleGit } from 'simple-git';
|
||||
|
||||
|
@ -234,6 +235,9 @@ export interface Reloadable {
|
|||
reload(): void;
|
||||
}
|
||||
|
||||
// @public
|
||||
export function setVersion<J extends PackageJson>(packages: IPackage<J>[], version: SemVer): Promise<void>;
|
||||
|
||||
// @public
|
||||
export interface WorkspaceDefinition {
|
||||
directory: string;
|
||||
|
|
|
@ -50,3 +50,4 @@ export type {
|
|||
IPackageManager,
|
||||
} from "./types.js";
|
||||
export { isIPackage, isIReleaseGroup } from "./types.js";
|
||||
export { setVersion } from "./versions.js";
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*!
|
||||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
import { strict as assert } from "node:assert";
|
||||
import path from "node:path";
|
||||
|
||||
import { expect } from "chai";
|
||||
import { afterEach, describe, it } from "mocha";
|
||||
import * as semver from "semver";
|
||||
import { simpleGit } from "simple-git";
|
||||
|
||||
import { loadBuildProject } from "../buildProject.js";
|
||||
import type { ReleaseGroupName, WorkspaceName } from "../types.js";
|
||||
import { setVersion } from "../versions.js";
|
||||
|
||||
import { testDataPath, testRepoRoot } from "./init.js";
|
||||
|
||||
const repo = loadBuildProject(path.join(testDataPath, "./testRepo"));
|
||||
const main = repo.releaseGroups.get("main" as ReleaseGroupName);
|
||||
assert(main !== undefined);
|
||||
|
||||
const group2 = repo.releaseGroups.get("group2" as ReleaseGroupName);
|
||||
assert(group2 !== undefined);
|
||||
|
||||
const group3 = repo.releaseGroups.get("group3" as ReleaseGroupName);
|
||||
assert(group3 !== undefined);
|
||||
|
||||
const secondWorkspace = repo.workspaces.get("second" as WorkspaceName);
|
||||
assert(secondWorkspace !== undefined);
|
||||
|
||||
/**
|
||||
* A git client rooted in the test repo. Used for resetting tests.
|
||||
*/
|
||||
const git = simpleGit(testRepoRoot);
|
||||
|
||||
describe("setVersion", () => {
|
||||
afterEach(async () => {
|
||||
await git.checkout(["HEAD", "--", testRepoRoot]);
|
||||
repo.reload();
|
||||
});
|
||||
|
||||
it("release group", async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
await setVersion(main.packages, semver.parse("1.2.1")!);
|
||||
|
||||
const allCorrect = main.packages.every((pkg) => pkg.version === "1.2.1");
|
||||
expect(main.version).to.equal("1.2.1");
|
||||
expect(allCorrect).to.be.true;
|
||||
});
|
||||
|
||||
it("workspace", async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
await setVersion(secondWorkspace.packages, semver.parse("2.2.1")!);
|
||||
|
||||
const allCorrect = secondWorkspace.packages.every((pkg) => pkg.version === "2.2.1");
|
||||
expect(allCorrect).to.be.true;
|
||||
});
|
||||
|
||||
it("repo", async () => {
|
||||
const packages = [...repo.packages.values()];
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
await setVersion(packages, semver.parse("1.2.1")!);
|
||||
|
||||
const allCorrect = packages.every((pkg) => pkg.version === "1.2.1");
|
||||
expect(allCorrect).to.be.true;
|
||||
});
|
||||
});
|
|
@ -0,0 +1,37 @@
|
|||
/*!
|
||||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
import { SemVer } from "semver";
|
||||
|
||||
import { updatePackageJsonFileAsync } from "./packageJsonUtils.js";
|
||||
import type { IPackage, PackageJson } from "./types.js";
|
||||
|
||||
/**
|
||||
* Sets the version of a group of packages, writing the new version in package.json. After the update, the packages are
|
||||
* reloaded so the in-memory data reflects the version changes.
|
||||
*
|
||||
* @param packages - An array of objects whose version should be updated.
|
||||
* @param version - The version to set.
|
||||
*/
|
||||
export async function setVersion<J extends PackageJson>(
|
||||
packages: IPackage<J>[],
|
||||
version: SemVer,
|
||||
): Promise<void> {
|
||||
const translatedVersion = version;
|
||||
const setPackagePromises: Promise<void>[] = [];
|
||||
for (const pkg of packages) {
|
||||
setPackagePromises.push(
|
||||
updatePackageJsonFileAsync<J>(pkg.directory, async (json) => {
|
||||
json.version = translatedVersion.version;
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.all(setPackagePromises);
|
||||
|
||||
// Reload all the packages to refresh the in-memory data
|
||||
for (const pkg of packages) {
|
||||
pkg.reload();
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче