Alexandru Circus Alexandru Circus - 3 months ago 12
TypeScript Question

Overwrite static member by using another file

I'm converting a js project to typeScript and I want to overwrite some constants static members by using a local/untracked_by_git file.

My original constants file:

export default class Constants{
static MYSQL_HOST = 'some_host';
static MYSQL_PORT = 0000;
static MYSQL_DATABASE = 'prod_database_name';
static MYSQL_USER = 'user';
static MYSQL_PWD = 'password';
}


I want a local file (untracked by versioning system) that is able to overwrite any of this fields (in order to connect to some particular db, different than my collegues)

Answer

following the advice from @mario-alexandro-santini

export default class Constants{
    static MYSQL_HOST = process.env.MYSQL_HOST || 'some_host';
    static MYSQL_PORT = process.env.MYSQL_PORT 0000;
    static MYSQL_DATABASE = process.env.MYSQL_DATABASE || 'prod_database_name';
    static MYSQL_USER = process.env.MYSQL_USER || 'user';
    static MYSQL_PWD = process.env.MYSQL_PWD || 'password';
}

assuming you are running in a UNIX based environment you can have a bash file lets call it set-env.sh. add that file name to the .gitignore file and it should contain something like this.

#!/bin/sh
MYSQL_HOST="Other values"
MYSQL_PORT="LOL"
MYSQL_DATABASE="LAlaLA"
//etc..

when you run the app just do something like ./set-env.sh && <the command you did before>