Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): prevent duplicate stream definitions #408

Merged
merged 7 commits into from
Oct 17, 2023
7 changes: 6 additions & 1 deletion packages/pages/src/generate/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ 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<void> => {
const projectStructure = await ProjectStructure.init({ scope });
const templateFilepaths = getTemplateFilepaths(
projectStructure.getTemplatePaths()
);

await createTemplatesJson(templateFilepaths, projectStructure, "FEATURES");
try {
await createTemplatesJson(templateFilepaths, projectStructure, "FEATURES");
} catch (error) {
logErrorAndExit(error);
}
};

export const featureCommand = (program: Command) => {
Expand Down
20 changes: 19 additions & 1 deletion packages/pages/src/generate/templates/createTemplatesJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs-extra";
import isEqual from "lodash/isEqual.js";
import path from "path";
import {
FeaturesConfig,
Expand Down Expand Up @@ -87,7 +88,7 @@ export const getTemplatesConfig = (
features.push(featureConfig);
const streamConfig = convertTemplateConfigToStreamConfig(module.config);
if (streamConfig) {
streams.push(streamConfig);
pushStreamConfigIfValid(streams, streamConfig);
}
}

Expand All @@ -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}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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" });
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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" });
Expand All @@ -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" });
Expand All @@ -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);
}
};
};
Expand Down
Binary file modified packages/pages/yext-pages-1.0.0-rc.4.tgz
Binary file not shown.