@comet/upgrade 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const ts_morph_1 = require("ts-morph");
13
+ /**
14
+ * Adds the `formatError` property to `GraphQLModule` options to hide `GraphQL` field suggestions for non dev environments.
15
+ */
16
+ function hideGraphqlFieldSuggestions() {
17
+ var _a;
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
20
+ const sourceFile = project.getSourceFile("api/src/app.module.ts");
21
+ if (!sourceFile)
22
+ throw new Error("app.module.ts not found");
23
+ // Add the required import statements
24
+ sourceFile.addImportDeclaration({
25
+ namedImports: ["ValidationError"],
26
+ moduleSpecifier: "apollo-server-express",
27
+ });
28
+ sourceFile.addImportDeclaration({
29
+ namedImports: ["ApolloDriverConfig"],
30
+ moduleSpecifier: "@nestjs/apollo",
31
+ });
32
+ // Get the forRoot method within AppModule
33
+ const forRootMethod = sourceFile.getClassOrThrow("AppModule").getMethodOrThrow("forRoot");
34
+ // Get the GraphQLModule.forRootAsync call within the forRoot method
35
+ const graphqlForRootCall = (_a = forRootMethod
36
+ .getBody()) === null || _a === void 0 ? void 0 : _a.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression).find((call) => call.getText().includes("GraphQLModule.forRootAsync"));
37
+ if (!graphqlForRootCall)
38
+ throw new Error("GraphQLModule.forRootAsync call not found within forRoot method.");
39
+ // Add the generic type ApolloDriverConfig to the GraphQLModule.forRootAsync call
40
+ if (!forRootMethod.getFullText().includes("GraphQLModule.forRootAsync<ApolloDriverConfig>")) {
41
+ const expression = graphqlForRootCall.getExpression();
42
+ if (!expression)
43
+ throw new Error("Expression not found within GraphQLModule.forRootAsync call.");
44
+ const expressionText = expression.getText();
45
+ expression.replaceWithText(expressionText.replace(`${expressionText}`, `${expressionText}<ApolloDriverConfig>`));
46
+ }
47
+ const formatErrorImplText = `
48
+ (error) => {
49
+ if (process.env.NODE_ENV !== "development") {
50
+ if (error instanceof ValidationError) {
51
+ return new ValidationError("Invalid request.");
52
+ }
53
+ }
54
+ return error;
55
+ }`;
56
+ // return if property formatError already exists
57
+ if (graphqlForRootCall.getArguments().find((arg) => arg.getText().includes("formatError"))) {
58
+ throw new Error(`formatError property already exists in GraphQLModule.forRootAsync options. To be sure GraphQl field suggestions are disabled for non dev environments, please check if the implementation already contains: ${formatErrorImplText}`);
59
+ }
60
+ // Find the useFactory function within the GraphQLModule.forRootAsync call
61
+ const useFactoryFunction = graphqlForRootCall.getFirstDescendantByKindOrThrow(ts_morph_1.SyntaxKind.ArrowFunction, "useFactory function not found within GraphQLModule.forRootAsync call.");
62
+ if (!useFactoryFunction.getParent().getText().includes("useFactory"))
63
+ throw new Error("useFactory function not found within GraphQLModule.forRootAsync call.");
64
+ // Find the object literal being returned by the useFactory function
65
+ const returnObjectLiteral = useFactoryFunction.getFirstDescendantByKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression, "Object literal not found within useFactory function.");
66
+ // Add your formatError to GraphQLModule.forRootAsync options
67
+ returnObjectLiteral.addPropertyAssignment({
68
+ name: "formatError",
69
+ initializer: ({ write }) => write(formatErrorImplText),
70
+ });
71
+ sourceFile.saveSync();
72
+ });
73
+ }
74
+ exports.default = hideGraphqlFieldSuggestions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comet/upgrade",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Upgrade scripts for Comet DXP",
5
5
  "homepage": "https://github.com/vivid-planet/comet-upgrade#readme",
6
6
  "bugs": {
@@ -31,7 +31,8 @@
31
31
  "eslint": "^8.56.0",
32
32
  "glob": "^10.3.10",
33
33
  "prettier": "^2.8.1",
34
- "semver": "^7.3.8"
34
+ "semver": "^7.3.8",
35
+ "ts-morph": "^22.0.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@comet/eslint-config": "^3.2.1",