1 |
|
2 | const {join, resolve} = require('path');
|
3 | const process = require('process');
|
4 | const {safeLoad} = require('js-yaml');
|
5 | const {readFileSync} = require('fs');
|
6 |
|
7 | const SetEnv = (node_env, rails_env)=>{
|
8 | let env = node_env || rails_env;
|
9 | switch (env){
|
10 | case undefined:
|
11 | return 'development';
|
12 | case 'staging':
|
13 | return 'production';
|
14 | default:
|
15 | return env;
|
16 | }
|
17 | };
|
18 |
|
19 | const {NODE_ENV, RAILS_ENV} = process.env;
|
20 | process.env.NODE_ENV = SetEnv(NODE_ENV, RAILS_ENV);
|
21 | const env = process.env;
|
22 |
|
23 | const configPath = resolve('config', 'webpack');
|
24 | const loadersDir = join(__dirname, 'loaders');
|
25 | const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'));
|
26 | const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'));
|
27 |
|
28 | let amazonS3;
|
29 | try {
|
30 | amazonS3 = safeLoad(readFileSync(join(configPath, 'amazons3.yml'), 'utf8'));
|
31 | } catch (e){
|
32 | amazonS3 = false;
|
33 | }
|
34 |
|
35 | const date = new Date();
|
36 | const timeStamp = date.getTime();
|
37 | const basePath = `${paths.entry}/${timeStamp}`;
|
38 |
|
39 | const publicPathCheck = (node_env, ds, aws)=>{
|
40 | if (node_env === 'development' && ds.enabled){
|
41 | return `http://${devServer.host}:${devServer.port}/`;
|
42 | } else if (aws){
|
43 | let cloudfront = aws[node_env].cloudfront;
|
44 | let baseDir = aws[node_env].baseDir;
|
45 | return `${cloudfront}/${baseDir}/${basePath}/`;
|
46 | }
|
47 |
|
48 | return `/${paths.entry}/`;
|
49 | };
|
50 |
|
51 | const publicPath = publicPathCheck(env.RAILS_ENV, devServer, amazonS3);
|
52 |
|
53 | module.exports = {
|
54 | amazonS3
|
55 | , basePath
|
56 | , devServer
|
57 | , env
|
58 | , paths
|
59 | , loadersDir
|
60 | , publicPath
|
61 | };
|