It's often useful to generate a file of constants, usually as environment variables, for your Angular apps. This Gulp plugin will allow you to provide an object of properties and will generate an Angular module of constants.
npm install gulp-ts-config
It's pretty simple:
gulpTsConfig(moduleName)
We start with our task. Our source file is a JSON file containing our configuration. We will pipe this through gulpTsConfig
and out will come an angular module of constants.
var gulp = require('gulp');
var gulpTsConfig = require('gulp-ts-config');
gulp.task('test', function () {
gulp.src('appsettings.json')
.pipe(gulpTsConfig('AppSettings'))
.pipe(gulp.dest('.'))
});
Assume that appsettings.json
contains:
{
"ApiEndpoint": "https://localhost:5000/api"
}
Running gulp test
will take appsettings.json
and produce appsettings.ts
with the following content:
export class AppSettings {
public static get ApiEndpoint(): string {
return "https://localhost:5000/api";
}
}
We now can include this configuration module in our main app and access the constants
import { AppSettings } from 'appsettings';
Currently there are a few configurable options to control the output of your configuration file:
- options.environment
- options.constants
- options.createModule
- options.type
- options.wrap
- options.parser
- options.pretty
This is copy from Atticus White @robinpowered
Thanks