Skip to content

Instantly share code, notes, and snippets.

@vespertilian
Last active August 5, 2018 14:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vespertilian/7c06b97f14fc6141d39d to your computer and use it in GitHub Desktop.
Save vespertilian/7c06b97f14fc6141d39d to your computer and use it in GitHub Desktop.
Sample gulp config task for typescript using gulp ng-constant, creates a Angular constant typescript file
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
gulp.task('config', function () {
var config = require('../config.json'),
envConfig = config['development'];
[process.env.NODE_ENV === 'production' ? 'production' : 'development']
if(process.env.NODE_ENV === 'production'){
console.log('loading production config');
envConfig = config['production'];
}
else{
console.log('loading dev config');
}
return $.ngConstant({
constants: envConfig,
templatePath: 'constant.tpl.ejs',
stream: true
})
.pipe($.rename('config.ts'))
.pipe(gulp.dest('src/app/'));
});
{
"development": {
"apiBase": "http://localhost:3000/api"
},
"production": {
"apiBase": "https:/someurl.net/api"
}
}
export interface IConstants {<% constants.forEach(function(constant) {%>
<%= constant.name.replace(/"/g, "'") %>: string;<% }) %>
}
export class Constants {
static get Default(): any {
return {<% constants.forEach(function(constant) {%>
<%= constant.name.replace(/"/g, "'") %>: <%= constant.value.replace(/"/g, "'")%>,<% }) %>
};
}
}
import { Constants } from './config';
module appName {
'use strict';
angular.module('appName', [
'ngAnimate',
'ngCookies',
'ngTouch',
'ngSanitize',
'ngMessages',
'ngAria',
'ui.router',
'ngMaterial',
'ngResource',
'formly'
])
.constant('constants', Constants.Default)
}
import { Constants } from './config';
export class MainController {
public apiBase: string;
/* @ngInject */
constructor (constants: Constants) {
this.apiBase = constants.apiBase
}
}
@JonElster
Copy link

Awesome

@piraveen
Copy link

Great gist, thanks a lot 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment