diff --git a/packages/pages/src/generate/features/features.ts b/packages/pages/src/generate/features/features.ts index f563fa5cc..453f1e771 100644 --- a/packages/pages/src/generate/features/features.ts +++ b/packages/pages/src/generate/features/features.ts @@ -2,6 +2,7 @@ import { ProjectStructure } from "../../common/src/project/structure.js"; import { getTemplateFilepaths } from "../../common/src/template/internal/getTemplateFilepaths.js"; import { Command } from "commander"; import { createTemplatesJson } from "../templates/createTemplatesJson.js"; +import { logErrorAndExit } from "../../util/logError.js"; const handler = async ({ scope }: { scope: string }): Promise => { const projectStructure = await ProjectStructure.init({ scope }); @@ -9,7 +10,11 @@ const handler = async ({ scope }: { scope: string }): Promise => { projectStructure.getTemplatePaths() ); - await createTemplatesJson(templateFilepaths, projectStructure, "FEATURES"); + try { + await createTemplatesJson(templateFilepaths, projectStructure, "FEATURES"); + } catch (error) { + logErrorAndExit(error); + } }; export const featureCommand = (program: Command) => { diff --git a/packages/pages/src/generate/templates/createTemplatesJson.ts b/packages/pages/src/generate/templates/createTemplatesJson.ts index de5c3c125..7d0109eee 100644 --- a/packages/pages/src/generate/templates/createTemplatesJson.ts +++ b/packages/pages/src/generate/templates/createTemplatesJson.ts @@ -1,4 +1,5 @@ import fs from "fs-extra"; +import isEqual from "lodash/isEqual.js"; import path from "path"; import { FeaturesConfig, @@ -87,7 +88,7 @@ export const getTemplatesConfig = ( features.push(featureConfig); const streamConfig = convertTemplateConfigToStreamConfig(module.config); if (streamConfig) { - streams.push(streamConfig); + pushStreamConfigIfValid(streams, streamConfig); } } @@ -114,3 +115,20 @@ export const mergeFeatureJson = ( streams, }; }; + +export const pushStreamConfigIfValid = ( + streams: StreamConfig[], + streamConfig: StreamConfig +): void => { + const matchingStreamConfig = streams.find( + (stream) => stream.$id === streamConfig.$id + ); + if (!matchingStreamConfig) { + streams.push(streamConfig); + return; + } + if (isEqual(matchingStreamConfig, streamConfig)) { + return; + } + throw `Conflicting configurations found for stream ID: ${streamConfig.$id}`; +}; diff --git a/packages/pages/src/generate/templates/createTemplatesJsonFromModule.ts b/packages/pages/src/generate/templates/createTemplatesJsonFromModule.ts index 5f5673d16..47f1ebae2 100644 --- a/packages/pages/src/generate/templates/createTemplatesJsonFromModule.ts +++ b/packages/pages/src/generate/templates/createTemplatesJsonFromModule.ts @@ -8,6 +8,10 @@ import { import { ProjectStructure } from "../../common/src/project/structure.js"; import { StreamConfig } from "../../common/src/feature/stream.js"; import { TemplateModuleCollection } from "../../common/src/template/loader/loader.js"; +import { + mergeFeatureJson, + pushStreamConfigIfValid, +} from "./createTemplatesJson.js"; // TODO: rename functions in this file once there is a migration flow and checks for it // TODO: mergeFeatureJson will no longer be necessary @@ -22,7 +26,7 @@ export const getFeaturesConfig = ( const featureConfig = convertTemplateConfigToFeatureConfig(module.config); features.push(featureConfig); module.config.stream && - streams.push({ + pushStreamConfigIfValid(streams, { ...module.config.stream, source: "knowledgeGraph", destination: "pages", @@ -59,24 +63,3 @@ export const createFeaturesJson = ( JSON.stringify(featuresJson, null, " ") ); }; - -/** - * Overwrites the "features" and "streams" fields in the feature.json while keeping other fields - * if the feature.json already exists. - */ -export const mergeFeatureJson = ( - featurePath: string, - features: FeatureConfig[], - streams: any -): FeaturesConfig => { - let originalFeaturesJson = {} as any; - if (fs.existsSync(featurePath)) { - originalFeaturesJson = JSON.parse(fs.readFileSync(featurePath).toString()); - } - - return { - ...originalFeaturesJson, - features, - streams, - }; -}; diff --git a/packages/pages/src/vite-plugin/build/closeBundle/closeBundle.ts b/packages/pages/src/vite-plugin/build/closeBundle/closeBundle.ts index e0d4e6460..080823733 100644 --- a/packages/pages/src/vite-plugin/build/closeBundle/closeBundle.ts +++ b/packages/pages/src/vite-plugin/build/closeBundle/closeBundle.ts @@ -22,6 +22,7 @@ import { TemplateModuleCollection, loadTemplateModules, } from "../../../common/src/template/loader/loader.js"; +import { logErrorAndExit } from "../../../util/logError.js"; export default (projectStructure: ProjectStructure) => { return async () => { @@ -97,7 +98,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Validated functions"); } catch (e) { finisher.fail("One or more functions failed validation"); - throw e; + logErrorAndExit(e); } finisher = logger.timedLog({ startLog: "Writing functionMetadata.json" }); @@ -106,7 +107,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Successfully wrote functionMetadata.json"); } catch (e: any) { finisher.fail("Failed to write functionMetadata.json"); - throw new Error(e); + logErrorAndExit(e); } } @@ -117,7 +118,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Successfully bundled serverless functions"); } catch (e: any) { finisher.fail("Failed to bundle serverless functions"); - throw new Error(e); + logErrorAndExit(e); } } @@ -127,7 +128,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Successfully wrote features.json"); } catch (e: any) { finisher.fail("Failed to write features.json"); - throw new Error(e); + logErrorAndExit(e); } finisher = logger.timedLog({ startLog: "Writing manifest.json" }); @@ -136,7 +137,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Successfully wrote manifest.json"); } catch (e: any) { finisher.fail("Failed to write manifest.json"); - throw new Error(e); + logErrorAndExit(e); } finisher = logger.timedLog({ startLog: "Updating ci.json" }); @@ -155,7 +156,7 @@ export default (projectStructure: ProjectStructure) => { finisher.succeed("Successfully updated ci.json"); } catch (e: any) { finisher.fail("Failed to update ci.json"); - throw new Error(e); + logErrorAndExit(e); } }; }; diff --git a/packages/pages/yext-pages-1.0.0-rc.4.tgz b/packages/pages/yext-pages-1.0.0-rc.4.tgz index 1eaa246df..872f7a080 100644 Binary files a/packages/pages/yext-pages-1.0.0-rc.4.tgz and b/packages/pages/yext-pages-1.0.0-rc.4.tgz differ