UNPKG

dryrun-ci

Version:

DryRun CI - Local GitLab CI/CD pipeline testing tool with Docker execution, performance monitoring, and security sandboxing

113 lines (112 loc) 4.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerBuilder = void 0; const child_process_1 = require("child_process"); const util_1 = require("util"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const toml_1 = require("@iarna/toml"); const access = (0, util_1.promisify)(fs.access); const readFile = (0, util_1.promisify)(fs.readFile); class ContainerBuilder { async checkNixpacksInstalled() { try { await new Promise((resolve, reject) => { const nixpacks = (0, child_process_1.spawn)('nixpacks', ['--version']); nixpacks.on('error', reject); nixpacks.on('close', (code) => { if (code === 0) resolve(true); else reject(new Error('Nixpacks not found')); }); }); return true; } catch (error) { return false; } } async hasNixpacksConfig(projectPath) { try { const nixpacksPath = path.join(projectPath, 'nixpacks.toml'); await access(nixpacksPath, fs.constants.R_OK); // Validate the TOML file const content = await readFile(nixpacksPath, 'utf8'); (0, toml_1.parse)(content); return true; } catch (error) { return false; } } async buildImage(projectPath, imageName) { const hasNixpacks = await this.hasNixpacksConfig(projectPath); const nixpacksInstalled = await this.checkNixpacksInstalled(); if (hasNixpacks && nixpacksInstalled) { // Use Nixpacks for building await new Promise((resolve, reject) => { const nixpacks = (0, child_process_1.spawn)('nixpacks', ['build', projectPath, '--name', imageName], { stdio: 'inherit' }); nixpacks.on('error', reject); nixpacks.on('close', (code) => { if (code === 0) resolve(true); else reject(new Error(`Nixpacks build failed with code ${code}`)); }); }); } else if (hasNixpacks && !nixpacksInstalled) { throw new Error('Nixpacks configuration found but nixpacks is not installed. Please install nixpacks first.'); } else { // Fallback to Dockerfile if exists, otherwise use auto-detection const dockerfilePath = path.join(projectPath, 'Dockerfile'); try { await access(dockerfilePath, fs.constants.R_OK); // Use Docker build await new Promise((resolve, reject) => { const docker = (0, child_process_1.spawn)('docker', ['build', '-t', imageName, projectPath], { stdio: 'inherit' }); docker.on('error', reject); docker.on('close', (code) => { if (code === 0) resolve(true); else reject(new Error(`Docker build failed with code ${code}`)); }); }); } catch (error) { throw new Error('No valid build configuration found (nixpacks.toml or Dockerfile)'); } } } } exports.ContainerBuilder = ContainerBuilder;