Environment: global variables across dev, stage, prod

So I had some constants that were being duplicated across each of the environment files and knew it was a matter of time before I updated something in one, and missed it in another.

So I updated the aurelia-cli environment task to build out the environment.ts file using a global.ts file located in the environment folder and concatenating the env associated file using gulp-concat (which you have to install to use this.)

Simple but helps with maintainability. I thought others might find it useful.

import * as project from '../aurelia.json';
import * as rename from 'gulp-rename';
import {CLIOptions} from 'aurelia-cli';
import * as gulp from 'gulp';
import * as fs from 'fs';
import path from 'path';
import * as through from 'through2';
import * as concat from 'gulp-concat';

function configureEnvironment() {
  let env = CLIOptions.getEnvironment();

  return gulp.src([
      `aurelia_project/environments/globals${project.transpiler.fileExtension}`,
      `aurelia_project/environments/${env}${project.transpiler.fileExtension}`
    ])
    .pipe(concat(`environment${project.transpiler.fileExtension}`))
    .pipe(gulp.dest(project.paths.root))
    .pipe(through.obj(function (file, enc,  cb) {
      // https://github.com/webpack/watchpack/issues/25#issuecomment-287789288
      var now = Date.now() / 1000;
      var then = now - 10;
      fs.utimes(file.path, then, then, function (err) { if (err) throw err });
      cb(null, file);
    }));
}

export default configureEnvironment;
1 Like