Created
          January 28, 2019 15:31 
        
      - 
      
- 
        Save marcosborges/86b7b44d379e7e2ea72a2a098a4d0adf to your computer and use it in GitHub Desktop. 
    Angular - Trabalhando com criação dinâmica das configurações com base nas variáveis de ambiente
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ... | |
| "scripts": { | |
| "ng": "ng", | |
| "config": "ts-node ./src/set-env.ts", | |
| "start": "npm run config -- --environment=dev && ng serve", | |
| "build": "npm run config && ng build --output-path=dist", | |
| "test": "ng test", | |
| "lint": "ng lint", | |
| "e2e": "ng e2e" | |
| }, | |
| ... | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const fs = require('fs'); | |
| const yargs = require('yargs'); | |
| if(!yargs.argv.environment){ | |
| const dotenv = require('dotenv'); | |
| dotenv.config(); | |
| } | |
| const targetPath = `./src/environments/environment.ts`; | |
| const envConfigFile = ` | |
| export const environment = { | |
| production: true, | |
| url_api: "${process.env.URL_API}", | |
| token_name: "${process.env.TOKEN_NAME}", | |
| }; | |
| ` | |
| fs.writeFile(targetPath, envConfigFile, function (err) { | |
| if (err) { | |
| console.log(err); | |
| } | |
| console.log(`Output generated at ${targetPath}`); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment