refactor: add path join to prevent concatenate double slash and update the getImageName

This commit is contained in:
Mauricio Siu
2024-12-19 02:05:30 -06:00
parent 844d582147
commit c51b502116
2 changed files with 250 additions and 248 deletions

View File

@@ -37,13 +37,13 @@ export type ApplicationNested = InferResultType<
export const buildApplication = async (
application: ApplicationNested,
logPath: string,
logPath: string
) => {
const writeStream = createWriteStream(logPath, { flags: "a" });
const { buildType, sourceType } = application;
try {
writeStream.write(
`\nBuild ${buildType}: ✅\nSource Type: ${sourceType}: ✅\n`,
`\nBuild ${buildType}: ✅\nSource Type: ${sourceType}: ✅\n`
);
console.log(`Build ${buildType}: ✅`);
if (buildType === "nixpacks") {
@@ -77,7 +77,7 @@ export const buildApplication = async (
export const getBuildCommand = (
application: ApplicationNested,
logPath: string,
logPath: string
) => {
let command = "";
const { buildType, registry } = application;
@@ -106,7 +106,7 @@ export const getBuildCommand = (
};
export const mechanizeDockerContainer = async (
application: ApplicationNested,
application: ApplicationNested
) => {
const {
appName,
@@ -144,7 +144,7 @@ export const mechanizeDockerContainer = async (
const filesMount = generateFileMounts(appName, application);
const envVariables = prepareEnvironmentVariables(
env,
application.project.env,
application.project.env
);
const image = getImageName(application);
@@ -211,21 +211,21 @@ const getImageName = (application: ApplicationNested) => {
}
if (registry) {
return join(registry.imagePrefix || "", appName);
return join(registry.registryUrl, registry.imagePrefix || "", appName);
}
return `${appName}:latest`;
};
const getAuthConfig = (application: ApplicationNested) => {
const { registry, username, password, sourceType } = application;
const { registry, username, password, sourceType, registryUrl } = application;
if (sourceType === "docker") {
if (username && password) {
return {
password,
username,
serveraddress: "https://index.docker.io/v1/",
serveraddress: registryUrl || "",
};
}
} else if (registry) {

View File

@@ -1,11 +1,11 @@
import type { WriteStream } from "node:fs";
import { join } from "node:path";
import path, { join } from "node:path";
import type { ApplicationNested } from "../builders";
import { spawnAsync } from "../process/spawnAsync";
export const uploadImage = async (
application: ApplicationNested,
writeStream: WriteStream,
writeStream: WriteStream
) => {
const registry = application.registry;
@@ -13,18 +13,19 @@ export const uploadImage = async (
throw new Error("Registry not found");
}
const { registryUrl, imagePrefix, registryType } = registry;
const { registryUrl, imagePrefix } = registry;
const { appName } = application;
const imageName = `${appName}:latest`;
const finalURL = registryUrl;
const registryTag =
`${registryUrl}/${join(imagePrefix || "", imageName)}`.replace(/\/+/g, "/");
const registryTag = path
.join(registryUrl, join(imagePrefix || "", imageName))
.replace(/\/+/g, "/");
try {
writeStream.write(
`📦 [Enabled Registry] Uploading image to ${registry.registryType} | ${imageName} | ${finalURL}\n`,
`📦 [Enabled Registry] Uploading image to ${registry.registryType} | ${imageName} | ${finalURL}\n`
);
const loginCommand = spawnAsync(
"docker",
@@ -33,7 +34,7 @@ export const uploadImage = async (
if (writeStream.writable) {
writeStream.write(data);
}
},
}
);
loginCommand.child?.stdin?.write(registry.password);
loginCommand.child?.stdin?.end();
@@ -58,7 +59,7 @@ export const uploadImage = async (
export const uploadImageRemoteCommand = (
application: ApplicationNested,
logPath: string,
logPath: string
) => {
const registry = application.registry;
@@ -72,7 +73,9 @@ export const uploadImageRemoteCommand = (
const finalURL = registryUrl;
const registryTag = join(imagePrefix || "", imageName);
const registryTag = path
.join(registryUrl, join(imagePrefix || "", imageName))
.replace(/\/+/g, "/");
try {
const command = `
@@ -95,7 +98,6 @@ export const uploadImageRemoteCommand = (
`;
return command;
} catch (error) {
console.log(error);
throw error;
}
};