@trackunit/react-graphql-tools 0.0.59 → 0.0.60

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [0.0.60](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.59...react-graphql-tools/0.0.60) (2024-01-04)
6
+
7
+ ### Dependency Updates
8
+
9
+ * `test-setup` updated to version `0.0.59`
5
10
  ## [0.0.59](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.58...react-graphql-tools/0.0.59) (2023-12-14)
6
11
 
7
12
  ### Dependency Updates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-graphql-tools",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "main": "./src/index.js",
5
5
  "executors": "./executors.json",
6
6
  "repository": "https://github.com/Trackunit/manager",
@@ -6,111 +6,46 @@ const tslib_1 = require("tslib");
6
6
  const fs_1 = require("fs");
7
7
  const path_1 = require("path");
8
8
  const prettier = tslib_1.__importStar(require("prettier"));
9
- const ts_morph_1 = require("ts-morph");
10
9
  const scrubMockFile_1 = require("./scrubMockFile");
11
- /**
12
- * Removes unused identifiers from the source file.
13
- *
14
- * This does more then sourceFile.fixUnusedIdentifiers(diagnostic) as it also removes unused enums and interfaces.
15
- *
16
- * @param sourceFile The source file to remove unused identifiers from.
17
- */
18
- const removeUnusedIdentifiers = (sourceFile) => {
19
- const diagnostics = sourceFile.getPreEmitDiagnostics();
20
- diagnostics.forEach(diagnostic => {
21
- var _a;
22
- const messageText = diagnostic.getMessageText();
23
- if (typeof messageText === "string" && messageText.startsWith("'")) {
24
- const identifierName = (_a = messageText.match(/'([^']+)'/)) === null || _a === void 0 ? void 0 : _a[1];
25
- if (identifierName) {
26
- if (messageText.includes("is declared but never used")) {
27
- const identifier = sourceFile.getVariableDeclaration(identifierName) ||
28
- sourceFile.getInterface(identifierName) ||
29
- sourceFile.getEnum(identifierName) ||
30
- sourceFile.getClass(identifierName) ||
31
- sourceFile.getTypeAlias(identifierName);
32
- if (!identifier) {
33
- // eslint-disable-next-line no-console
34
- console.log("Could not find: ", messageText);
35
- }
36
- else {
37
- identifier.remove();
38
- }
39
- }
40
- }
41
- }
42
- });
43
- };
44
- const convertEnumToConstObject = (enumDeclaration) => {
45
- const enumName = enumDeclaration.getName();
46
- const members = enumDeclaration.getMembers();
47
- const properties = members.map(member => {
48
- const name = member.getName();
49
- const value = member.getValue();
50
- return `${name}: '${value}'`;
51
- });
52
- const lowerCaseFirstLetter = enumName[0].toLowerCase() + enumName.slice(1);
53
- const constObjectText = `export const ${lowerCaseFirstLetter} = {\n ${properties.join(",\n ")},\n} as const;\nexport type ${enumName} = typeof ${lowerCaseFirstLetter}[keyof typeof ${lowerCaseFirstLetter}];`;
54
- // Replace the EnumDeclaration with the const object text
55
- enumDeclaration.replaceWithText(constObjectText);
56
- };
10
+ const utils_1 = require("./utils");
57
11
  /**
58
12
  * Generates React hooks from your graphql files.
59
13
  */
60
14
  const scrubFileContent = (fileContent, nxRoot, isVerbose) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
61
15
  const start = new Date().getTime();
62
- const project = new ts_morph_1.Project({
63
- compilerOptions: {
64
- noUnusedLocals: true,
65
- noUnusedParameters: true,
66
- },
67
- });
68
- const generatedFile = project.createSourceFile("tmp.ts", fileContent);
69
- generatedFile.insertStatements(0, "/* eslint-disable @typescript-eslint/no-explicit-any */");
70
- generatedFile.insertStatements(1, "/* eslint-disable @typescript-eslint/array-type */");
71
- generatedFile.getTypeAliases().forEach(node => {
72
- if (!node.getName().startsWith("Incremental")) {
73
- node.setIsExported(false);
74
- }
75
- if (node.getName().startsWith("Query") ||
76
- node.getName().startsWith("Mutation") ||
77
- // Since they reference each other we remove them here.
78
- node.getName() === "Site" ||
79
- node.getName() === "Event" ||
80
- node.getName() === "Operator" ||
81
- node.getName() === "Account" ||
82
- node.getName() === "Operator" ||
83
- node.getName() === "Asset") {
84
- node.remove();
16
+ const sourceFile = (0, utils_1.createSourceFile)(fileContent);
17
+ let resultFileContent = "";
18
+ const scalarsType = sourceFile.getTypeAliasOrThrow("Scalars");
19
+ scalarsType.getEndLineNumber();
20
+ resultFileContent += fileContent.split("\n").slice(0, scalarsType.getEndLineNumber()).join("\n") + "\n";
21
+ const alreadyAdded = sourceFile
22
+ .getTypeAliases()
23
+ .filter(typeAlias => typeAlias.getStartLineNumber() < scalarsType.getEndLineNumber())
24
+ .map(typeAlias => typeAlias.getName());
25
+ const { preservedTypes, queryAndMutations } = (0, utils_1.getPreservedTypes)(sourceFile, isVerbose);
26
+ isVerbose && console.log("PreservedTypes:", preservedTypes);
27
+ sourceFile.getEnums().forEach(enumType => {
28
+ const typeName = enumType.getName();
29
+ if (preservedTypes.includes(typeName) && !alreadyAdded.includes(typeName) && !(0, utils_1.isFragment)(typeName)) {
30
+ alreadyAdded.push(typeName);
31
+ resultFileContent += "\n" + (0, utils_1.convertEnumToConstObject)(enumType) + "\n";
85
32
  }
86
33
  });
87
- generatedFile.getEnums().forEach(node => {
88
- node.setIsExported(false);
89
- });
90
- let lastWidth = 0;
91
- do {
92
- lastWidth = generatedFile.getFullWidth();
93
- removeUnusedIdentifiers(generatedFile);
94
- if (isVerbose) {
95
- console.log("Cleanup", lastWidth, generatedFile.getFullWidth());
34
+ sourceFile.getTypeAliases().forEach(typeAlias => {
35
+ const typeName = typeAlias.getName();
36
+ if (preservedTypes.includes(typeName) && !alreadyAdded.includes(typeName) && !(0, utils_1.isFragment)(typeName)) {
37
+ alreadyAdded.push(typeName);
38
+ resultFileContent += "\n" + typeAlias.getText() + "\n";
96
39
  }
97
- } while (lastWidth !== generatedFile.getFullWidth());
98
- generatedFile.getTypeAliases().forEach(node => {
99
- node.setIsExported(true);
100
- });
101
- generatedFile.getVariableStatements().forEach(variableStatement => {
102
- variableStatement.setIsExported(true);
103
- });
104
- generatedFile.getEnums().forEach(node => {
105
- node.setIsExported(true);
106
- convertEnumToConstObject(node);
107
40
  });
41
+ const resultQueryAndMutations = queryAndMutations.map(queryOrMutation => queryOrMutation.getFullText()).join("\n");
42
+ const result = resultFileContent + "\n" + resultQueryAndMutations;
108
43
  if (isVerbose) {
109
44
  const end = new Date().getTime();
110
45
  const time = end - start;
111
46
  console.log("Execution time: " + time);
112
47
  }
113
- return generatedFile.getFullText();
48
+ return result;
114
49
  });
115
50
  exports.scrubFileContent = scrubFileContent;
116
51
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"scrubFile.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/scrubFile.ts"],"names":[],"mappings":";;;;AAAA,+BAA+B;AAC/B,2BAAiD;AACjD,+BAA4B;AAC5B,2DAAqC;AACrC,uCAAgE;AAChE,mDAAuD;AAEvD;;;;;;GAMG;AACH,MAAM,uBAAuB,GAAG,CAAC,UAAsB,EAAE,EAAE;IACzD,MAAM,WAAW,GAAG,UAAU,CAAC,qBAAqB,EAAE,CAAC;IACvD,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;;QAC/B,MAAM,WAAW,GAAG,UAAU,CAAC,cAAc,EAAE,CAAC;QAEhD,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAClE,MAAM,cAAc,GAAG,MAAA,WAAW,CAAC,KAAK,CAAC,WAAW,CAAC,0CAAG,CAAC,CAAC,CAAC;YAC3D,IAAI,cAAc,EAAE;gBAClB,IAAI,WAAW,CAAC,QAAQ,CAAC,4BAA4B,CAAC,EAAE;oBACtD,MAAM,UAAU,GACd,UAAU,CAAC,sBAAsB,CAAC,cAAc,CAAC;wBACjD,UAAU,CAAC,YAAY,CAAC,cAAc,CAAC;wBACvC,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC;wBAClC,UAAU,CAAC,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IAC5C,IAAI,eAAe,EAAE;QACnB,eAAe,CAAC,MAAM,GAAG,YAAY,CAAC;QACtC,IAAI;YACF,aAAa,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,uBAAuB,EAAE,eAAe,CAAC,CAAC;SACjF;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;SACnD;KACF;IACD,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzD,IAAA,kBAAa,EAAC,SAAS,GAAG,UAAU,EAAE,aAAa,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAC5E,OAAO,SAAS,CAAC;AACnB,CAAC,CAAA,CAAC;AAjDW,QAAA,SAAS,aAiDpB","sourcesContent":["/* eslint-disable no-console */\nimport { readFileSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport * as prettier from \"prettier\";\nimport { EnumDeclaration, Project, SourceFile } from \"ts-morph\";\nimport { scrubMockFileContent } from \"./scrubMockFile\";\n\n/**\n * Removes unused identifiers from the source file.\n *\n * This does more then sourceFile.fixUnusedIdentifiers(diagnostic) as it also removes unused enums and interfaces.\n *\n * @param sourceFile The source file to remove unused identifiers from.\n */\nconst removeUnusedIdentifiers = (sourceFile: SourceFile) => {\n const diagnostics = sourceFile.getPreEmitDiagnostics();\n diagnostics.forEach(diagnostic => {\n const messageText = diagnostic.getMessageText();\n\n if (typeof messageText === \"string\" && messageText.startsWith(\"'\")) {\n const identifierName = messageText.match(/'([^']+)'/)?.[1];\n if (identifierName) {\n if (messageText.includes(\"is declared but never used\")) {\n const identifier =\n sourceFile.getVariableDeclaration(identifierName) ||\n sourceFile.getInterface(identifierName) ||\n sourceFile.getEnum(identifierName) ||\n sourceFile.getClass(identifierName) ||\n sourceFile.getTypeAlias(identifierName);\n\n if (!identifier) {\n // eslint-disable-next-line no-console\n console.log(\"Could not find: \", messageText);\n } else {\n identifier.remove();\n }\n }\n }\n }\n });\n};\n\nconst convertEnumToConstObject = (enumDeclaration: EnumDeclaration) => {\n const enumName = enumDeclaration.getName();\n const members = enumDeclaration.getMembers();\n\n const properties = members.map(member => {\n const name = member.getName();\n const value = member.getValue();\n return `${name}: '${value}'`;\n });\n\n const lowerCaseFirstLetter = enumName[0].toLowerCase() + enumName.slice(1);\n const constObjectText = `export const ${lowerCaseFirstLetter} = {\\n ${properties.join(\n \",\\n \"\n )},\\n} as const;\\nexport type ${enumName} = typeof ${lowerCaseFirstLetter}[keyof typeof ${lowerCaseFirstLetter}];`;\n\n // Replace the EnumDeclaration with the const object text\n enumDeclaration.replaceWithText(constObjectText);\n};\n\n/**\n * Generates React hooks from your graphql files.\n */\nexport const scrubFileContent = async (fileContent: string, nxRoot: string, isVerbose?: boolean) => {\n const start = new Date().getTime();\n\n const project = new Project({\n compilerOptions: {\n noUnusedLocals: true,\n noUnusedParameters: true,\n },\n });\n const generatedFile = project.createSourceFile(\"tmp.ts\", fileContent);\n\n generatedFile.insertStatements(0, \"/* eslint-disable @typescript-eslint/no-explicit-any */\");\n generatedFile.insertStatements(1, \"/* eslint-disable @typescript-eslint/array-type */\");\n\n generatedFile.getTypeAliases().forEach(node => {\n if (!node.getName().startsWith(\"Incremental\")) {\n node.setIsExported(false);\n }\n\n if (\n node.getName().startsWith(\"Query\") ||\n node.getName().startsWith(\"Mutation\") ||\n // Since they reference each other we remove them here.\n node.getName() === \"Site\" ||\n node.getName() === \"Event\" ||\n node.getName() === \"Operator\" ||\n node.getName() === \"Account\" ||\n node.getName() === \"Operator\" ||\n node.getName() === \"Asset\"\n ) {\n node.remove();\n }\n });\n generatedFile.getEnums().forEach(node => {\n node.setIsExported(false);\n });\n let lastWidth = 0;\n do {\n lastWidth = generatedFile.getFullWidth();\n removeUnusedIdentifiers(generatedFile);\n if (isVerbose) {\n console.log(\"Cleanup\", lastWidth, generatedFile.getFullWidth());\n }\n } while (lastWidth !== generatedFile.getFullWidth());\n\n generatedFile.getTypeAliases().forEach(node => {\n node.setIsExported(true);\n });\n generatedFile.getVariableStatements().forEach(variableStatement => {\n variableStatement.setIsExported(true);\n });\n generatedFile.getEnums().forEach(node => {\n node.setIsExported(true);\n convertEnumToConstObject(node);\n });\n\n if (isVerbose) {\n const end = new Date().getTime();\n const time = end - start;\n console.log(\"Execution time: \" + time);\n }\n\n return generatedFile.getFullText();\n};\n\n/**\n * Scrubs the file content.\n *\n * @param file The file to scrub.\n */\nexport const scrubFile = async (file: string, nxRoot: string, isVerbose?: boolean) => {\n const fileContent = readFileSync(file, { encoding: \"utf-8\" });\n if (fileContent.length === 0) {\n console.warn(`File ${file} is empty`);\n }\n const scrubbedMockFileContent = await scrubMockFileContent(fileContent, nxRoot, isVerbose);\n\n const scrubbedFileContent = await scrubFileContent(fileContent, nxRoot, isVerbose);\n\n let options = null;\n try {\n options = prettier\n .resolveConfig(join(nxRoot, \".prettierrc\"), {\n useCache: false,\n editorconfig: false,\n config: join(nxRoot, \".prettierrc\"),\n })\n .catch((error: Error) => console.log(error));\n } catch (e) {\n console.log(e);\n }\n if (!options) {\n throw new Error(\"Could not find prettier config\");\n }\n const prettierOptions = await options;\n\n let prettySrc = scrubbedFileContent;\n if (prettierOptions) {\n prettierOptions.parser = \"typescript\";\n try {\n prettySrc = await prettier.format(scrubbedFileContent, prettierOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n writeFileSync(file, prettySrc, { encoding: \"utf-8\" });\n\n let prettyMockSrc = scrubbedMockFileContent;\n if (prettierOptions) {\n prettierOptions.parser = \"typescript\";\n try {\n prettyMockSrc = await prettier.format(scrubbedMockFileContent, prettierOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n const parentDir = file.split(\"/\").slice(0, -1).join(\"/\");\n writeFileSync(parentDir + \"/mock.ts\", prettyMockSrc, { encoding: \"utf-8\" });\n return prettySrc;\n};\n"]}
1
+ {"version":3,"file":"scrubFile.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/scrubFile.ts"],"names":[],"mappings":";;;;AAAA,+BAA+B;AAC/B,2BAAiD;AACjD,+BAA4B;AAC5B,2DAAqC;AACrC,mDAAuD;AACvD,mCAAoG;AAEpG;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAAO,WAAmB,EAAE,MAAc,EAAE,SAAmB,EAAE,EAAE;IACjG,MAAM,KAAK,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;IAEnC,MAAM,UAAU,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEjD,IAAI,iBAAiB,GAAG,EAAE,CAAC;IAC3B,MAAM,WAAW,GAAG,UAAU,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC9D,WAAW,CAAC,gBAAgB,EAAE,CAAC;IAC/B,iBAAiB,IAAI,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC,gBAAgB,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACxG,MAAM,YAAY,GAAG,UAAU;SAC5B,cAAc,EAAE;SAChB,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,kBAAkB,EAAE,GAAG,WAAW,CAAC,gBAAgB,EAAE,CAAC;SACpF,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC;IAEzC,MAAM,EAAE,cAAc,EAAE,iBAAiB,EAAE,GAAG,IAAA,yBAAiB,EAAC,UAAU,EAAE,SAAS,CAAC,CAAC;IACvF,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAC;IAE5D,UAAU,CAAC,QAAQ,EAAE,CAAC,OAAO,CA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AG,YAAY,CAAC;QACtC,IAAI;YACF,SAAS,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;SACzE;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;SACnD;KACF;IACD,IAAA,kBAAa,EAAC,IAAI,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAEtD,IAAI,aAAa,GAAG,uBAAuB,CAAC;IAC5C,IAAI,eAAe,EAAE;QACnB,eAAe,CAAC,MAAM,GAAG,YAAY,CAAC;QACtC,IAAI;YACF,aAAa,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,uBAAuB,EAAE,eAAe,CAAC,CAAC;SACjF;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;SACnD;KACF;IACD,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzD,IAAA,kBAAa,EAAC,SAAS,GAAG,UAAU,EAAE,aAAa,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAC5E,OAAO,SAAS,CAAC;AACnB,CAAC,CAAA,CAAC;AAjDW,QAAA,SAAS,aAiDpB","sourcesContent":["/* eslint-disable no-console */\nimport { readFileSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport * as prettier from \"prettier\";\nimport { scrubMockFileContent } from \"./scrubMockFile\";\nimport { convertEnumToConstObject, createSourceFile, getPreservedTypes, isFragment } from \"./utils\";\n\n/**\n * Generates React hooks from your graphql files.\n */\nexport const scrubFileContent = async (fileContent: string, nxRoot: string, isVerbose?: boolean) => {\n const start = new Date().getTime();\n\n const sourceFile = createSourceFile(fileContent);\n\n let resultFileContent = \"\";\n const scalarsType = sourceFile.getTypeAliasOrThrow(\"Scalars\");\n scalarsType.getEndLineNumber();\n resultFileContent += fileContent.split(\"\\n\").slice(0, scalarsType.getEndLineNumber()).join(\"\\n\") + \"\\n\";\n const alreadyAdded = sourceFile\n .getTypeAliases()\n .filter(typeAlias => typeAlias.getStartLineNumber() < scalarsType.getEndLineNumber())\n .map(typeAlias => typeAlias.getName());\n\n const { preservedTypes, queryAndMutations } = getPreservedTypes(sourceFile, isVerbose);\n isVerbose && console.log(\"PreservedTypes:\", preservedTypes);\n\n sourceFile.getEnums().forEach(enumType => {\n const typeName = enumType.getName();\n if (preservedTypes.includes(typeName) && !alreadyAdded.includes(typeName) && !isFragment(typeName)) {\n alreadyAdded.push(typeName);\n resultFileContent += \"\\n\" + convertEnumToConstObject(enumType) + \"\\n\";\n }\n });\n sourceFile.getTypeAliases().forEach(typeAlias => {\n const typeName = typeAlias.getName();\n if (preservedTypes.includes(typeName) && !alreadyAdded.includes(typeName) && !isFragment(typeName)) {\n alreadyAdded.push(typeName);\n resultFileContent += \"\\n\" + typeAlias.getText() + \"\\n\";\n }\n });\n\n const resultQueryAndMutations = queryAndMutations.map(queryOrMutation => queryOrMutation.getFullText()).join(\"\\n\");\n\n const result = resultFileContent + \"\\n\" + resultQueryAndMutations;\n if (isVerbose) {\n const end = new Date().getTime();\n const time = end - start;\n console.log(\"Execution time: \" + time);\n }\n\n return result;\n};\n\n/**\n * Scrubs the file content.\n *\n * @param file The file to scrub.\n */\nexport const scrubFile = async (file: string, nxRoot: string, isVerbose?: boolean) => {\n const fileContent = readFileSync(file, { encoding: \"utf-8\" });\n if (fileContent.length === 0) {\n console.warn(`File ${file} is empty`);\n }\n const scrubbedMockFileContent = await scrubMockFileContent(fileContent, nxRoot, isVerbose);\n\n const scrubbedFileContent = await scrubFileContent(fileContent, nxRoot, isVerbose);\n\n let options = null;\n try {\n options = prettier\n .resolveConfig(join(nxRoot, \".prettierrc\"), {\n useCache: false,\n editorconfig: false,\n config: join(nxRoot, \".prettierrc\"),\n })\n .catch((error: Error) => console.log(error));\n } catch (e) {\n console.log(e);\n }\n if (!options) {\n throw new Error(\"Could not find prettier config\");\n }\n const prettierOptions = await options;\n\n let prettySrc = scrubbedFileContent;\n if (prettierOptions) {\n prettierOptions.parser = \"typescript\";\n try {\n prettySrc = await prettier.format(scrubbedFileContent, prettierOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n writeFileSync(file, prettySrc, { encoding: \"utf-8\" });\n\n let prettyMockSrc = scrubbedMockFileContent;\n if (prettierOptions) {\n prettierOptions.parser = \"typescript\";\n try {\n prettyMockSrc = await prettier.format(scrubbedMockFileContent, prettierOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n const parentDir = file.split(\"/\").slice(0, -1).join(\"/\");\n writeFileSync(parentDir + \"/mock.ts\", prettyMockSrc, { encoding: \"utf-8\" });\n return prettySrc;\n};\n"]}
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSourceFile = exports.convertEnumToConstObject = exports.getPreservedTypes = exports.isFragment = exports.isMutationVariables = exports.isMutation = exports.isQueryVariables = exports.isQuery = void 0;
4
+ const ts_morph_1 = require("ts-morph");
5
+ /**
6
+ * Check if the name is a *Query
7
+ */
8
+ const isQuery = (name) => {
9
+ return name.endsWith("Query") && name.length > "Query".length;
10
+ };
11
+ exports.isQuery = isQuery;
12
+ /**
13
+ * Check if the name is a *QueryVariables
14
+ */
15
+ const isQueryVariables = (name) => {
16
+ return name.endsWith("QueryVariables");
17
+ };
18
+ exports.isQueryVariables = isQueryVariables;
19
+ /**
20
+ * Check if the name is a Mutation
21
+ */
22
+ const isMutation = (name) => {
23
+ return name.endsWith("Mutation") && name.length > "Mutation".length;
24
+ };
25
+ exports.isMutation = isMutation;
26
+ /**
27
+ * Check if the name is a *QueryVariables
28
+ */
29
+ const isMutationVariables = (name) => {
30
+ return name.endsWith("MutationVariables");
31
+ };
32
+ exports.isMutationVariables = isMutationVariables;
33
+ /**
34
+ * Check if the name is a *Fragment and not *_Fragment
35
+ */
36
+ const isFragment = (name) => {
37
+ return name.endsWith("Fragment") && !name.endsWith("_Fragment");
38
+ };
39
+ exports.isFragment = isFragment;
40
+ /**
41
+ * Debug where you are in the tree
42
+ */
43
+ const getFullPathUpOfNode = (node) => {
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ let currentNode = node;
46
+ const paths = [];
47
+ while (currentNode) {
48
+ // You can customize what details to include for each node (e.g., kind, text)
49
+ let name = currentNode.getName ? currentNode.getName() : currentNode.getKindName();
50
+ if (node.getKind() === ts_morph_1.SyntaxKind.TypeLiteral) {
51
+ const typeLiteral = node.asKindOrThrow(ts_morph_1.SyntaxKind.TypeLiteral);
52
+ name = typeLiteral.getText();
53
+ }
54
+ paths.unshift(`${name}`);
55
+ currentNode = currentNode.getParent();
56
+ }
57
+ return paths.join(">>");
58
+ };
59
+ /**
60
+ * Get the types that should be preserved
61
+ */
62
+ const getPreservedTypes = (sourceFile, isVerbose) => {
63
+ const typeAliases = sourceFile.getTypeAliases();
64
+ const queryAndMutations = [];
65
+ const preservedTypes = [];
66
+ const variablesListToProcess = [];
67
+ const queryMutationListToProcess = [];
68
+ typeAliases.forEach(typeAlias => {
69
+ const name = typeAlias.getName();
70
+ if ((0, exports.isQueryVariables)(name) || (0, exports.isMutationVariables)(name) || (0, exports.isFragment)(name)) {
71
+ variablesListToProcess.push(typeAlias);
72
+ queryAndMutations.push(typeAlias);
73
+ }
74
+ else if ((0, exports.isQuery)(name) || (0, exports.isMutation)(name)) {
75
+ queryMutationListToProcess.push(typeAlias);
76
+ queryAndMutations.push(typeAlias);
77
+ }
78
+ });
79
+ sourceFile.getVariableStatements().forEach(variableStatement => {
80
+ var _a;
81
+ const variable = (_a = variableStatement
82
+ .getFirstChildByKind(ts_morph_1.SyntaxKind.VariableDeclarationList)) === null || _a === void 0 ? void 0 : _a.getFirstChildByKind(ts_morph_1.SyntaxKind.VariableDeclaration);
83
+ if (variable === null || variable === void 0 ? void 0 : variable.getName().endsWith("Document")) {
84
+ queryAndMutations.push(variableStatement);
85
+ }
86
+ else if (variable === null || variable === void 0 ? void 0 : variable.getName().endsWith("FragmentDoc")) {
87
+ queryAndMutations.push(variableStatement);
88
+ }
89
+ });
90
+ // RUN VARAIABLES FIRST since it will force full props if nessasary
91
+ // VARIABLES are input so force include all props
92
+ variablesListToProcess.forEach(typeAlias => {
93
+ preserveTypes(typeAlias, preservedTypes, sourceFile, isVerbose);
94
+ });
95
+ // QUERY and MUTATION are dynamic so dont force props
96
+ queryMutationListToProcess.forEach(typeAlias => {
97
+ preserveTypes(typeAlias, preservedTypes, sourceFile, isVerbose);
98
+ });
99
+ return { preservedTypes, queryAndMutations };
100
+ };
101
+ exports.getPreservedTypes = getPreservedTypes;
102
+ const preserveTypes = (node, preservedTypes, sourceFile, isVerbose) => {
103
+ const typeReferences = [];
104
+ if (!node.getText().startsWith("export type Query") && !node.getText().startsWith("export type Mutation")) {
105
+ node.forEachDescendant(descendant => {
106
+ if (descendant.getKind() === ts_morph_1.SyntaxKind.TypeReference) {
107
+ if (!descendant.getText().startsWith("Scalars") &&
108
+ !descendant.getText().startsWith("Maybe<") &&
109
+ !descendant.getText().startsWith("InputMaybe<") &&
110
+ !descendant.getText().startsWith("Array<") &&
111
+ !descendant.getText().startsWith("Exact<") &&
112
+ !descendant.getText().includes("__typename")) {
113
+ if (isVerbose) {
114
+ // eslint-disable-next-line no-console
115
+ console.log("ADDING", getFullPathUpOfNode(descendant), ":", descendant.getText(), descendant.getKindName());
116
+ }
117
+ if (!preservedTypes.includes(descendant.getText())) {
118
+ const foundTypeAlias = sourceFile.getTypeAlias(descendant.getText());
119
+ preservedTypes.push(descendant.getText());
120
+ if (foundTypeAlias) {
121
+ preserveTypes(foundTypeAlias, preservedTypes, sourceFile, isVerbose);
122
+ }
123
+ }
124
+ }
125
+ }
126
+ });
127
+ }
128
+ return typeReferences;
129
+ };
130
+ /**
131
+ * Convert an enum to a const object and string literal type
132
+ */
133
+ const convertEnumToConstObject = (enumDeclaration) => {
134
+ const enumName = enumDeclaration.getName();
135
+ const members = enumDeclaration.getMembers();
136
+ const properties = members.map(member => {
137
+ const name = member.getName();
138
+ const value = member.getValue();
139
+ return `${name}: '${value}'`;
140
+ });
141
+ const lowerCaseFirstLetter = enumName[0].toLowerCase() + enumName.slice(1);
142
+ const constObjectText = `export const ${lowerCaseFirstLetter} = {\n ${properties.join(",\n ")},\n} as const;\nexport type ${enumName} = typeof ${lowerCaseFirstLetter}[keyof typeof ${lowerCaseFirstLetter}];`;
143
+ return constObjectText;
144
+ };
145
+ exports.convertEnumToConstObject = convertEnumToConstObject;
146
+ /**
147
+ * Create a source file from a string
148
+ */
149
+ const createSourceFile = (fileContent) => {
150
+ const project = new ts_morph_1.Project({
151
+ compilerOptions: {
152
+ noUnusedLocals: true,
153
+ noUnusedParameters: true,
154
+ },
155
+ });
156
+ return project.createSourceFile("tmp.ts", fileContent);
157
+ };
158
+ exports.createSourceFile = createSourceFile;
159
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/utils.ts"],"names":[],"mappings":";;;AAAA,uCAAwG;AAExG;;GAEG;AACI,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE;IACtC,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;AAChE,CAAC,CAAC;AAFW,QAAA,OAAO,WAElB;AACF;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAE,EAAE;IAC/C,OAAO,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC;AACzC,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B;AAEF;;G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AChC,OAAO,GAAG,IAAI,MAAM,KAAK,GAAG,CAAC;IAC/B,CAAC,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3E,MAAM,eAAe,GAAG,gBAAgB,oBAAoB,WAAW,UAAU,CAAC,IAAI,CACpF,OAAO,CACR,+BAA+B,QAAQ,aAAa,oBAAoB,iBAAiB,oBAAoB,IAAI,CAAC;IAEnH,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AAhBW,QAAA,wBAAwB,4BAgBnC;AAEF;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAAC,WAAmB,EAAE,EAAE;IACtD,MAAM,OAAO,GAAG,IAAI,kBAAO,CAAC;QAC1B,eAAe,EAAE;YACf,cAAc,EAAE,IAAI;YACpB,kBAAkB,EAAE,IAAI;SACzB;KACF,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,gBAAgB,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AACzD,CAAC,CAAC;AATW,QAAA,gBAAgB,oBAS3B","sourcesContent":["import { EnumDeclaration, Node, Project, SourceFile, SyntaxKind, TypeAliasDeclaration } from \"ts-morph\";\n\n/**\n * Check if the name is a *Query\n */\nexport const isQuery = (name: string) => {\n return name.endsWith(\"Query\") && name.length > \"Query\".length;\n};\n/**\n * Check if the name is a *QueryVariables\n */\nexport const isQueryVariables = (name: string) => {\n return name.endsWith(\"QueryVariables\");\n};\n\n/**\n * Check if the name is a Mutation\n */\nexport const isMutation = (name: string) => {\n return name.endsWith(\"Mutation\") && name.length > \"Mutation\".length;\n};\n\n/**\n * Check if the name is a *QueryVariables\n */\nexport const isMutationVariables = (name: string) => {\n return name.endsWith(\"MutationVariables\");\n};\n\n/**\n * Check if the name is a *Fragment and not *_Fragment\n */\nexport const isFragment = (name: string) => {\n return name.endsWith(\"Fragment\") && !name.endsWith(\"_Fragment\");\n};\n\n/**\n * Debug where you are in the tree\n */\nconst getFullPathUpOfNode = (node: Node) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let currentNode: any | undefined = node;\n const paths = [];\n\n while (currentNode) {\n // You can customize what details to include for each node (e.g., kind, text)\n let name = currentNode.getName ? currentNode.getName() : currentNode.getKindName();\n if (node.getKind() === SyntaxKind.TypeLiteral) {\n const typeLiteral = node.asKindOrThrow(SyntaxKind.TypeLiteral);\n name = typeLiteral.getText();\n }\n\n paths.unshift(`${name}`);\n currentNode = currentNode.getParent();\n }\n\n return paths.join(\">>\");\n};\n\n/**\n * Get the types that should be preserved\n */\nexport const getPreservedTypes = (\n sourceFile: SourceFile,\n isVerbose?: boolean\n): {\n preservedTypes: string[];\n queryAndMutations: Node[];\n} => {\n const typeAliases = sourceFile.getTypeAliases();\n const queryAndMutations: Node[] = [];\n\n const preservedTypes: string[] = [];\n\n const variablesListToProcess: TypeAliasDeclaration[] = [];\n const queryMutationListToProcess: TypeAliasDeclaration[] = [];\n\n typeAliases.forEach(typeAlias => {\n const name = typeAlias.getName();\n if (isQueryVariables(name) || isMutationVariables(name) || isFragment(name)) {\n variablesListToProcess.push(typeAlias);\n queryAndMutations.push(typeAlias);\n } else if (isQuery(name) || isMutation(name)) {\n queryMutationListToProcess.push(typeAlias);\n queryAndMutations.push(typeAlias);\n }\n });\n\n sourceFile.getVariableStatements().forEach(variableStatement => {\n const variable = variableStatement\n .getFirstChildByKind(SyntaxKind.VariableDeclarationList)\n ?.getFirstChildByKind(SyntaxKind.VariableDeclaration);\n if (variable?.getName().endsWith(\"Document\")) {\n queryAndMutations.push(variableStatement);\n } else if (variable?.getName().endsWith(\"FragmentDoc\")) {\n queryAndMutations.push(variableStatement);\n }\n });\n\n // RUN VARAIABLES FIRST since it will force full props if nessasary\n\n // VARIABLES are input so force include all props\n variablesListToProcess.forEach(typeAlias => {\n preserveTypes(typeAlias, preservedTypes, sourceFile, isVerbose);\n });\n\n // QUERY and MUTATION are dynamic so dont force props\n queryMutationListToProcess.forEach(typeAlias => {\n preserveTypes(typeAlias, preservedTypes, sourceFile, isVerbose);\n });\n\n return { preservedTypes, queryAndMutations };\n};\n\nconst preserveTypes = (node: Node, preservedTypes: string[], sourceFile: SourceFile, isVerbose?: boolean): Node[] => {\n const typeReferences: Node[] = [];\n if (!node.getText().startsWith(\"export type Query\") && !node.getText().startsWith(\"export type Mutation\")) {\n node.forEachDescendant(descendant => {\n if (descendant.getKind() === SyntaxKind.TypeReference) {\n if (\n !descendant.getText().startsWith(\"Scalars\") &&\n !descendant.getText().startsWith(\"Maybe<\") &&\n !descendant.getText().startsWith(\"InputMaybe<\") &&\n !descendant.getText().startsWith(\"Array<\") &&\n !descendant.getText().startsWith(\"Exact<\") &&\n !descendant.getText().includes(\"__typename\")\n ) {\n if (isVerbose) {\n // eslint-disable-next-line no-console\n console.log(\"ADDING\", getFullPathUpOfNode(descendant), \":\", descendant.getText(), descendant.getKindName());\n }\n if (!preservedTypes.includes(descendant.getText())) {\n const foundTypeAlias = sourceFile.getTypeAlias(descendant.getText());\n preservedTypes.push(descendant.getText());\n if (foundTypeAlias) {\n preserveTypes(foundTypeAlias, preservedTypes, sourceFile, isVerbose);\n }\n }\n }\n }\n });\n }\n\n return typeReferences;\n};\n\n/**\n * Convert an enum to a const object and string literal type\n */\nexport const convertEnumToConstObject = (enumDeclaration: EnumDeclaration) => {\n const enumName = enumDeclaration.getName();\n const members = enumDeclaration.getMembers();\n\n const properties = members.map(member => {\n const name = member.getName();\n const value = member.getValue();\n return `${name}: '${value}'`;\n });\n\n const lowerCaseFirstLetter = enumName[0].toLowerCase() + enumName.slice(1);\n const constObjectText = `export const ${lowerCaseFirstLetter} = {\\n ${properties.join(\n \",\\n \"\n )},\\n} as const;\\nexport type ${enumName} = typeof ${lowerCaseFirstLetter}[keyof typeof ${lowerCaseFirstLetter}];`;\n\n return constObjectText;\n};\n\n/**\n * Create a source file from a string\n */\nexport const createSourceFile = (fileContent: string) => {\n const project = new Project({\n compilerOptions: {\n noUnusedLocals: true,\n noUnusedParameters: true,\n },\n });\n\n return project.createSourceFile(\"tmp.ts\", fileContent);\n};\n"]}