-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-env-config.js
40 lines (30 loc) · 1.15 KB
/
gulp-env-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var path = require('path'),
fs = require('fs'),
ip = require('ip'),
yargs = require('yargs');
function getEnv() {
let dev = (yargs.argv.dev || process.env.npm_config_dev) ? 'dev' : undefined;
let production = (yargs.argv.production || process.env.npm_config_production) ? 'production' : undefined;
let local = (!dev && !production) ? 'local' : undefined;
return local || dev || production;
}
function replaceEnv(env, file) {
let envPattern = /ENV(\s)?=(\s)?('|")([a-z]+|\s)?('|")(;)?/i;
return file.replace(envPattern, `ENV = '${env}';`);
}
function replaceIP(file) {
let ipPattern = /IP(\s)?=(\s)?('|")([0-9|\.]+)?('|")(;)?/i;
return file.replace(ipPattern, `IP = '${ip.address()}';`);
}
function execConfig(filePath, file) {
let env = getEnv();
file = replaceEnv(env, file);
if (env === 'local') file = replaceIP(file);
fs.writeFileSync(filePath, file, { encoding: 'utf8' });
console.log(`\t > Set up "${env}" environment, at IP address ${ip.address()}`);
}
module.exports = function () {
let filePath = path.resolve(__dirname, 'src/app/core/config.service.ts');
let file = fs.readFileSync(filePath, 'utf8');
execConfig(filePath, file);
};