|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import * as fs from 'fs-extra'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +import { DEFAULT_BUILD_TIMEOUT_SECONDS } from './constants'; |
| 7 | +import type { Env, RecipeInstance } from './types'; |
| 8 | +import { spawnAsync } from './utils'; |
| 9 | + |
| 10 | +export async function buildApp(appDir: string, recipeInstance: RecipeInstance, env: Env): Promise<void> { |
| 11 | + const { recipe, label, dependencyOverrides } = recipeInstance; |
| 12 | + |
| 13 | + const packageJsonPath = path.resolve(appDir, 'package.json'); |
| 14 | + |
| 15 | + if (dependencyOverrides) { |
| 16 | + // Override dependencies |
| 17 | + const packageJson: { dependencies?: Record<string, string> } = JSON.parse( |
| 18 | + fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }), |
| 19 | + ); |
| 20 | + packageJson.dependencies = packageJson.dependencies |
| 21 | + ? { ...packageJson.dependencies, ...dependencyOverrides } |
| 22 | + : dependencyOverrides; |
| 23 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { |
| 24 | + encoding: 'utf-8', |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + if (recipe.buildCommand) { |
| 29 | + console.log(`Running build command for test application "${label}"`); |
| 30 | + |
| 31 | + const buildResult = await spawnAsync(recipe.buildCommand, { |
| 32 | + cwd: appDir, |
| 33 | + timeout: (recipe.buildTimeoutSeconds ?? DEFAULT_BUILD_TIMEOUT_SECONDS) * 1000, |
| 34 | + env: { |
| 35 | + ...process.env, |
| 36 | + ...env, |
| 37 | + } as unknown as NodeJS.ProcessEnv, |
| 38 | + }); |
| 39 | + |
| 40 | + if (buildResult.error) { |
| 41 | + console.log(`Build failed for test application "${label}"`); |
| 42 | + |
| 43 | + // Prepends some text to the output build command's output so we can distinguish it from logging in this script |
| 44 | + console.log(buildResult.stdout.replace(/^/gm, ' [BUILD OUTPUT] ')); |
| 45 | + console.log(buildResult.stderr.replace(/^/gm, ' [BUILD OUTPUT] ')); |
| 46 | + |
| 47 | + console.log('[BUILD ERROR] ', buildResult.error); |
| 48 | + throw buildResult.error; |
| 49 | + } |
| 50 | + |
| 51 | + if (recipe.buildAssertionCommand) { |
| 52 | + console.log(`Running build assertion for test application "${label}"`); |
| 53 | + |
| 54 | + const buildAssertionResult = await spawnAsync( |
| 55 | + recipe.buildAssertionCommand, |
| 56 | + { |
| 57 | + cwd: appDir, |
| 58 | + timeout: (recipe.buildTimeoutSeconds ?? DEFAULT_BUILD_TIMEOUT_SECONDS) * 1000, |
| 59 | + env: { |
| 60 | + ...process.env, |
| 61 | + ...env, |
| 62 | + } as unknown as NodeJS.ProcessEnv, |
| 63 | + }, |
| 64 | + buildResult.stdout, |
| 65 | + ); |
| 66 | + |
| 67 | + if (buildAssertionResult.error) { |
| 68 | + console.log(`Build assertion failed for test application "${label}"`); |
| 69 | + |
| 70 | + // Prepends some text to the output build command's output so we can distinguish it from logging in this script |
| 71 | + console.log(buildAssertionResult.stdout.replace(/^/gm, ' [BUILD ASSERTION OUTPUT] ')); |
| 72 | + console.log(buildAssertionResult.stderr.replace(/^/gm, ' [BUILD ASSERTION OUTPUT] ')); |
| 73 | + |
| 74 | + console.log('[BUILD ASSERTION ERROR] ', buildAssertionResult.error); |
| 75 | + |
| 76 | + throw buildAssertionResult.error; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments