Blog>
Snippets

Angular Monorepo Environment Configuration

Instructions on how to manage and configure environment-specific files for multiple applications within a monorepo.
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// Retrieve the application and environment from the command-line arguments
const [, , app, env] = process.argv;

if (!app || !env) {
    throw new Error('You must provide application name and environment.');
}

// Define the path to the environment-specific configurations
const envConfigPath = path.join(__dirname, 'apps', app, 'src', 'environments');

// Define the file names for environment-specific files
const targetEnvFile = `environment.${env}.ts`;
const defaultEnvFile = `environment.ts`;

// Copy the environment-specific file to the default environment file location
fs.copyFileSync(path.join(envConfigPath, targetEnvFile), path.join(envConfigPath, defaultEnvFile));

// Execute the build or serve command
execSync(
    `ng build ${app} --configuration=${env}`, 
    { stdio: 'inherit' }
);
The code snippet is a simple Node.js script to automate the process of setting up environment-specific configuration files for an Angular application within a monorepo. It expects command-line arguments for the application name and environment. The script will copy the environment-specific configuration to the default file and then execute the Angular CLI build or serve command with the appropriate configuration.