gshigeto / ionic-environment-variables

Easy to use environment variables for Ionic3!
MIT License
276 stars 36 forks source link

Custom environments command #13

Closed webbkvalite closed 6 years ago

webbkvalite commented 6 years ago

When following the readme file and running: "serve:testing": "MY_ENV=testing ionic-app-scripts serve"

The following error is shown: 'MY_ENV' is not recognized as an internal or external command

How is the command supposed to be run?

Robinyo commented 6 years ago

Hi,

Have you tried setting your ENV var from the command line?

"test": "ionic-app-scripts serve",

 npm run test
 MY_ENV=test npm run test

Or try setting your ENV and running another command. For example, on macOS/Linux:

MY_ENV=test npm run clean

or Windows:

set MY_ENV=test npm run clean

Cheers Rob

Cacowned commented 6 years ago

@webbkvalite Which OS do you use?

webbkvalite commented 6 years ago

I'm using Windows. It works well now, when first setting the variable with "set":

Updated the "scripts" in package.json (with a little extra memory for my big app):

  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "ionic:build:dev": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
    "ionic:build:staging": "set MY_ENV=staging&&node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
    "ionic:build:prod": "set MY_ENV=prod&&node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build --prod",
    "ionic:serve:dev": "ionic-app-scripts serve --port 8100",
    "ionic:serve:staging": "set MY_ENV=staging&& ionic-app-scripts serve",
    "ionic:serve:prod": "set MY_ENV=prod&& ionic-app-scripts serve",
    "watch": "ionic-app-scripts watch"
  },

Also did a little change in the webpack.config.js to suit my setup:

var chalk = require("chalk");
var fs = require('fs');
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');

var env = process.env.IONIC_ENV;
var myEnv = process.env.MY_ENV;

useDefaultConfig[env].resolve.alias = {
    "@app/env": path.resolve(environmentPath())
};

function environmentPath() {
    var filePath = './src/environments/environment.' + (myEnv || env) + '.ts';
    if (!fs.existsSync(filePath)) {
        console.log(chalk.red('\n' + filePath + ' does not exist!'));
    } else {
        console.log("Using " + filePath);
        return filePath;
    }
}

module.exports = function () {
    return useDefaultConfig;
};