question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set environment variable using command line arguments

See original GitHub issue

I’m submitting a feature request

  • Library Version: CLI 0.26.0 Framework 1.0.8

Please tell us about your environment:

  • Operating System: Windows 10

  • Node Version: 6.9.4

  • NPM Version: 4.1.1

  • Browser: Chrome 56 but it’s not browser related

  • Language: TypeScript 2.1.5

I’d sometimes need the possibility to set the value of a specific environment variable using a command line argument.

E.g. I’d like to set a version number which is generated by my build server:

aurelia_project/environments/prod.ts

export default {
    debug: false,
    testing: false,
    // $buildVersion$ should be replaced during build with the actual value
    buildVersion: $buildVersion$
};

Imaginary command: au build --env prod --buildVersion 1.1.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
suamikimcommented, Mar 14, 2017

Perfect, this is exactly the hook I was looking for. Thanks for the tip!

1reaction
AStokercommented, Mar 13, 2017

Quick note, the src/environtment.ts file is modified at build time to reflect how you are trying to run. If you run with the dev environment, the file will have the options that represent that (as configured by the aurelia.json file). So if you manually modified it, and then ran, then that file would have been updated with your build instructions.

Now, the src/environments should be what is pulled down when you ask for it, as you can see this in the main.js of the skeleton project. The reason you’re not getting your updated file contents is because of the order in which you are processing your files. If you look at the transpile task, that is where the environment variables are read in and the appropriate file copied. But also inside here is where we transpile/build the code. Which means your replacement function is happening after the code is transpiled, and therefore not being brought into your bundle.

To solve your problem, simply do your replace function inside the transpile.js configureEnvironment function, since that is where you should be doing this kind of configuration.

So, your configureEnvironment function simply is modified to look like this (this is for ES, fyi, but the solution is identical).

function configureEnvironment() {
  let env = CLIOptions.getEnvironment();
  let buildVersion = CLIOptions.getFlagValue('buildVersion') || '0.0.0'; //Added this to get buildVersion
  return gulp.src(`aurelia_project/environments/${env}.js`)
    .pipe(changedInPlace({firstPass:true}))
    .pipe(replace('{buildVersion}', buildVersion)) //Replace your text with the build version
    .pipe(rename('environment.js'))
    .pipe(gulp.dest(project.paths.root));
}

That’s it, that’s all it takes 😃 Let me know if this still doesn’t work, or if you have any more questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set an Environment Variable in Windows - Command Line ...
To set persistent environment variables at the command line, we will use setx.exe . It became part of Windows as of Vista/Windows Server...
Read more >
set (environment variable) - Microsoft Learn
Displays, sets, or removes cmd.exe environment variables. If used without parameters, set displays the current environment variable settings.
Read more >
Using the command prompt to change environment variables
The following diagram shows the syntax for setting an environment variable at a command prompt in Windows. If no value is specified, the...
Read more >
Argument passing strategy - environment variables vs ...
An environmental variable is simply a pair, VARIABLE=value . Command line arguments can be much more complicated: they can be positional or ...
Read more >
How to Change your Command Line Environment
Setting Environment Variables on the Fly · Set the variable on its own line, then use it anywhere: $ SOMETHING="some value" $ echo...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found