We have special rules to setup env variables on our javascript projects For providing variables we have
.envfile in the root of the project For sharing this variables through a project we creatingconfig.tsfile For launch in different env we're creating special npm script Also we're using third party libraries which helps us to create correct environment cross-env and dotenv
File with all env variables
API_URL=http://server.com/api
API_URL_DEV=http://localhost:6000
MONGO_NAME=prod
MONGO_PASSWORD=prod-password
MONGO_NAME_DEV=test
MONGO_PASSWORD_DEV=password
JWT_SECRET=secret
How we handle different env in code using Single Responsibility Principle
interface IConfig {
apiUrl: string;
zoomId: string;
zoomRedirectUrl: string;
}
const mode: string = process.env.NODE_ENV || "development";
const configMap: IHashMap<IConfig> = {
development: {
apiUrl: process.env.API_URL_DEV,
mongoName: process.env.MONGO_NAME_DEV,
mongoPassword: process.env.MONGO_PASSWORD_DEV,
jwtSecret: process.env.JWT_SECRET,
},
production: {
apiUrl: process.env.API_URL,
mongoName: process.env.MONGO_NAME,
mongoPassword: process.env.MONGO_PASSWORD,
jwtSecret: process.env.JWT_SECRET,
},
};
export const CONFIG = configMap[mode];Usual npm start should launch with dev environments to prevent random production launch
"scripts": {
"start": "react-scripts start",
"start:prod": "cross-env NODE_ENV=production react-scripts start",
"build": "react-scripts build",
},{
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"jsxSingleQuote": false,
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"trailingComma": "es5",
"endOfLine": "lf",
"printWidth": 80
}ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code.
Run linters against staged git files and don't let 💩 slip into your codebase!
npm i -D lint-staged prettier tslint "scripts": {
"lint": "tslint 'src/**/*.{ts,tsx}'",
"precommit": "lint-staged",
"prettier": "prettier --config ./.prettierrc --write \"./src/**/*.{ts,tsx}\""
},
"lint-staged": {
"*.{ts,tsx}": [
"yarn prettier",
"yarn lint",
"git add"
]
}react-native-typescript-transformer react-navigation react-native-vector-icons react-native-dropdownalert