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

View File

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