mirror of
https://github.com/hoverkraft-tech/compose-action.git
synced 2026-03-06 08:13:10 +08:00
chore: minor refactoring
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
parent
c36b7c122a
commit
4894d24920
39
dist/index.js
generated
vendored
39
dist/index.js
generated
vendored
@ -47949,8 +47949,13 @@ class DockerComposeInstallerService {
|
|||||||
this.manualInstallerAdapter = manualInstallerAdapter;
|
this.manualInstallerAdapter = manualInstallerAdapter;
|
||||||
}
|
}
|
||||||
async install({ composeVersion, cwd, githubToken }) {
|
async install({ composeVersion, cwd, githubToken }) {
|
||||||
const currentVersion = await this.getInstalledVersion(cwd);
|
const currentVersion = await this.version({ cwd });
|
||||||
const needsInstall = !currentVersion || (composeVersion && composeVersion !== currentVersion);
|
const normalizedCurrentVersion = currentVersion ? this.normalizeVersion(currentVersion) : null;
|
||||||
|
const normalizedRequestedVersion = composeVersion
|
||||||
|
? this.normalizeVersion(composeVersion)
|
||||||
|
: null;
|
||||||
|
const needsInstall = !currentVersion ||
|
||||||
|
(composeVersion && normalizedRequestedVersion !== normalizedCurrentVersion);
|
||||||
if (!needsInstall) {
|
if (!needsInstall) {
|
||||||
return currentVersion;
|
return currentVersion;
|
||||||
}
|
}
|
||||||
@ -47962,21 +47967,24 @@ class DockerComposeInstallerService {
|
|||||||
targetVersion = await this.getLatestVersion(githubToken);
|
targetVersion = await this.getLatestVersion(githubToken);
|
||||||
}
|
}
|
||||||
await this.installVersion(targetVersion);
|
await this.installVersion(targetVersion);
|
||||||
return this.version({ cwd });
|
const installedVersion = await this.version({ cwd });
|
||||||
}
|
if (!installedVersion ||
|
||||||
async getInstalledVersion(cwd) {
|
this.normalizeVersion(installedVersion) !== this.normalizeVersion(targetVersion)) {
|
||||||
try {
|
throw new Error(`Failed to install Docker Compose version "${targetVersion}", installed version is "${installedVersion ?? "unknown"}"`);
|
||||||
return await this.version({ cwd });
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return installedVersion;
|
||||||
}
|
}
|
||||||
async version({ cwd }) {
|
async version({ cwd }) {
|
||||||
const result = await (0,dist.version)({
|
try {
|
||||||
cwd,
|
const result = await (0,dist.version)({
|
||||||
});
|
cwd,
|
||||||
return result.data.version;
|
});
|
||||||
|
return result.data.version;
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
// If version check fails (e.g., Docker Compose not installed), return null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async getLatestVersion(githubToken) {
|
async getLatestVersion(githubToken) {
|
||||||
const octokit = getOctokit(githubToken);
|
const octokit = getOctokit(githubToken);
|
||||||
@ -47986,6 +47994,9 @@ class DockerComposeInstallerService {
|
|||||||
});
|
});
|
||||||
return response.data.tag_name;
|
return response.data.tag_name;
|
||||||
}
|
}
|
||||||
|
normalizeVersion(version) {
|
||||||
|
return version.replace(/^v/i, "");
|
||||||
|
}
|
||||||
async installVersion(version) {
|
async installVersion(version) {
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case "linux":
|
case "linux":
|
||||||
|
|||||||
@ -21,6 +21,52 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
let mockAgent: MockAgent;
|
let mockAgent: MockAgent;
|
||||||
let service: InstanceType<typeof DockerComposeInstallerService>;
|
let service: InstanceType<typeof DockerComposeInstallerService>;
|
||||||
|
|
||||||
|
const composeVersionResponse = (version: string) => ({
|
||||||
|
exitCode: 0,
|
||||||
|
out: "",
|
||||||
|
err: "",
|
||||||
|
data: {
|
||||||
|
version,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const installCompose = (composeVersion: string | null, githubToken: string | null) =>
|
||||||
|
service.install({
|
||||||
|
composeVersion,
|
||||||
|
cwd: "/path/to/cwd",
|
||||||
|
githubToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
const setPlatform = (platform: NodeJS.Platform) => {
|
||||||
|
Object.defineProperty(process, "platform", {
|
||||||
|
value: platform,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockLatestRelease = (version: string) => {
|
||||||
|
const mockClient = mockAgent.get("https://api.github.com");
|
||||||
|
mockClient
|
||||||
|
.intercept({
|
||||||
|
path: "/repos/docker/compose/releases/latest",
|
||||||
|
method: "GET",
|
||||||
|
})
|
||||||
|
.reply(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
tag_name: version,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
setGlobalDispatcher(mockClient);
|
||||||
|
Object.defineProperty(globalThis, "fetch", {
|
||||||
|
value: jest.fn(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
mockAgent = new MockAgent();
|
mockAgent = new MockAgent();
|
||||||
@ -36,21 +82,10 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
describe("install", () => {
|
describe("install", () => {
|
||||||
it("should return current version when no version is provided", async () => {
|
it("should return current version when no version is provided", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValue({
|
versionMock.mockResolvedValue(composeVersionResponse("2.0.0"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "2.0.0",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose(null, null);
|
||||||
composeVersion: null,
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe("2.0.0");
|
expect(result).toBe("2.0.0");
|
||||||
@ -59,21 +94,10 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
|
|
||||||
it("should not install anything when expected version is already installed", async () => {
|
it("should not install anything when expected version is already installed", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValue({
|
versionMock.mockResolvedValue(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose("v1.2.3", null);
|
||||||
composeVersion: "1.2.3",
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe("1.2.3");
|
expect(result).toBe("1.2.3");
|
||||||
@ -82,35 +106,14 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
|
|
||||||
it("should install the requested version if it is not already installed", async () => {
|
it("should install the requested version if it is not already installed", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const expectedVersion = "1.3.0";
|
const expectedVersion = "1.3.0";
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse(expectedVersion));
|
||||||
exitCode: 0,
|
setPlatform("linux");
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: expectedVersion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
|
||||||
value: "linux",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose(expectedVersion, null);
|
||||||
composeVersion: expectedVersion,
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe(expectedVersion);
|
expect(result).toBe(expectedVersion);
|
||||||
@ -119,58 +122,15 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
|
|
||||||
it("should install the latest version if requested", async () => {
|
it("should install the latest version if requested", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const latestVersion = "v1.4.0";
|
const latestVersion = "v1.4.0";
|
||||||
|
mockLatestRelease(latestVersion);
|
||||||
const mockClient = mockAgent.get("https://api.github.com");
|
versionMock.mockResolvedValueOnce(composeVersionResponse(latestVersion));
|
||||||
mockClient
|
setPlatform("linux");
|
||||||
.intercept({
|
|
||||||
path: "/repos/docker/compose/releases/latest",
|
|
||||||
method: "GET",
|
|
||||||
})
|
|
||||||
.reply(
|
|
||||||
200,
|
|
||||||
{
|
|
||||||
tag_name: latestVersion,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"content-type": "application/json",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
setGlobalDispatcher(mockClient);
|
|
||||||
|
|
||||||
versionMock.mockResolvedValueOnce({
|
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: latestVersion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
|
||||||
value: "linux",
|
|
||||||
});
|
|
||||||
Object.defineProperty(globalThis, "fetch", {
|
|
||||||
value: jest.fn(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose("latest", "token");
|
||||||
composeVersion: "latest",
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: "token",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe(latestVersion);
|
expect(result).toBe(latestVersion);
|
||||||
@ -179,58 +139,26 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
|
|
||||||
it("should throw an error if the latest version if requested and no Github token is provided", async () => {
|
it("should throw an error if the latest version if requested and no Github token is provided", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
await expect(
|
await expect(installCompose("latest", null)).rejects.toThrow(
|
||||||
service.install({
|
"GitHub token is required to install the latest version"
|
||||||
composeVersion: "latest",
|
);
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
})
|
|
||||||
).rejects.toThrow("GitHub token is required to install the latest version");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw an error on unsupported platforms", async () => {
|
it("should throw an error on unsupported platforms", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const expectedVersion = "1.3.0";
|
const expectedVersion = "1.3.0";
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse(expectedVersion));
|
||||||
exitCode: 0,
|
setPlatform("win32");
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: expectedVersion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
|
||||||
value: "win32",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
await expect(
|
await expect(installCompose(expectedVersion, null)).rejects.toThrow(
|
||||||
service.install({
|
`Unsupported platform: win32`
|
||||||
composeVersion: expectedVersion,
|
);
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
})
|
|
||||||
).rejects.toThrow(`Unsupported platform: win32`);
|
|
||||||
|
|
||||||
expect(manualInstallerAdapterMock.install).not.toHaveBeenCalled();
|
expect(manualInstallerAdapterMock.install).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@ -242,25 +170,11 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
const installedVersion = "2.0.0";
|
const installedVersion = "2.0.0";
|
||||||
|
|
||||||
// After installation, version() returns the new version
|
// After installation, version() returns the new version
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse(installedVersion));
|
||||||
exitCode: 0,
|
setPlatform("linux");
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: installedVersion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
|
||||||
value: "linux",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose(installedVersion, "token");
|
||||||
composeVersion: installedVersion,
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: "token",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe(installedVersion);
|
expect(result).toBe(installedVersion);
|
||||||
@ -271,58 +185,15 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
// Arrange: first call to version() doesn't find
|
// Arrange: first call to version() doesn't find
|
||||||
versionMock.mockRejectedValueOnce(new Error("version check failed"));
|
versionMock.mockRejectedValueOnce(new Error("version check failed"));
|
||||||
// second call finds newly installed version
|
// second call finds newly installed version
|
||||||
versionMock.mockResolvedValueOnce({
|
versionMock.mockResolvedValueOnce(composeVersionResponse("v1.4.0"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "v1.4.0",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const latestVersion = "v1.4.0";
|
const latestVersion = "v1.4.0";
|
||||||
|
mockLatestRelease(latestVersion);
|
||||||
const mockClient = mockAgent.get("https://api.github.com");
|
versionMock.mockResolvedValueOnce(composeVersionResponse(latestVersion));
|
||||||
mockClient
|
setPlatform("linux");
|
||||||
.intercept({
|
|
||||||
path: "/repos/docker/compose/releases/latest",
|
|
||||||
method: "GET",
|
|
||||||
})
|
|
||||||
.reply(
|
|
||||||
200,
|
|
||||||
{
|
|
||||||
tag_name: latestVersion,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"content-type": "application/json",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
setGlobalDispatcher(mockClient);
|
|
||||||
|
|
||||||
versionMock.mockResolvedValueOnce({
|
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: latestVersion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
|
||||||
value: "linux",
|
|
||||||
});
|
|
||||||
Object.defineProperty(globalThis, "fetch", {
|
|
||||||
value: jest.fn(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose("latest", "token");
|
||||||
composeVersion: "latest",
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: "token",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe(latestVersion);
|
expect(result).toBe(latestVersion);
|
||||||
@ -332,41 +203,38 @@ describe("DockerComposeInstallerService", () => {
|
|||||||
it("should throw if Compose is missing and no GitHub token is provided", async () => {
|
it("should throw if Compose is missing and no GitHub token is provided", async () => {
|
||||||
// Arrange: first call to version() doesn't find
|
// Arrange: first call to version() doesn't find
|
||||||
versionMock.mockRejectedValueOnce(new Error("version check failed"));
|
versionMock.mockRejectedValueOnce(new Error("version check failed"));
|
||||||
|
setPlatform("linux");
|
||||||
|
|
||||||
Object.defineProperty(process, "platform", {
|
await expect(installCompose("latest", null)).rejects.toThrow(
|
||||||
value: "linux",
|
"GitHub token is required to install the latest version"
|
||||||
});
|
);
|
||||||
|
|
||||||
await expect(
|
|
||||||
service.install({
|
|
||||||
composeVersion: "latest",
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
})
|
|
||||||
).rejects.toThrow("GitHub token is required to install the latest version");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not install when the version is already installed and no version is specified", async () => {
|
it("should not install when the version is already installed and no version is specified", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
versionMock.mockResolvedValue({
|
versionMock.mockResolvedValue(composeVersionResponse("1.2.3"));
|
||||||
exitCode: 0,
|
|
||||||
out: "",
|
|
||||||
err: "",
|
|
||||||
data: {
|
|
||||||
version: "1.2.3",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await service.install({
|
const result = await installCompose("", null);
|
||||||
composeVersion: "",
|
|
||||||
cwd: "/path/to/cwd",
|
|
||||||
githubToken: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result).toBe("1.2.3");
|
expect(result).toBe("1.2.3");
|
||||||
expect(manualInstallerAdapterMock.install).not.toHaveBeenCalled();
|
expect(manualInstallerAdapterMock.install).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should throw when installed version does not match target", async () => {
|
||||||
|
// Arrange
|
||||||
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.2.3"));
|
||||||
|
|
||||||
|
const targetVersion = "v1.4.0";
|
||||||
|
versionMock.mockResolvedValueOnce(composeVersionResponse("1.3.0"));
|
||||||
|
setPlatform("linux");
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
await expect(installCompose(targetVersion, "token")).rejects.toThrow(
|
||||||
|
`Failed to install Docker Compose version "${targetVersion}", installed version is "1.3.0"`
|
||||||
|
);
|
||||||
|
expect(manualInstallerAdapterMock.install).toHaveBeenCalledWith(targetVersion);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -17,9 +17,16 @@ export class DockerComposeInstallerService {
|
|||||||
constructor(private readonly manualInstallerAdapter: ManualInstallerAdapter) {}
|
constructor(private readonly manualInstallerAdapter: ManualInstallerAdapter) {}
|
||||||
|
|
||||||
async install({ composeVersion, cwd, githubToken }: InstallInputs): Promise<string> {
|
async install({ composeVersion, cwd, githubToken }: InstallInputs): Promise<string> {
|
||||||
const currentVersion = await this.getInstalledVersion(cwd);
|
const currentVersion = await this.version({ cwd });
|
||||||
|
|
||||||
const needsInstall = !currentVersion || (composeVersion && composeVersion !== currentVersion);
|
const normalizedCurrentVersion = currentVersion ? this.normalizeVersion(currentVersion) : null;
|
||||||
|
const normalizedRequestedVersion = composeVersion
|
||||||
|
? this.normalizeVersion(composeVersion)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const needsInstall =
|
||||||
|
!currentVersion ||
|
||||||
|
(composeVersion && normalizedRequestedVersion !== normalizedCurrentVersion);
|
||||||
if (!needsInstall) {
|
if (!needsInstall) {
|
||||||
return currentVersion;
|
return currentVersion;
|
||||||
}
|
}
|
||||||
@ -35,24 +42,32 @@ export class DockerComposeInstallerService {
|
|||||||
|
|
||||||
await this.installVersion(targetVersion);
|
await this.installVersion(targetVersion);
|
||||||
|
|
||||||
return this.version({ cwd });
|
const installedVersion = await this.version({ cwd });
|
||||||
|
|
||||||
|
if (
|
||||||
|
!installedVersion ||
|
||||||
|
this.normalizeVersion(installedVersion) !== this.normalizeVersion(targetVersion)
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to install Docker Compose version "${targetVersion}", installed version is "${installedVersion ?? "unknown"}"`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return installedVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getInstalledVersion(cwd: string): Promise<string | null> {
|
private async version({ cwd }: VersionInputs): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
return await this.version({ cwd });
|
const result = await version({
|
||||||
|
cwd,
|
||||||
|
});
|
||||||
|
return result.data.version;
|
||||||
} catch {
|
} catch {
|
||||||
|
// If version check fails (e.g., Docker Compose not installed), return null
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async version({ cwd }: VersionInputs): Promise<string> {
|
|
||||||
const result = await version({
|
|
||||||
cwd,
|
|
||||||
});
|
|
||||||
return result.data.version;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getLatestVersion(githubToken: string): Promise<string> {
|
private async getLatestVersion(githubToken: string): Promise<string> {
|
||||||
const octokit = github.getOctokit(githubToken);
|
const octokit = github.getOctokit(githubToken);
|
||||||
|
|
||||||
@ -64,6 +79,10 @@ export class DockerComposeInstallerService {
|
|||||||
return response.data.tag_name;
|
return response.data.tag_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private normalizeVersion(version: string): string {
|
||||||
|
return version.replace(/^v/i, "");
|
||||||
|
}
|
||||||
|
|
||||||
private async installVersion(version: string): Promise<void> {
|
private async installVersion(version: string): Promise<void> {
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case "linux":
|
case "linux":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user