-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmix-env-file.js
34 lines (30 loc) · 1.04 KB
/
mix-env-file.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
let mix = require('laravel-mix');
let fs = require('fs');
let dotenv = require('dotenv');
let path = require('path');
mix.extend(
'env',
new class {
register(env) {
if (env) {
const file = path.resolve(env);
let envConfig;
try {
envConfig = dotenv.parse(fs.readFileSync(file));
} catch (error) {
console.error('Unable to parse env file at ' + file + '\r\nPlease make sure this file exists and is formatted correctly.');
process.exit();
}
let reference = /\$\{([^\}]+)\}/;
for (let k in envConfig) {
let ref = envConfig[k].match(reference);
if (ref && ref[1] && envConfig[ref[1]]) {
process.env[k] = envConfig[ref[1]];
} else {
process.env[k] = envConfig[k];
}
}
}
}
}()
);