@trackunit/react-graphql-tools 1.14.83-alpha-0322e181a48.0 → 1.14.84
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 +17 -0
- package/migrations/entry.js.map +1 -0
- package/package.json +2 -3
- package/src/executors/createHooks/executor.js +5 -5
- package/src/executors/createHooks/executor.js.map +1 -1
- package/src/executors/createHooks/generateHooks.js +2 -13
- package/src/executors/createHooks/generateHooks.js.map +1 -1
- package/src/executors/createHooks/schema.d.ts +3 -1
- package/src/executors/createHooks/schema.json +7 -1
- package/src/executors/createHooks/scrubFile.js +22 -10
- package/src/executors/createHooks/scrubFile.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
## 1.14.84 (2026-07-30)
|
|
2
|
+
|
|
3
|
+
### 🩹 Fixes
|
|
4
|
+
|
|
5
|
+
- **react-graphql-tools:** remove stale mock.ts when mock generation is disabled [SF-660] ([4e85ea20bfa](https://github.com/Trackunit/manager/commit/4e85ea20bfa))
|
|
6
|
+
|
|
7
|
+
### ❤️ Thank You
|
|
8
|
+
|
|
9
|
+
- Cursor @cursoragent
|
|
10
|
+
- kla-trackunit
|
|
11
|
+
|
|
12
|
+
## 1.14.83 (2026-07-29)
|
|
13
|
+
|
|
14
|
+
### 🧱 Updated Dependencies
|
|
15
|
+
|
|
16
|
+
- Updated react-vite-test-setup to 0.0.83
|
|
17
|
+
|
|
1
18
|
## 1.14.82 (2026-07-29)
|
|
2
19
|
|
|
3
20
|
### 🧱 Updated Dependencies
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entry.js","sourceRoot":"","sources":["../../../../../libs/react/graphql-tools/migrations/entry.ts"],"names":[],"mappings":"","sourcesContent":["export {};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-graphql-tools",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.84",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"executors": "./executors.json",
|
|
6
6
|
"generators": "./generators.json",
|
|
@@ -17,8 +17,7 @@
|
|
|
17
17
|
"@faker-js/faker": "^8.4.1",
|
|
18
18
|
"win-ca": "^3.5.1",
|
|
19
19
|
"tslib": "^2.6.2",
|
|
20
|
-
"@nx/devkit": "
|
|
21
|
-
"@nx/js": "23.1.0"
|
|
20
|
+
"@nx/devkit": "22.7.5"
|
|
22
21
|
},
|
|
23
22
|
"migrations": "./migrations.json",
|
|
24
23
|
"types": "./src/index.d.ts",
|
|
@@ -9,11 +9,11 @@ const scrubFile_1 = require("./scrubFile");
|
|
|
9
9
|
/**
|
|
10
10
|
* This executor generates react hooks for all graphql files.
|
|
11
11
|
*
|
|
12
|
-
* @param
|
|
12
|
+
* @param options GraphQL generation options
|
|
13
13
|
* @param context The execution context for this executor
|
|
14
|
-
* @returns {Promise<{ success:
|
|
14
|
+
* @returns {Promise<{ success: boolean; }>} success when finished
|
|
15
15
|
*/
|
|
16
|
-
const CreateGraphqlHooks = async (
|
|
16
|
+
const CreateGraphqlHooks = async (options, context) => {
|
|
17
17
|
const nxRoot = `${context.root}`;
|
|
18
18
|
if (!context.projectName) {
|
|
19
19
|
throw new Error("context projectName is required");
|
|
@@ -55,10 +55,10 @@ export default config;
|
|
|
55
55
|
// eslint-disable-next-line no-console
|
|
56
56
|
console.log("Scrubbing file: ", pathToGenerateFile);
|
|
57
57
|
}
|
|
58
|
-
await (0, scrubFile_1.scrubFile)(pathToGenerateFile, nxRoot, context.isVerbose);
|
|
58
|
+
await (0, scrubFile_1.scrubFile)(pathToGenerateFile, nxRoot, context.isVerbose, options.generateMocks);
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
return { success: result };
|
|
61
|
+
return { success: Boolean(result) };
|
|
62
62
|
};
|
|
63
63
|
exports.CreateGraphqlHooks = CreateGraphqlHooks;
|
|
64
64
|
//# sourceMappingURL=executor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/executor.ts"],"names":[],"mappings":";;;AACA,2BAAgC;AAChC,+BAA4B;AAC5B,kBAAgB;AAChB,mDAAgD;
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/executor.ts"],"names":[],"mappings":";;;AACA,2BAAgC;AAChC,+BAA4B;AAC5B,kBAAgB;AAChB,mDAAgD;AAEhD,2CAAwC;AAExC;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,KAAK,EACrC,OAAyC,EACzC,OAAwB,EACgB,EAAE;IAC1C,MAAM,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;IAED,MAAM,OAAO,GAAG,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC;IAEnF,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;IAC1C,CAAC;IAED,MAAM,WAAW,GAAG,IAAA,WAAI,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,MAAM,YAAY,GAAG,IAAA,eAAU,EAAC,IAAA,WAAI,EAAC,WAAW,EAAE,mBAAmB,CAAC,CAAC;QACrE,CAAC,CAAC,IAAA,WAAI,EAAC,WAAW,EAAE,mBAAmB,CAAC;QACxC,CAAC,CAAC,IAAA,WAAI,EAAC,WAAW,EAAE,mBAAmB,CAAC,CAAC;IAE3C,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,eAAe,GAAG,IAAA,WAAI,EAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IACxD,IAAI,CAAC,IAAA,eAAU,EAAC,eAAe,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,gCAAgC,WAAW;;;;;;;;;CAS9D,CAAC,CAAC;IACD,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,IAAA,6BAAa,EAAC;QACjC,MAAM,EAAE,OAAO,CAAC,IAAI;QACpB,eAAe;QACf,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,YAAY;KACb,CAAC,CAAC;IACH,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,KAAK,MAAM,EAAE,CAAC;QACtC,MAAM,kBAAkB,GAAG,IAAA,WAAI,EAAC,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,YAAY,CAAC,CAAC;QAC9F,IAAI,IAAA,eAAU,EAAC,kBAAkB,CAAC,EAAE,CAAC;YACnC,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;gBACtB,sCAAsC;gBACtC,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;YACtD,CAAC;YACD,MAAM,IAAA,qBAAS,EAAC,kBAAkB,EAAE,MAAM,EAAE,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;QACxF,CAAC;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;AACtC,CAAC,CAAC;AAvDW,QAAA,kBAAkB,sBAuD7B","sourcesContent":["import { ExecutorContext } from \"@nx/devkit\";\nimport { existsSync } from \"fs\";\nimport { join } from \"path\";\nimport \"win-ca\";\nimport { generateHooks } from \"./generateHooks\";\nimport type { CreateGraphqlHooksExecutorSchema } from \"./schema\";\nimport { scrubFile } from \"./scrubFile\";\n\n/**\n * This executor generates react hooks for all graphql files.\n *\n * @param options GraphQL generation options\n * @param context The execution context for this executor\n * @returns {Promise<{ success: boolean; }>} success when finished\n */\nexport const CreateGraphqlHooks = async (\n options: CreateGraphqlHooksExecutorSchema,\n context: ExecutorContext\n): Promise<{ readonly success: boolean }> => {\n const nxRoot = `${context.root}`;\n if (!context.projectName) {\n throw new Error(\"context projectName is required\");\n }\n\n const libRoot = context.projectsConfigurations.projects[context.projectName]?.root;\n\n if (!libRoot) {\n throw new Error(\"Missing library root\");\n }\n\n const fullLibRoot = join(nxRoot, libRoot);\n const tsConfigPath = existsSync(join(fullLibRoot, \"tsconfig.lib.json\"))\n ? join(fullLibRoot, \"tsconfig.lib.json\")\n : join(fullLibRoot, \"tsconfig.app.json\");\n\n if (!tsConfigPath) {\n throw new Error(\"context projectsConfigurations is required\");\n }\n\n const codeGenFilePath = join(fullLibRoot, \"codegen.ts\");\n if (!existsSync(codeGenFilePath)) {\n throw new Error(`codegen.ts file not found in ${fullLibRoot} please add this file and run again to generate hooks. The content should be\nimport { type CodegenConfig } from \"@graphql-codegen/cli\";\nimport { getGraphqlCodegenConfig } from \"@trackunit/iris-app-build-utilities\";\n\nconst config: CodegenConfig = {\n ...getGraphqlCodegenConfig(__dirname),\n // You can override codegen default config here\n};\nexport default config;\n`);\n }\n\n const result = await generateHooks({\n nxRoot: context.root,\n codeGenFilePath,\n isVerbose: context.isVerbose,\n tsConfigPath,\n });\n if (process.env.SKIP_SCRUB !== \"true\") {\n const pathToGenerateFile = join(fullLibRoot, \"src\", \"generated\", \"graphql-api\", \"graphql.ts\");\n if (existsSync(pathToGenerateFile)) {\n if (context.isVerbose) {\n // eslint-disable-next-line no-console\n console.log(\"Scrubbing file: \", pathToGenerateFile);\n }\n await scrubFile(pathToGenerateFile, nxRoot, context.isVerbose, options.generateMocks);\n }\n }\n return { success: Boolean(result) };\n};\n"]}
|
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateHooks = void 0;
|
|
4
4
|
const cli_1 = require("@graphql-codegen/cli");
|
|
5
|
-
const internal_1 = require("@nx/js/internal");
|
|
6
|
-
const node_path_1 = require("node:path");
|
|
7
5
|
/**
|
|
8
6
|
* Generates React hooks from your graphql files.
|
|
9
7
|
*
|
|
@@ -30,17 +28,8 @@ const generateHooks = async (options) => {
|
|
|
30
28
|
// eslint-disable-next-line no-console
|
|
31
29
|
console.log(contextConfig);
|
|
32
30
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// aliases codegen.ts imports only resolve once we register them ourselves.
|
|
36
|
-
const unregisterTsProject = (0, internal_1.registerTsProject)((0, node_path_1.join)(options.nxRoot, "tsconfig.base.json"));
|
|
37
|
-
try {
|
|
38
|
-
const context = await (0, cli_1.createContext)(contextConfig);
|
|
39
|
-
return await (0, cli_1.generate)(context);
|
|
40
|
-
}
|
|
41
|
-
finally {
|
|
42
|
-
unregisterTsProject();
|
|
43
|
-
}
|
|
31
|
+
const context = await (0, cli_1.createContext)(contextConfig);
|
|
32
|
+
return await (0, cli_1.generate)(context);
|
|
44
33
|
};
|
|
45
34
|
exports.generateHooks = generateHooks;
|
|
46
35
|
//# sourceMappingURL=generateHooks.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateHooks.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/generateHooks.ts"],"names":[],"mappings":";;;AAAA,8CAA+D;AAC/D
|
|
1
|
+
{"version":3,"file":"generateHooks.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/generateHooks.ts"],"names":[],"mappings":";;;AAAA,8CAA+D;AAC/D;;;;;;;;GAQG;AACI,MAAM,aAAa,GAAG,KAAK,EAAE,OAKnC,EAAE,EAAE;IACH,MAAM,aAAa,GAAG;QACpB,KAAK,EAAE,KAAK;QACZ,SAAS,EAAE,IAAI;QACf,MAAM,EAAE,CAAC,OAAO,CAAC,SAAS;QAC1B,UAAU,EAAE,KAAK;QACjB,OAAO,EAAE,KAAK;QACd,OAAO,EAAE,OAAO,CAAC,YAAY;QAC7B,MAAM,EAAE,OAAO,CAAC,eAAe;QAC/B,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,OAAO,CAAC,SAAS;QAC1B,KAAK,EAAE,OAAO,CAAC,SAAS;KACzB,CAAC;IACF,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QACtB,sCAAsC;QACtC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAC7B,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,IAAA,mBAAa,EAAC,aAAa,CAAC,CAAC;IAEnD,OAAO,MAAM,IAAA,cAAQ,EAAC,OAAO,CAAC,CAAC;AACjC,CAAC,CAAC;AA1BW,QAAA,aAAa,iBA0BxB","sourcesContent":["import { createContext, generate } from \"@graphql-codegen/cli\";\n/**\n * Generates React hooks from your graphql files.\n *\n * @param options - The generation options.\n * @param options.codeGenFilePath - The path to the codegen file.\n * @param options.nxRoot - The nx root.\n * @param options.tsConfigPath - The path to the tsconfig file.\n * @param options.isVerbose - If true, will log more information.\n */\nexport const generateHooks = async (options: {\n codeGenFilePath: string;\n nxRoot: string;\n isVerbose?: boolean;\n tsConfigPath: string;\n}) => {\n const contextConfig = {\n watch: false,\n overwrite: true,\n silent: !options.isVerbose,\n errorsOnly: false,\n profile: false,\n project: options.tsConfigPath,\n config: options.codeGenFilePath,\n require: [],\n verbose: options.isVerbose,\n debug: options.isVerbose,\n };\n if (options.isVerbose) {\n // eslint-disable-next-line no-console\n console.log(contextConfig);\n }\n\n const context = await createContext(contextConfig);\n\n return await generate(context);\n};\n"]}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "http://json-schema.org/schema",
|
|
3
3
|
"type": "object",
|
|
4
|
-
"properties": {
|
|
4
|
+
"properties": {
|
|
5
|
+
"generateMocks": {
|
|
6
|
+
"type": "boolean",
|
|
7
|
+
"default": true,
|
|
8
|
+
"description": "Generate React/Apollo mock helpers alongside operation types."
|
|
9
|
+
}
|
|
10
|
+
}
|
|
5
11
|
}
|
|
@@ -19,7 +19,9 @@ const utils_1 = require("./utils");
|
|
|
19
19
|
*/
|
|
20
20
|
const getPrettierOptions = async (nxRoot) => {
|
|
21
21
|
const prettierConfigPath = (0, path_1.join)(nxRoot, "prettier.config.mjs");
|
|
22
|
-
return await prettier
|
|
22
|
+
return await prettier
|
|
23
|
+
.resolveConfig(prettierConfigPath, { useCache: false, editorconfig: false })
|
|
24
|
+
.catch((error) => {
|
|
23
25
|
console.log(error);
|
|
24
26
|
return null;
|
|
25
27
|
});
|
|
@@ -72,7 +74,7 @@ exports.scrubFileContent = scrubFileContent;
|
|
|
72
74
|
*
|
|
73
75
|
* @param file The file to scrub.
|
|
74
76
|
*/
|
|
75
|
-
const scrubFile = async (file, nxRoot, isVerbose) => {
|
|
77
|
+
const scrubFile = async (file, nxRoot, isVerbose, generateMocks = true) => {
|
|
76
78
|
const fileContent = (0, fs_1.readFileSync)(file, { encoding: "utf-8" });
|
|
77
79
|
if (fileContent.length === 0) {
|
|
78
80
|
console.warn(`File ${file} is empty`);
|
|
@@ -90,7 +92,9 @@ const scrubFile = async (file, nxRoot, isVerbose) => {
|
|
|
90
92
|
// The main scrub reads line numbers from the pristine AST, so it must run before the mock scrub
|
|
91
93
|
// mutates the shared source file via insertStatements().
|
|
92
94
|
const scrubbedFileContent = await (0, exports.scrubFileContent)(fileContent, nxRoot, isVerbose, sharedSourceFile);
|
|
93
|
-
const scrubbedMockFileContent =
|
|
95
|
+
const scrubbedMockFileContent = generateMocks
|
|
96
|
+
? await (0, scrubMockFile_1.scrubMockFileContent)(fileContent, nxRoot, isVerbose, sharedSourceFile)
|
|
97
|
+
: undefined;
|
|
94
98
|
const prettierOptions = await getPrettierOptions(nxRoot);
|
|
95
99
|
let prettySrc = scrubbedFileContent;
|
|
96
100
|
let prettyMockSrc = scrubbedMockFileContent;
|
|
@@ -102,17 +106,25 @@ const scrubFile = async (file, nxRoot, isVerbose) => {
|
|
|
102
106
|
catch (error) {
|
|
103
107
|
console.error("Error in prettier.format:", error);
|
|
104
108
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
if (scrubbedMockFileContent !== undefined) {
|
|
110
|
+
try {
|
|
111
|
+
prettyMockSrc = await prettier.format(scrubbedMockFileContent, formatOptions);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error("Error in prettier.format:", error);
|
|
115
|
+
}
|
|
110
116
|
}
|
|
111
117
|
}
|
|
112
118
|
(0, fs_1.writeFileSync)(file, prettySrc, { encoding: "utf-8" });
|
|
113
|
-
const
|
|
119
|
+
const mockFilePath = (0, path_1.join)((0, path_1.dirname)(file), "mock.ts");
|
|
120
|
+
// A project that turns mock generation off may still carry a mock file from an earlier run,
|
|
121
|
+
// so remove it instead of leaving stale helpers behind in the generated output.
|
|
122
|
+
if (prettyMockSrc === undefined) {
|
|
123
|
+
(0, fs_1.rmSync)(mockFilePath, { force: true });
|
|
124
|
+
return prettySrc;
|
|
125
|
+
}
|
|
114
126
|
if (prettyMockSrc.length > 0) {
|
|
115
|
-
(0, fs_1.writeFileSync)(
|
|
127
|
+
(0, fs_1.writeFileSync)(mockFilePath, prettyMockSrc, { encoding: "utf-8" });
|
|
116
128
|
}
|
|
117
129
|
return prettySrc;
|
|
118
130
|
};
|
|
@@ -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,+BAAqC;AACrC,2DAAqC;AACrC,uCAA+C;AAC/C,mDAAuD;AACvD,mCAAoG;AAEpG;;;;;;;GAOG;AACH,MAAM,kBAAkB,GAAG,KAAK,EAAE,MAAc,EAAoC,EAAE;IACpF,MAAM,kBAAkB,GAAG,IAA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D,IAAI,SAAS,GAAG,mBAAmB,CAAC;IACpC,IAAI,aAAa,GAAG,uBAAuB,CAAC;IAC5C,IAAI,eAAe,EAAE,CAAC;QACpB,MAAM,aAAa,GAAqB,EAAE,GAAG,eAAe,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;QACrF,IAAI,CAAC;YACH,SAAS,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC;QACxE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,CAAC;YACH,aAAa,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,uBAAuB,EAAE,aAAa,CAAC,CAAC;QAChF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;IACD,IAAA,kBAAa,EAAC,IAAI,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAEtD,MAAM,SAAS,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;IAChC,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,IAAA,kBAAa,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,SAAS,CAAC,EAAE,aAAa,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAClF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AA/CW,QAAA,SAAS,aA+CpB","sourcesContent":["/* eslint-disable no-console */\nimport { readFileSync, writeFileSync } from \"fs\";\nimport { dirname, join } from \"path\";\nimport * as prettier from \"prettier\";\nimport { Project, SourceFile } from \"ts-morph\";\nimport { scrubMockFileContent } from \"./scrubMockFile\";\nimport { convertEnumToConstObject, createSourceFile, getPreservedTypes, isFragment } from \"./utils\";\n\n/**\n * Resolves the repo's Prettier config by searching upward from `nxRoot` (the same\n * lookup Prettier's CLI/editor integrations use), rather than requiring an exact\n * `prettier.config.mjs` at that path. This lets generated code be formatted with the\n * repo's real config when it exists, while degrading gracefully (no formatting, no\n * throw) for workspaces without one - e.g. the ad-hoc workspaces `nx-tools-e2e`\n * scaffolds via `create-iris-app-workspace` to exercise this executor in isolation.\n */\nconst getPrettierOptions = async (nxRoot: string): Promise<prettier.Options | null> => {\n const prettierConfigPath = join(nxRoot, \"prettier.config.mjs\");\n return await prettier.resolveConfig(prettierConfigPath, { useCache: false, editorconfig: false }).catch(\n (error: Error): null => {\n console.log(error);\n return null;\n }\n );\n};\n\n/**\n * Generates React hooks from your graphql files.\n */\nexport const scrubFileContent = async (\n fileContent: string,\n _nxRoot: string,\n isVerbose?: boolean,\n providedSourceFile?: SourceFile\n) => {\n const start = new Date().getTime();\n\n const sourceFile = providedSourceFile ?? 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 resultFileContent = resultFileContent.replace(\n \"export type InputMaybe<T> = Maybe<T>;\",\n \"export type InputMaybe<T> = Maybe<T> | undefined;\"\n );\n\n const { preservedTypes, queryAndMutations } = getPreservedTypes(sourceFile, isVerbose);\n if (isVerbose) {\n console.log(\"PreservedTypes:\", preservedTypes);\n }\n\n sourceFile.getEnums().forEach(enumType => {\n const typeName = enumType.getName();\n if (preservedTypes.has(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.has(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\n // Parse the (large) generated file into ts-morph once and share it between the main and mock scrub\n // instead of parsing it twice. Use the mock's stricter compiler options (the main scrub doesn't use\n // the TypeChecker, so they don't affect its output).\n const sharedSourceFile = new Project({\n compilerOptions: {\n noUnusedLocals: true,\n noUnusedParameters: true,\n strictNullChecks: true,\n },\n }).createSourceFile(\"tmp.ts\", fileContent);\n\n // The main scrub reads line numbers from the pristine AST, so it must run before the mock scrub\n // mutates the shared source file via insertStatements().\n const scrubbedFileContent = await scrubFileContent(fileContent, nxRoot, isVerbose, sharedSourceFile);\n\n const scrubbedMockFileContent = await scrubMockFileContent(fileContent, nxRoot, isVerbose, sharedSourceFile);\n\n const prettierOptions = await getPrettierOptions(nxRoot);\n\n let prettySrc = scrubbedFileContent;\n let prettyMockSrc = scrubbedMockFileContent;\n if (prettierOptions) {\n const formatOptions: prettier.Options = { ...prettierOptions, parser: \"typescript\" };\n try {\n prettySrc = await prettier.format(scrubbedFileContent, formatOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n try {\n prettyMockSrc = await prettier.format(scrubbedMockFileContent, formatOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n writeFileSync(file, prettySrc, { encoding: \"utf-8\" });\n\n const parentDir = dirname(file);\n if (prettyMockSrc.length > 0) {\n writeFileSync(join(parentDir, \"mock.ts\"), prettyMockSrc, { encoding: \"utf-8\" });\n }\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,2BAAyD;AACzD,+BAAqC;AACrC,2DAAqC;AACrC,uCAA+C;AAC/C,mDAAuD;AACvD,mCAAoG;AAEpG;;;;;;;GAOG;AACH,MAAM,kBAAkB,GAAG,KAAK,EAAE,MAAc,EAAoC,EAAE;IACpF,MAAM,kBAAkB,GAAG,IAAA,WAAI,EAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC;IAC/D,OAAO,MAAM,QAAQ;SAClB,aAAa,CAAC,kBAAkB,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;SAC3E,KAAK,CAAC,CAAC,KAAY,EAAQ,EAAE;QAC5B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF;;GAEG;AACI,MAAM,gBAAgB,GAAG,KAAK,EACnC,WAAmB,EACnB,OAAe,EACf,SAAmB,EACnB,kBAA+B,EAC/B,EAAE;IACF,MAAM,KAAK,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;IAEnC,MAAM,UAAU,GAAG,kBAAkB,IAAI,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEvE,IAAI,iBAAiB,GAAG,EAAE,CAAC;IAC3B,MAAM,WAAW,GAAG,UAAU,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC9D,WAAW,CAA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,KAAK,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;IACH,CAAC;IACD,IAAA,kBAAa,EAAC,IAAI,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAEtD,MAAM,YAAY,GAAG,IAAA,WAAI,EAAC,IAAA,cAAO,EAAC,IAAI,CAAC,EAAE,SAAS,CAAC,CAAC;IAEpD,4FAA4F;IAC5F,gFAAgF;IAChF,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;QAChC,IAAA,WAAM,EAAC,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACtC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,IAAA,kBAAa,EAAC,YAAY,EAAE,aAAa,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AA3DW,QAAA,SAAS,aA2DpB","sourcesContent":["/* eslint-disable no-console */\nimport { readFileSync, rmSync, writeFileSync } from \"fs\";\nimport { dirname, join } from \"path\";\nimport * as prettier from \"prettier\";\nimport { Project, SourceFile } from \"ts-morph\";\nimport { scrubMockFileContent } from \"./scrubMockFile\";\nimport { convertEnumToConstObject, createSourceFile, getPreservedTypes, isFragment } from \"./utils\";\n\n/**\n * Resolves the repo's Prettier config by searching upward from `nxRoot` (the same\n * lookup Prettier's CLI/editor integrations use), rather than requiring an exact\n * `prettier.config.mjs` at that path. This lets generated code be formatted with the\n * repo's real config when it exists, while degrading gracefully (no formatting, no\n * throw) for workspaces without one - e.g. the ad-hoc workspaces `nx-tools-e2e`\n * scaffolds via `create-iris-app-workspace` to exercise this executor in isolation.\n */\nconst getPrettierOptions = async (nxRoot: string): Promise<prettier.Options | null> => {\n const prettierConfigPath = join(nxRoot, \"prettier.config.mjs\");\n return await prettier\n .resolveConfig(prettierConfigPath, { useCache: false, editorconfig: false })\n .catch((error: Error): null => {\n console.log(error);\n return null;\n });\n};\n\n/**\n * Generates React hooks from your graphql files.\n */\nexport const scrubFileContent = async (\n fileContent: string,\n _nxRoot: string,\n isVerbose?: boolean,\n providedSourceFile?: SourceFile\n) => {\n const start = new Date().getTime();\n\n const sourceFile = providedSourceFile ?? 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 resultFileContent = resultFileContent.replace(\n \"export type InputMaybe<T> = Maybe<T>;\",\n \"export type InputMaybe<T> = Maybe<T> | undefined;\"\n );\n\n const { preservedTypes, queryAndMutations } = getPreservedTypes(sourceFile, isVerbose);\n if (isVerbose) {\n console.log(\"PreservedTypes:\", preservedTypes);\n }\n\n sourceFile.getEnums().forEach(enumType => {\n const typeName = enumType.getName();\n if (preservedTypes.has(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.has(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, generateMocks = true) => {\n const fileContent = readFileSync(file, { encoding: \"utf-8\" });\n if (fileContent.length === 0) {\n console.warn(`File ${file} is empty`);\n }\n\n // Parse the (large) generated file into ts-morph once and share it between the main and mock scrub\n // instead of parsing it twice. Use the mock's stricter compiler options (the main scrub doesn't use\n // the TypeChecker, so they don't affect its output).\n const sharedSourceFile = new Project({\n compilerOptions: {\n noUnusedLocals: true,\n noUnusedParameters: true,\n strictNullChecks: true,\n },\n }).createSourceFile(\"tmp.ts\", fileContent);\n\n // The main scrub reads line numbers from the pristine AST, so it must run before the mock scrub\n // mutates the shared source file via insertStatements().\n const scrubbedFileContent = await scrubFileContent(fileContent, nxRoot, isVerbose, sharedSourceFile);\n\n const scrubbedMockFileContent = generateMocks\n ? await scrubMockFileContent(fileContent, nxRoot, isVerbose, sharedSourceFile)\n : undefined;\n\n const prettierOptions = await getPrettierOptions(nxRoot);\n\n let prettySrc = scrubbedFileContent;\n let prettyMockSrc = scrubbedMockFileContent;\n if (prettierOptions) {\n const formatOptions: prettier.Options = { ...prettierOptions, parser: \"typescript\" };\n try {\n prettySrc = await prettier.format(scrubbedFileContent, formatOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n if (scrubbedMockFileContent !== undefined) {\n try {\n prettyMockSrc = await prettier.format(scrubbedMockFileContent, formatOptions);\n } catch (error) {\n console.error(\"Error in prettier.format:\", error);\n }\n }\n }\n writeFileSync(file, prettySrc, { encoding: \"utf-8\" });\n\n const mockFilePath = join(dirname(file), \"mock.ts\");\n\n // A project that turns mock generation off may still carry a mock file from an earlier run,\n // so remove it instead of leaving stale helpers behind in the generated output.\n if (prettyMockSrc === undefined) {\n rmSync(mockFilePath, { force: true });\n return prettySrc;\n }\n\n if (prettyMockSrc.length > 0) {\n writeFileSync(mockFilePath, prettyMockSrc, { encoding: \"utf-8\" });\n }\n return prettySrc;\n};\n"]}
|