|
1 | 1 | import fs from 'fs';
|
2 | 2 | import path from 'path';
|
3 | 3 | import { constantCase } from 'constant-case';
|
4 |
| -import packageConfig from '../package.json'; |
| 4 | +import { JssConfig, jssConfigFactory } from './config'; |
5 | 5 |
|
6 |
| -/* eslint-disable no-console */ |
| 6 | +/* |
| 7 | + CONFIG GENERATION |
| 8 | + Generates the /src/temp/config.js file which contains runtime configuration |
| 9 | + that the app can import and use. |
| 10 | +*/ |
7 | 11 |
|
8 |
| -/** |
9 |
| - * Generate config |
10 |
| - * The object returned from this function will be made available by importing src/temp/config.js. |
11 |
| - * This is executed prior to the build running, so it's a way to inject environment or build config-specific |
12 |
| - * settings as variables into the JSS app. |
13 |
| - * NOTE! Any configs returned here will be written into the client-side JS bundle. DO NOT PUT SECRETS HERE. |
14 |
| - * @param {object} configOverrides Keys in this object will override any equivalent global config keys. |
15 |
| - */ |
16 |
| -export function generateConfig(configOverrides?: { [key: string]: string }): void { |
17 |
| - const defaultConfig = { |
18 |
| - sitecoreApiKey: 'no-api-key-set', |
19 |
| - sitecoreApiHost: '', |
20 |
| - jssAppName: 'Unknown', |
21 |
| - }; |
| 12 | +const defaultConfig: JssConfig = { |
| 13 | + sitecoreApiKey: 'no-api-key-set', |
| 14 | + sitecoreApiHost: '', |
| 15 | + jssAppName: 'Unknown', |
| 16 | + graphQLEndpointPath: '', |
| 17 | + defaultLanguage: 'en', |
| 18 | +}; |
22 | 19 |
|
23 |
| - // require + combine config sources |
24 |
| - const scjssConfig = transformScJssConfig(); |
25 |
| - const packageJson = transformPackageConfig(); |
| 20 | +generateConfig(defaultConfig); |
26 | 21 |
|
27 |
| - // Object.assign merges the objects in order, so config overrides are performed as: |
28 |
| - // default config <-- scjssconfig.json <-- package.json <-- configOverrides |
29 |
| - // Optional: add any other dynamic config source (e.g. environment-specific config files). |
30 |
| - const config = Object.assign(defaultConfig, scjssConfig, packageJson, configOverrides); |
31 |
| - |
32 |
| - // The GraphQL endpoint is an example of making a _computed_ config setting |
33 |
| - // based on other config settings. |
34 |
| - const computedConfig: { [key: string]: string } = {}; |
35 |
| - computedConfig.graphQLEndpoint = '`${config.sitecoreApiHost}${config.graphQLEndpointPath}`'; |
| 22 | +/** |
| 23 | + * Generates the JSS config based on config plugins (under ./config/plugins) |
| 24 | + * and then writes the config to disk. |
| 25 | + * @param {JssConfig} defaultConfig Default configuration. |
| 26 | + */ |
| 27 | +function generateConfig(defaultConfig: JssConfig): void { |
| 28 | + jssConfigFactory |
| 29 | + .create(defaultConfig) |
| 30 | + .then((config) => { |
| 31 | + writeConfig(config); |
| 32 | + }) |
| 33 | + .catch((e) => { |
| 34 | + console.error('Error generating config'); |
| 35 | + console.error(e); |
| 36 | + process.exit(1); |
| 37 | + }); |
| 38 | +} |
36 | 39 |
|
| 40 | +/** |
| 41 | + * Writes the config object to disk with support for environment variables. |
| 42 | + * @param {JssConfig} config JSS configuration to write. |
| 43 | + */ |
| 44 | +function writeConfig(config: JssConfig): void { |
37 | 45 | let configText = `/* eslint-disable */
|
38 | 46 | // Do not edit this file, it is auto-generated at build time!
|
39 | 47 | // See scripts/bootstrap.ts to modify the generation of this file.
|
40 | 48 | const config = {};\n`;
|
41 | 49 |
|
42 |
| - // Set base configuration values, allowing override with environment variables |
| 50 | + // Set configuration values, allowing override with environment variables |
43 | 51 | Object.keys(config).forEach((prop) => {
|
44 |
| - configText += `config.${prop} = process.env.${constantCase(prop)} || "${config[prop]}",\n`; |
45 |
| - }); |
46 |
| - // Set computed values, allowing override with environment variables |
47 |
| - Object.keys(computedConfig).forEach((prop) => { |
48 |
| - configText += `config.${prop} = process.env.${constantCase(prop)} || ${ |
49 |
| - computedConfig[prop] |
50 |
| - };\n`; |
| 52 | + configText += `config.${prop} = process.env.${constantCase(prop)} || '${config[prop]}',\n`; |
51 | 53 | });
|
52 | 54 | configText += `module.exports = config;`;
|
53 | 55 |
|
54 | 56 | const configPath = path.resolve('src/temp/config.js');
|
55 | 57 | console.log(`Writing runtime config to ${configPath}`);
|
56 | 58 | fs.writeFileSync(configPath, configText, { encoding: 'utf8' });
|
57 | 59 | }
|
58 |
| - |
59 |
| -function transformScJssConfig() { |
60 |
| - // scjssconfig.json may not exist if you've never run `jss setup` (development) |
61 |
| - // or are depending on environment variables instead (production). |
62 |
| - let config; |
63 |
| - try { |
64 |
| - // eslint-disable-next-line global-require |
65 |
| - config = require('../scjssconfig.json'); |
66 |
| - } catch (e) { |
67 |
| - return {}; |
68 |
| - } |
69 |
| - |
70 |
| - if (!config) return {}; |
71 |
| - |
72 |
| - return { |
73 |
| - sitecoreApiKey: config.sitecore.apiKey, |
74 |
| - sitecoreApiHost: config.sitecore.layoutServiceHost, |
75 |
| - }; |
76 |
| -} |
77 |
| - |
78 |
| -function transformPackageConfig() { |
79 |
| - if (!packageConfig.config) return {}; |
80 |
| - |
81 |
| - return { |
82 |
| - jssAppName: packageConfig.config.appName, |
83 |
| - graphQLEndpointPath: packageConfig.config.graphQLEndpointPath, |
84 |
| - defaultLanguage: packageConfig.config.language || 'en', |
85 |
| - }; |
86 |
| -} |
0 commit comments