Last active
January 5, 2021 21:19
-
-
Save rossholdway/16724496806b66a162ee6cbf8bfc5def to your computer and use it in GitHub Desktop.
A script to run before the ionic build process that generates an app.config.ts file based off of a .env/{environment}.json file. See this comment for more info: https://github.com/ionic-team/ionic-app-scripts/issues/762#issuecomment-295235407
This file contains 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
#!/usr/bin/env node | |
var path = require('path'); | |
var process = require('process'); | |
var fs = require('fs'); | |
class Environment { | |
constructor(args) { | |
args = JSON.parse(args).original; | |
const defaultEnv = 'development'; //Set default environment | |
let env; | |
if(args.includes('ionic:build')) { | |
let envFlags = args.filter((arg) => arg.includes("--env")); | |
env = (envFlags.length === 1) ? envFlags[0].substr(envFlags[0].indexOf("=") + 1) : defaultEnv; | |
} else { | |
let index = args.indexOf('--env'); | |
env = (index > -1) ? args[index+1] : defaultEnv; | |
} | |
console.log(`Using environment config: ${env}`); | |
this.setEnvironment(env); | |
} | |
setEnvironment(env) { | |
let config; | |
try { | |
config = require(path.join('../', '.env', env + '.json')); | |
} catch(e) { | |
throw new Error(`The config file for this environment is missing (${e.message})`); | |
} | |
var wstream = fs.createWriteStream(path.resolve('./src/app/app.config.ts')); | |
wstream.write(` | |
import { OpaqueToken } from "@angular/core"; //Use InjectionToken in Angular 4.x | |
export let APP_CONFIG = new OpaqueToken("app.config"); | |
export const AppConfig = ${JSON.stringify(config)}; | |
`); | |
wstream.end(); | |
} | |
} | |
new Environment(process.env.npm_config_argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rossholdway any updates on this? Did you get this working again with the
--env=
flag?