Skip to content

Instantly share code, notes, and snippets.

@piraveen
Forked from vespertilian/config.js
Last active May 27, 2016 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piraveen/52b7aea36f06f02b17064969fadca00f to your computer and use it in GitHub Desktop.
Save piraveen/52b7aea36f06f02b17064969fadca00f 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 default {<% constants.forEach(function(constant) {%>
<%= constant.name.replace(/"/g, "'") %>: <%= constant.value.replace(/"/g, "'")%>,<% }) %>
};
export var Constants = {<% 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)
}
import { Constants } from './config';
export class MainController {
public apiBase: string;
/* @ngInject */
constructor (constants: Constants) {
this.apiBase = constants.apiBase
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment