#!/usr/bin/env node
/**
* Inject SAM API Configuration into Web Component Build
*
* This script runs before the Vite build to embed:
* 1. SAM GraphQL endpoint and API key for chat queries
* 2. Theme configuration - Fallback defaults
*
* Configuration comes from environment variables (set by CodeBuild):
* - SAM_GRAPHQL_ENDPOINT: SAM stack AppSync GraphQL URL (required)
* - SAM_GRAPHQL_API_KEY: API key for API access (optional)
*
* Usage: node scripts/inject-amplify-config.js
*
* Writes: src/amplify-config.generated.ts (imported by src/wc.ts)
*/
const fs = require('fs');
const path = require('path');
// Paths relative to src/ragstack-chat/scripts/
const LOCAL_THEME_PATH = path.join(__dirname, '../theme_config.json');
const REPO_ROOT_THEME_PATH = path.join(__dirname, '../../../theme_config.json');
const GENERATED_CONFIG_PATH = path.join(__dirname, '../src/amplify-config.generated.ts');
/**
* Get theme configuration from environment variables or file
*/
function getThemeConfig() {
// Priority 1: Environment variables (set by CodeBuild from DynamoDB)
const hasEnvOverrides = process.env.CHAT_PRIMARY_COLOR || process.env.CHAT_FONT_FAMILY || process.env.CHAT_SPACING;
if (hasEnvOverrides) {
console.log('✓ Using theme config from environment variables');
return {
themeOverrides: {
primaryColor: process.env.CHAT_PRIMARY_COLOR || undefined,
fontFamily: process.env.CHAT_FONT_FAMILY || undefined,
spacing: process.env.CHAT_SPACING || undefined,
}
};
}
// Priority 2: theme_config.json file
const themePath = fs.existsSync(LOCAL_THEME_PATH) ? LOCAL_THEME_PATH :
fs.existsSync(REPO_ROOT_THEME_PATH) ? REPO_ROOT_THEME_PATH : null;
if (themePath) {
try {
const themeRaw = fs.readFileSync(themePath, 'utf-8');
const theme = JSON.parse(themeRaw);
console.log('✓ Using theme config from:', themePath);
// Remove themePreset if present in file
delete theme.themePreset;
return theme;
} catch (e) {
console.warn('⚠ Failed to parse theme_config.json:', e.message);
}
}
// Priority 3: Defaults
console.log('✓ Using default theme config');
return {
themeOverrides: {}
};
}
function main() {
console.log('🔧 Injecting SAM API configuration into web component...');
// Get SAM API configuration from environment variables
const samEndpoint = process.env.SAM_GRAPHQL_ENDPOINT || '';
const samApiKey = process.env.SAM_GRAPHQL_API_KEY || '';
if (!samEndpoint) {
console.warn('⚠ Warning: SAM_GRAPHQL_ENDPOINT not set');
console.warn(' The web component will not be able to make API calls.');
console.warn(' Set SAM_GRAPHQL_ENDPOINT environment variable.');
} else {
console.log('✓ SAM GraphQL endpoint:', samEndpoint);
}
if (samApiKey) {
console.log('✓ SAM API key configured');
} else {
console.log('⚠ SAM API key not set (may be using IAM auth)');
}
// Get theme configuration (fallback defaults)
const themeConfig = getThemeConfig();
// Generate TypeScript config file
const configContent = `/**
* Auto-generated SAM API Configuration
*
* This file is generated by scripts/inject-amplify-config.js during build.
* DO NOT EDIT MANUALLY - changes will be overwritten.
*
* Generated: ${new Date().toISOString()}
*/
/**
* SAM Stack GraphQL API endpoint and API key
* Used by ChatInterface to query the knowledge base
*/
declare const SAM_GRAPHQL_ENDPOINT: string;
declare const SAM_GRAPHQL_API_KEY: string;
/**
* Theme configuration embedded at build time (fallback defaults)
*
* These defaults are used if runtime theme fetching fails.
* Can be overridden per-instance using attributes on <ragstack-chat>.
*/
export const THEME_CONFIG = ${JSON.stringify(themeConfig, null, 2)} as const;
`;
// Write config file
fs.writeFileSync(GENERATED_CONFIG_PATH, configContent, 'utf-8');
console.log('✅ Configuration injected successfully');
console.log(` SAM Endpoint: ${samEndpoint || 'Not configured'}`);
}
main();