Skip to content

Commit

Permalink
Switch to logErrorAndExit
Browse files Browse the repository at this point in the history
  • Loading branch information
briantstephan committed Oct 17, 2023
1 parent d855b69 commit adc360d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
3 changes: 2 additions & 1 deletion packages/pages/src/generate/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const projectStructure = await ProjectStructure.init({ scope });
Expand All @@ -12,7 +13,7 @@ const handler = async ({ scope }: { scope: string }): Promise<void> => {
try {
await createTemplatesJson(templateFilepaths, projectStructure, "FEATURES");
} catch (error) {
console.error(error);
logErrorAndExit(error);
}
};

Expand Down
32 changes: 19 additions & 13 deletions packages/pages/src/generate/templates/createTemplatesJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,9 @@ export const getTemplatesConfig = (
const featureConfig = convertTemplateConfigToFeatureConfig(module.config);
features.push(featureConfig);
const streamConfig = convertTemplateConfigToStreamConfig(module.config);
if (!streamConfig) {
continue;
if (streamConfig) {
pushStreamConfigIfValid(streams, streamConfig);
}
const matchingStreamConfig = streams.find(
(stream) => stream.$id === streamConfig.$id
);
if (!matchingStreamConfig) {
streams.push(streamConfig);
continue;
}
if (isEqual(matchingStreamConfig, streamConfig)) {
continue;
}
throw `Conflicting configurations found for stream ID: ${streamConfig.$id}`;
}

return { features, streams };
Expand All @@ -126,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,
};
};
13 changes: 7 additions & 6 deletions packages/pages/src/vite-plugin/build/closeBundle/closeBundle.ts
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.

0 comments on commit adc360d

Please sign in to comment.