( deployment: Omit< z.infer<typeof apiCreateDeploymentPreview>, "deploymentId" | "createdAt" | "status" | "logPath" >, )
| 192 | }; |
| 193 | |
| 194 | export const createDeploymentPreview = async ( |
| 195 | deployment: Omit< |
| 196 | z.infer<typeof apiCreateDeploymentPreview>, |
| 197 | "deploymentId" | "createdAt" | "status" | "logPath" |
| 198 | >, |
| 199 | ) => { |
| 200 | const previewDeployment = await findPreviewDeploymentById( |
| 201 | deployment.previewDeploymentId, |
| 202 | ); |
| 203 | await removeLastTenDeployments( |
| 204 | deployment.previewDeploymentId, |
| 205 | "previewDeployment", |
| 206 | previewDeployment?.application?.serverId, |
| 207 | ); |
| 208 | try { |
| 209 | const appName = `${previewDeployment.appName}`; |
| 210 | const { LOGS_PATH } = paths(!!previewDeployment?.application?.serverId); |
| 211 | const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); |
| 212 | const fileName = `${appName}-${formattedDateTime}.log`; |
| 213 | const logFilePath = path.join(LOGS_PATH, appName, fileName); |
| 214 | |
| 215 | if (previewDeployment?.application?.serverId) { |
| 216 | const server = await findServerById( |
| 217 | previewDeployment?.application?.serverId, |
| 218 | ); |
| 219 | |
| 220 | const command = ` |
| 221 | mkdir -p ${LOGS_PATH}/${appName}; |
| 222 | echo "Initializing deployment" >> ${logFilePath}; |
| 223 | `; |
| 224 | |
| 225 | await execAsyncRemote(server.serverId, command); |
| 226 | } else { |
| 227 | await fsPromises.mkdir(path.join(LOGS_PATH, appName), { |
| 228 | recursive: true, |
| 229 | }); |
| 230 | await fsPromises.writeFile(logFilePath, "Initializing deployment"); |
| 231 | } |
| 232 | |
| 233 | const deploymentCreate = await db |
| 234 | .insert(deployments) |
| 235 | .values({ |
| 236 | title: deployment.title || "Deployment", |
| 237 | status: "running", |
| 238 | logPath: logFilePath, |
| 239 | description: deployment.description || "", |
| 240 | previewDeploymentId: deployment.previewDeploymentId, |
| 241 | startedAt: new Date().toISOString(), |
| 242 | }) |
| 243 | .returning(); |
| 244 | if (deploymentCreate.length === 0 || !deploymentCreate[0]) { |
| 245 | throw new TRPCError({ |
| 246 | code: "BAD_REQUEST", |
| 247 | message: "Error creating the deployment", |
| 248 | }); |
| 249 | } |
| 250 | return deploymentCreate[0]; |
| 251 | } catch (error) { |
no test coverage detected