|
1 | | -import { LogType, fecLogger } from '@redhat-cloud-services/frontend-components-config-utilities'; |
| 1 | +import inquirer from 'inquirer'; |
| 2 | +import { FrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/feo-types'; |
| 3 | +import { fecLogger, LogType } from '@redhat-cloud-services/frontend-components-config-utilities'; |
| 4 | +import { hasFEOFeaturesEnabled, readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check'; |
2 | 5 |
|
3 | 6 | const { resolve } = require('path'); |
4 | 7 | const { statSync } = require('fs'); |
@@ -45,5 +48,63 @@ export function getWebpackConfigPath(path: string, cwd: string) { |
45 | 48 | } |
46 | 49 | } |
47 | 50 |
|
| 51 | +export async function setEnv(cwd: string) { |
| 52 | + return inquirer |
| 53 | + .prompt([ |
| 54 | + { |
| 55 | + type: 'list', |
| 56 | + name: 'clouddotEnv', |
| 57 | + message: 'Which platform environment you want to use?', |
| 58 | + choices: ['stage', 'prod', 'dev', 'ephemeral'], |
| 59 | + }, |
| 60 | + ]) |
| 61 | + .then(async (answers) => { |
| 62 | + const { clouddotEnv } = answers; |
| 63 | + |
| 64 | + if (clouddotEnv === 'ephemeral') { |
| 65 | + const answer = await inquirer.prompt([ |
| 66 | + { |
| 67 | + type: 'input', |
| 68 | + name: 'clouddotEnv', |
| 69 | + message: 'Please provide the gateway route of your ephemeral environment:', |
| 70 | + }, |
| 71 | + ]); |
| 72 | + process.env.EPHEMERAL_TARGET = answer.clouddotEnv; |
| 73 | + } |
| 74 | + process.env.CLOUDOT_ENV = clouddotEnv ? clouddotEnv : 'stage'; |
| 75 | + process.env.FEC_ROOT_DIR = cwd; |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +export function getCdnPath(fecConfig: any, webpackConfig: any, cwd: string): string { |
| 80 | + let cdnPath: string; |
| 81 | + const { insights } = require(`${cwd}/package.json`); |
| 82 | + const frontendCRDPath = fecConfig.frontendCRDPath ?? `${cwd}/deploy/frontend.yaml`; |
| 83 | + const frontendCRDRef: { current?: FrontendCRD } = { current: undefined }; |
| 84 | + let FEOFeaturesEnabled = false; |
| 85 | + |
| 86 | + try { |
| 87 | + frontendCRDRef.current = readFrontendCRD(frontendCRDPath); |
| 88 | + FEOFeaturesEnabled = hasFEOFeaturesEnabled(frontendCRDRef.current); |
| 89 | + } catch (e) { |
| 90 | + fecLogger( |
| 91 | + LogType.warn, |
| 92 | + `FEO features are not enabled. Unable to find frontend CRD file at ${frontendCRDPath}. If you want FEO features for local development, make sure to have a "deploy/frontend.yaml" file in your project or specify its location via "frontendCRDPath" attribute.`, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + if (FEOFeaturesEnabled && fecConfig.publicPath === 'auto' && frontendCRDRef.current) { |
| 97 | + cdnPath = `${frontendCRDRef.current?.objects[0]?.spec.frontend.paths[0]}/`.replace(/\/\//, '/'); |
| 98 | + } else if (fecConfig.publicPath === 'auto') { |
| 99 | + cdnPath = `/${fecConfig.deployment || 'apps'}/${insights.appname}/`; |
| 100 | + } else { |
| 101 | + cdnPath = webpackConfig.output.publicPath; |
| 102 | + } |
| 103 | + |
| 104 | + return cdnPath ?? ''; |
| 105 | +} |
| 106 | + |
48 | 107 | module.exports.validateFECConfig = validateFECConfig; |
49 | 108 | module.exports.getWebpackConfigPath = getWebpackConfigPath; |
| 109 | +module.exports.setEnv = setEnv; |
| 110 | +module.exports.getCdnPath = getCdnPath; |
0 commit comments