1
0

index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const path = require('path');
  2. const debug = require('debug')('app:config');
  3. let config = require('./local');
  4. const NODE_ENV = process.env.NODE_ENV || process.env.ENV || 'development';
  5. debug('Creating default configuration.');
  6. const argv = JSON.parse(process.env.npm_config_argv);
  7. const project = argv.remain.length > 0 ? argv.remain[0] : 'admin';
  8. debug('Current Project -> ' + project);
  9. let projectConfig = require(`../project/${project}/local`);
  10. config = Object.assign(
  11. {
  12. env: NODE_ENV,
  13. path_base: path.resolve(__dirname, '..'),
  14. dir_client: 'src',
  15. dir_dist: 'dist',
  16. dir_lib: 'lib',
  17. dir_components: 'components',
  18. basename: '/',
  19. dir_project: 'project/' + project,
  20. api_path: `/api`,
  21. },
  22. config[NODE_ENV],
  23. projectConfig[NODE_ENV],
  24. );
  25. function base() {
  26. const args = [config.path_base].concat([].slice.call(arguments));
  27. return path.resolve.apply(path, args);
  28. }
  29. config.utils_paths = {
  30. base,
  31. client: base.bind(null, config.dir_client),
  32. components: base.bind(null, config.dir_components),
  33. dist: base.bind(null, config.dir_dist),
  34. lib: base.bind(null, config.dir_lib),
  35. project: base.bind(null, config.dir_project),
  36. };
  37. config.globals = {
  38. 'process.env': {
  39. NODE_ENV: JSON.stringify(config.env),
  40. },
  41. NODE_ENV: config.env,
  42. __DEV__: config.env === 'development',
  43. __PROD__: config.env === 'production',
  44. __TEST__: config.env === 'test',
  45. __DEBUG__: config.env === 'development' || config.env === 'test',
  46. __API_PATH__: `\'${config.api_path}\'`,
  47. __BASE_NAME__: `\'${config.basename}\'`,
  48. __PcUrl__: `\'${config.PcUrl}\'`,
  49. __WechatPcAppId__: `\'${config.WechatPcAppId}\'`,
  50. __H5Url__: `\'${config.H5Url}\'`,
  51. __WechatH5AppId__: `\'${config.WechatH5AppId}\'`,
  52. };
  53. debug(`Looking for environment overrides for NODE_ENV '${config.env}'.`);
  54. const environments = require('./environments');
  55. const overrides = environments[config.env];
  56. if (overrides) {
  57. debug('Found overrides, applying to default configuration.');
  58. Object.assign(config, overrides());
  59. } else {
  60. debug('No environment overrides found, defaults will be used.');
  61. }
  62. module.exports = config;