|
1 | 1 | const fs = require('fs');
|
2 | 2 | const path = require('path');
|
| 3 | +const { constantCase } = require('constant-case'); |
3 | 4 | const packageConfig = require('../package.json');
|
4 | 5 |
|
5 | 6 | /* eslint-disable no-console */
|
@@ -33,17 +34,28 @@ module.exports = function generateConfig(configOverrides) {
|
33 | 34 |
|
34 | 35 | // The GraphQL endpoint is an example of making a _computed_ config setting
|
35 | 36 | // based on other config settings.
|
36 |
| - addGraphQLConfig(config); |
| 37 | + const computedConfig = {}; |
| 38 | + computedConfig.graphQLEndpoint = '`${config.sitecoreApiHost}${config.graphQLEndpointPath}`'; |
37 | 39 |
|
38 |
| - const configText = `/* eslint-disable */ |
| 40 | + let configText = `/* eslint-disable */ |
39 | 41 | // Do not edit this file, it is auto-generated at build time!
|
40 |
| -// See scripts/bootstrap.js to modify the generation of this file. |
41 |
| -module.exports = ${JSON.stringify(config, null, 2)};`; |
| 42 | +// See scripts/bootstrap.ts to modify the generation of this file. |
| 43 | +const config = {};\n`; |
| 44 | + |
| 45 | + // Set base configuration values, allowing override with environment variables |
| 46 | + Object.keys(config).forEach((prop) => { |
| 47 | + configText += `config.${prop} = process.env.${constantCase(prop)} || "${config[prop]}",\n`; |
| 48 | + }); |
| 49 | + // Set computed values, allowing override with environment variables |
| 50 | + Object.keys(computedConfig).forEach((prop) => { |
| 51 | + configText += `config.${prop} = process.env.${constantCase(prop)} || ${ |
| 52 | + computedConfig[prop] |
| 53 | + };\n`; |
| 54 | + }); |
| 55 | + configText += 'module.exports = config;'; |
42 | 56 |
|
43 | 57 | const configPath = path.resolve('src/temp/config.js');
|
44 |
| - |
45 | 58 | console.log(`Writing runtime config to ${configPath}`);
|
46 |
| - |
47 | 59 | fs.writeFileSync(configPath, configText, { encoding: 'utf8' });
|
48 | 60 | };
|
49 | 61 |
|
@@ -75,18 +87,3 @@ function transformPackageConfig() {
|
75 | 87 | graphQLEndpointPath: packageConfig.config.graphQLEndpointPath || null,
|
76 | 88 | };
|
77 | 89 | }
|
78 |
| - |
79 |
| -/** |
80 |
| - * Adds the GraphQL endpoint URL to the config object, and ensures that components needed to calculate it are valid |
81 |
| - */ |
82 |
| -function addGraphQLConfig(baseConfig) { |
83 |
| - if (!baseConfig.graphQLEndpointPath || typeof baseConfig.sitecoreApiHost === 'undefined') { |
84 |
| - console.error( |
85 |
| - 'The `graphQLEndpointPath` and/or `layoutServiceHost` configurations were not defined. You may need to run `jss setup`.' |
86 |
| - ); |
87 |
| - process.exit(1); |
88 |
| - } |
89 |
| - |
90 |
| - // eslint-disable-next-line no-param-reassign |
91 |
| - baseConfig.graphQLEndpoint = `${baseConfig.sitecoreApiHost}${baseConfig.graphQLEndpointPath}`; |
92 |
| -} |
0 commit comments