@trackunit/react-graphql-tools 0.0.13 → 0.0.15
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 +7 -0
- package/package.json +2 -2
- package/src/executors/createHooks/executor.js +4 -2
- package/src/executors/createHooks/executor.js.map +1 -1
- package/src/executors/createHooks/generateHooks.js +19 -7
- package/src/executors/createHooks/generateHooks.js.map +1 -1
- package/src/executors/createHooks/scrubFile.js +138 -0
- package/src/executors/createHooks/scrubFile.js.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [0.0.15](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.14...react-graphql-tools/0.0.15) (2023-05-12)
|
|
6
|
+
|
|
7
|
+
## [0.0.14](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.13...react-graphql-tools/0.0.14) (2023-05-11)
|
|
8
|
+
|
|
9
|
+
### Dependency Updates
|
|
10
|
+
|
|
11
|
+
* `test-setup` updated to version `0.0.1`
|
|
5
12
|
## [0.0.13](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.12...react-graphql-tools/0.0.13) (2023-05-04)
|
|
6
13
|
|
|
7
14
|
## [0.0.12](https://github.com/Trackunit/manager/compare/react-graphql-tools/0.0.11...react-graphql-tools/0.0.12) (2023-05-04)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-graphql-tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"executors": "./executors.json",
|
|
6
6
|
"repository": "https://github.com/Trackunit/manager",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"@graphql-codegen/fragment-matcher": "4.0.1",
|
|
11
11
|
"@graphql-codegen/typescript-operations": "3.0.4",
|
|
12
12
|
"@graphql-codegen/typescript-react-apollo": "3.3.7",
|
|
13
|
-
"@react-google-maps/api": "2.
|
|
13
|
+
"@react-google-maps/api": "2.18.1",
|
|
14
14
|
"@react-spring/web": "9.6.0",
|
|
15
15
|
"graphql": "15.8.0",
|
|
16
16
|
"i18next": "21.10.0",
|
|
@@ -21,10 +21,12 @@ const CreateGraphqlHooks = (_unused, context) => tslib_1.__awaiter(void 0, void
|
|
|
21
21
|
const fullLibRoot = `${nxRoot}/${libRoot}`;
|
|
22
22
|
const filePath = (0, path_1.join)(__dirname, "..", "..", "..", "..", "..", "..", "libs", "api", "public-gql-schema", "src", "public-schema.gql");
|
|
23
23
|
if ((0, fs_1.existsSync)(filePath)) {
|
|
24
|
-
yield (0, generateHooks_1.generateHooks)(filePath, fullLibRoot + "/src");
|
|
24
|
+
yield (0, generateHooks_1.generateHooks)(filePath, fullLibRoot + "/src", context.root, { isVerbose: context.isVerbose });
|
|
25
25
|
}
|
|
26
26
|
else {
|
|
27
|
-
yield (0, generateHooks_1.generateHooks)("https://iris.trackunit.com/api/graphql/", fullLibRoot + "/src"
|
|
27
|
+
yield (0, generateHooks_1.generateHooks)("https://iris.trackunit.com/api/graphql/", fullLibRoot + "/src", context.root, {
|
|
28
|
+
isVerbose: context.isVerbose,
|
|
29
|
+
});
|
|
28
30
|
}
|
|
29
31
|
return { success: 1 };
|
|
30
32
|
});
|
|
@@ -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,mDAAgD;AAEhD;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAAO,OAAgB,EAAE,OAAwB,EAAE,EAAE;IACrF,MAAM,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,OAAO,CAAC,sBAAsB,EAAE;QACnC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,WAAY,CAAC,CAAC,IAAI,CAAC;IACnF,MAAM,WAAW,GAAG,GAAG,MAAM,IAAI,OAAO,EAAE,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAA,WAAI,EACnB,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,KAAK,EACL,mBAAmB,EACnB,KAAK,EACL,mBAAmB,CACpB,CAAC;IACF,IAAI,IAAA,eAAU,EAAC,QAAQ,CAAC,EAAE;QACxB,MAAM,IAAA,6BAAa,EAAC,QAAQ,EAAE,WAAW,GAAG,MAAM,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/executor.ts"],"names":[],"mappings":";;;;AACA,2BAAgC;AAChC,+BAA4B;AAC5B,mDAAgD;AAEhD;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAAO,OAAgB,EAAE,OAAwB,EAAE,EAAE;IACrF,MAAM,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,OAAO,CAAC,sBAAsB,EAAE;QACnC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,WAAY,CAAC,CAAC,IAAI,CAAC;IACnF,MAAM,WAAW,GAAG,GAAG,MAAM,IAAI,OAAO,EAAE,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAA,WAAI,EACnB,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,KAAK,EACL,mBAAmB,EACnB,KAAK,EACL,mBAAmB,CACpB,CAAC;IACF,IAAI,IAAA,eAAU,EAAC,QAAQ,CAAC,EAAE;QACxB,MAAM,IAAA,6BAAa,EAAC,QAAQ,EAAE,WAAW,GAAG,MAAM,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;KACrG;SAAM;QACL,MAAM,IAAA,6BAAa,EAAC,yCAAyC,EAAE,WAAW,GAAG,MAAM,EAAE,OAAO,CAAC,IAAI,EAAE;YACjG,SAAS,EAAE,OAAO,CAAC,SAAS;SAC7B,CAAC,CAAC;KACJ;IACD,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;AACxB,CAAC,CAAA,CAAC;AA7BW,QAAA,kBAAkB,sBA6B7B;AAEF,kBAAe,0BAAkB,CAAC","sourcesContent":["import { ExecutorContext } from \"@nrwl/devkit\";\nimport { existsSync } from \"fs\";\nimport { join } from \"path\";\nimport { generateHooks } from \"./generateHooks\";\n\n/**\n * This executor generates react hooks for all graphql files.\n *\n * @param _unused unused props from nx\n * @param context The execution context for this executor\n * @returns {Promise<{ success: number; }>} success when finished\n */\nexport const CreateGraphqlHooks = async (_unused: unknown, context: ExecutorContext) => {\n const nxRoot = `${context.root}`;\n if (!context.projectsConfigurations) {\n throw new Error(\"context projectsConfigurations is required\");\n }\n const libRoot = context.projectsConfigurations.projects[context.projectName!].root;\n const fullLibRoot = `${nxRoot}/${libRoot}`;\n const filePath = join(\n __dirname,\n \"..\",\n \"..\",\n \"..\",\n \"..\",\n \"..\",\n \"..\",\n \"libs\",\n \"api\",\n \"public-gql-schema\",\n \"src\",\n \"public-schema.gql\"\n );\n if (existsSync(filePath)) {\n await generateHooks(filePath, fullLibRoot + \"/src\", context.root, { isVerbose: context.isVerbose });\n } else {\n await generateHooks(\"https://iris.trackunit.com/api/graphql/\", fullLibRoot + \"/src\", context.root, {\n isVerbose: context.isVerbose,\n });\n }\n return { success: 1 };\n};\n\nexport default CreateGraphqlHooks;\n"]}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateHooks = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const cli_1 = require("@graphql-codegen/cli");
|
|
6
|
+
const scrubFile_1 = require("./scrubFile");
|
|
6
7
|
/**
|
|
7
8
|
* Generates React hooks from your graphql files.
|
|
8
9
|
*
|
|
@@ -14,20 +15,27 @@ const cli_1 = require("@graphql-codegen/cli");
|
|
|
14
15
|
* @param options.plugins the options list of plugins to use.
|
|
15
16
|
* @param options.overridePluginConfig the options list of overridePluginConfig.
|
|
16
17
|
*/
|
|
17
|
-
const generateHooks = (schemaUrl, srcPath, options
|
|
18
|
-
name: "generated/graphql-api.tsx",
|
|
19
|
-
graphqlGlob: "*.graphql",
|
|
20
|
-
plugins: ["typescript", "typescript-operations", "typescript-react-apollo", "fragment-matcher"],
|
|
21
|
-
}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const generateHooks = (schemaUrl, srcPath, nxRoot, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
options = Object.assign({ name: "generated/graphql-api.tsx", graphqlGlob: "*.graphql", plugins: ["typescript", "typescript-operations", "typescript-react-apollo"] }, options);
|
|
22
20
|
const codegenOptions = {
|
|
23
21
|
generates: {},
|
|
24
22
|
};
|
|
23
|
+
const outputFile = `${srcPath}/${options.name}`;
|
|
25
24
|
codegenOptions.generates[`${srcPath}/${options.name}`] = Object.assign({ schema: schemaUrl, documents: `${srcPath}/**/${options.graphqlGlob}`, config: {
|
|
26
25
|
withHooks: true,
|
|
27
26
|
withHOC: false,
|
|
28
27
|
withComponent: false,
|
|
28
|
+
declarationKind: {
|
|
29
|
+
// This makes scrubbing a lot easier! marking types as classes (then we remove those)
|
|
30
|
+
type: "class",
|
|
31
|
+
// This makes scrubbing a lot easier! marking input as interfaces (then we keep these)
|
|
32
|
+
input: "interface",
|
|
33
|
+
},
|
|
34
|
+
// We need to do this later after scrubbing
|
|
35
|
+
enumsAsConst: false,
|
|
36
|
+
noExport: true,
|
|
37
|
+
avoidOptionals: true,
|
|
29
38
|
noNamespaces: true,
|
|
30
|
-
declarationKind: "interface",
|
|
31
39
|
namingConvention: {
|
|
32
40
|
typeNames: "keep",
|
|
33
41
|
enumValues: "pascal-case#pascalCase",
|
|
@@ -37,7 +45,11 @@ const generateHooks = (schemaUrl, srcPath, options = {
|
|
|
37
45
|
DateTime: "Date",
|
|
38
46
|
},
|
|
39
47
|
}, plugins: options.plugins }, options.overridePluginConfig);
|
|
40
|
-
yield (0, cli_1.generate)(codegenOptions, true)
|
|
48
|
+
yield (0, cli_1.generate)(codegenOptions, true)
|
|
49
|
+
.then(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
yield (0, scrubFile_1.scrubFile)(outputFile, nxRoot, options.isVerbose);
|
|
51
|
+
}))
|
|
52
|
+
.catch(e => {
|
|
41
53
|
// eslint-disable-next-line no-console
|
|
42
54
|
console.error("❌ There was an error generating GraphQL types", e);
|
|
43
55
|
process.exit(1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateHooks.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/generateHooks.ts"],"names":[],"mappings":";;;;AAAA,8CAA+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,2CAAwC;AAExC;;;;;;;;;;GAUG;AACI,MAAM,aAAa,GAAG,CAC3B,SAAiB,EACjB,OAAe,EACf,MAAc,EACd,OAMC,EACD,EAAE;IACF,OAAO,mBACL,IAAI,EAAE,2BAA2B,EACjC,WAAW,EAAE,WAAW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CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACP,CAAC,CAAA,CAAC;AA/DW,QAAA,aAAa,iBA+DxB","sourcesContent":["import { CodegenConfig, generate } from \"@graphql-codegen/cli\";\nimport { scrubFile } from \"./scrubFile\";\n\n/**\n * Generates React hooks from your graphql files.\n *\n * @param schemaUrl schema url to use.\n * @param srcPath the srcPath to look for options glob.\n * @param options the options to use.\n * @param options.name the name of the output file.\n * @param options.graphqlGlob the options glob allows you to override the default.\n * @param options.plugins the options list of plugins to use.\n * @param options.overridePluginConfig the options list of overridePluginConfig.\n */\nexport const generateHooks = async (\n schemaUrl: string,\n srcPath: string,\n nxRoot: string,\n options: {\n isVerbose?: boolean;\n name?: string;\n graphqlGlob?: string;\n plugins?: string[];\n overridePluginConfig?: CodegenConfig;\n }\n) => {\n options = {\n name: \"generated/graphql-api.tsx\",\n graphqlGlob: \"*.graphql\",\n plugins: [\"typescript\", \"typescript-operations\", \"typescript-react-apollo\"],\n ...options,\n };\n\n const codegenOptions: CodegenConfig = {\n generates: {},\n };\n const outputFile = `${srcPath}/${options.name}`;\n codegenOptions.generates[`${srcPath}/${options.name}`] = {\n schema: schemaUrl,\n documents: `${srcPath}/**/${options.graphqlGlob}`,\n config: {\n withHooks: true,\n withHOC: false,\n withComponent: false,\n declarationKind: {\n // This makes scrubbing a lot easier! marking types as classes (then we remove those)\n type: \"class\",\n // This makes scrubbing a lot easier! marking input as interfaces (then we keep these)\n input: \"interface\",\n },\n // We need to do this later after scrubbing\n enumsAsConst: false,\n noExport: true,\n avoidOptionals: true,\n noNamespaces: true,\n namingConvention: {\n typeNames: \"keep\",\n enumValues: \"pascal-case#pascalCase\",\n },\n scalars: {\n Date: \"Date\",\n DateTime: \"Date\",\n },\n },\n plugins: options.plugins,\n ...options.overridePluginConfig,\n };\n\n await generate(codegenOptions, true)\n .then(async () => {\n await scrubFile(outputFile, nxRoot, options.isVerbose);\n })\n .catch(e => {\n // eslint-disable-next-line no-console\n console.error(\"❌ There was an error generating GraphQL types\", e);\n process.exit(1);\n });\n};\n"]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scrubFile = exports.scrubFileContent = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const prettier = tslib_1.__importStar(require("prettier"));
|
|
8
|
+
const ts_morph_1 = require("ts-morph");
|
|
9
|
+
/**
|
|
10
|
+
* Removes unused identifiers from the source file.
|
|
11
|
+
*
|
|
12
|
+
* This does more then sourceFile.fixUnusedIdentifiers(diagnostic) as it also removes unused enums and interfaces.
|
|
13
|
+
*
|
|
14
|
+
* @param sourceFile The source file to remove unused identifiers from.
|
|
15
|
+
*/
|
|
16
|
+
const removeUnusedIdentifiers = (sourceFile) => {
|
|
17
|
+
const diagnostics = sourceFile.getPreEmitDiagnostics();
|
|
18
|
+
diagnostics.forEach(diagnostic => {
|
|
19
|
+
var _a;
|
|
20
|
+
const messageText = diagnostic.getMessageText();
|
|
21
|
+
if (typeof messageText === "string" && messageText.startsWith("'")) {
|
|
22
|
+
const identifierName = (_a = messageText.match(/'([^']+)'/)) === null || _a === void 0 ? void 0 : _a[1];
|
|
23
|
+
if (identifierName) {
|
|
24
|
+
if (messageText.includes("is declared but never used")) {
|
|
25
|
+
const identifier = sourceFile.getVariableDeclaration(identifierName) ||
|
|
26
|
+
sourceFile.getInterface(identifierName) ||
|
|
27
|
+
sourceFile.getEnum(identifierName) ||
|
|
28
|
+
sourceFile.getClass(identifierName) ||
|
|
29
|
+
sourceFile.getTypeAlias(identifierName);
|
|
30
|
+
if (!identifier) {
|
|
31
|
+
// eslint-disable-next-line no-console
|
|
32
|
+
console.log("Could not find: ", messageText);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
identifier.remove();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const convertEnumToConstObject = (enumDeclaration) => {
|
|
43
|
+
const enumName = enumDeclaration.getName();
|
|
44
|
+
const members = enumDeclaration.getMembers();
|
|
45
|
+
const properties = members.map(member => {
|
|
46
|
+
const name = member.getName();
|
|
47
|
+
const value = member.getValue();
|
|
48
|
+
return `${name}: '${value}'`;
|
|
49
|
+
});
|
|
50
|
+
const constObjectText = `export const ${enumName} = {\n ${properties.join(",\n ")},\n} as const;\nexport type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}];`;
|
|
51
|
+
// Replace the EnumDeclaration with the const object text
|
|
52
|
+
enumDeclaration.replaceWithText(constObjectText);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Generates React hooks from your graphql files.
|
|
56
|
+
*/
|
|
57
|
+
const scrubFileContent = (fileContent, nxRoot, isVerbose) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
58
|
+
const start = new Date().getTime();
|
|
59
|
+
const project = new ts_morph_1.Project({
|
|
60
|
+
compilerOptions: {
|
|
61
|
+
noUnusedLocals: true,
|
|
62
|
+
noUnusedParameters: true,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const generatedFile = project.createSourceFile("tmp.ts", fileContent);
|
|
66
|
+
generatedFile.insertStatements(0, "/* eslint-disable @typescript-eslint/no-explicit-any */");
|
|
67
|
+
generatedFile.insertStatements(1, "/* eslint-disable @typescript-eslint/array-type */");
|
|
68
|
+
// all return object types are defined as classes, where input types are defined as interfaces
|
|
69
|
+
// therefore we can remove all classes.
|
|
70
|
+
generatedFile.getClasses().forEach(node => {
|
|
71
|
+
node.remove();
|
|
72
|
+
});
|
|
73
|
+
let lastWidth = 0;
|
|
74
|
+
do {
|
|
75
|
+
lastWidth = generatedFile.getFullWidth();
|
|
76
|
+
removeUnusedIdentifiers(generatedFile);
|
|
77
|
+
if (isVerbose) {
|
|
78
|
+
console.log("Cleanup", lastWidth, generatedFile.getFullWidth());
|
|
79
|
+
}
|
|
80
|
+
} while (lastWidth !== generatedFile.getFullWidth());
|
|
81
|
+
generatedFile.getInterfaces().forEach(node => {
|
|
82
|
+
node.setIsExported(true);
|
|
83
|
+
});
|
|
84
|
+
generatedFile.getTypeAliases().forEach(node => {
|
|
85
|
+
node.setIsExported(true);
|
|
86
|
+
});
|
|
87
|
+
generatedFile.getVariableStatements().forEach(variableStatement => {
|
|
88
|
+
variableStatement.setIsExported(true);
|
|
89
|
+
});
|
|
90
|
+
generatedFile.getEnums().forEach(node => {
|
|
91
|
+
node.setIsExported(true);
|
|
92
|
+
convertEnumToConstObject(node);
|
|
93
|
+
});
|
|
94
|
+
if (isVerbose) {
|
|
95
|
+
const end = new Date().getTime();
|
|
96
|
+
const time = end - start;
|
|
97
|
+
// eslint-disable-next-line no-console
|
|
98
|
+
console.log("Execution time: " + time);
|
|
99
|
+
}
|
|
100
|
+
return generatedFile.getFullText();
|
|
101
|
+
});
|
|
102
|
+
exports.scrubFileContent = scrubFileContent;
|
|
103
|
+
/**
|
|
104
|
+
* Scrubs the file content.
|
|
105
|
+
*
|
|
106
|
+
* @param file The file to scrub.
|
|
107
|
+
*/
|
|
108
|
+
const scrubFile = (file, nxRoot, isVerbose) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
109
|
+
const scrubbedFileContent = yield (0, exports.scrubFileContent)((0, fs_1.readFileSync)(file, { encoding: "utf-8" }), nxRoot, isVerbose);
|
|
110
|
+
let options = null;
|
|
111
|
+
try {
|
|
112
|
+
options = prettier
|
|
113
|
+
.resolveConfig((0, path_1.join)(nxRoot, ".prettierrc"), {
|
|
114
|
+
useCache: false,
|
|
115
|
+
editorconfig: false,
|
|
116
|
+
config: (0, path_1.join)(nxRoot, ".prettierrc"),
|
|
117
|
+
})
|
|
118
|
+
// eslint-disable-next-line no-console
|
|
119
|
+
.catch((error) => console.log(error));
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
// eslint-disable-next-line no-console
|
|
123
|
+
console.log(e);
|
|
124
|
+
}
|
|
125
|
+
if (!options) {
|
|
126
|
+
throw new Error("Could not find prettier config");
|
|
127
|
+
}
|
|
128
|
+
const prettierOptions = yield options;
|
|
129
|
+
let prettySrc = scrubbedFileContent;
|
|
130
|
+
if (prettierOptions) {
|
|
131
|
+
prettierOptions.parser = "typescript";
|
|
132
|
+
prettySrc = prettier.format(scrubbedFileContent, prettierOptions);
|
|
133
|
+
}
|
|
134
|
+
(0, fs_1.writeFileSync)(file, prettySrc, { encoding: "utf-8" });
|
|
135
|
+
return prettySrc;
|
|
136
|
+
});
|
|
137
|
+
exports.scrubFile = scrubFile;
|
|
138
|
+
//# sourceMappingURL=scrubFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrubFile.js","sourceRoot":"","sources":["../../../../../../../libs/react/graphql-tools/src/executors/createHooks/scrubFile.ts"],"names":[],"mappings":";;;;AAAA,2BAAiD;AACjD,+BAA4B;AAC5B,2DAAqC;AACrC,uCAAgE;AAEhE;;;;;;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,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AAK,CAAC,gCAAgC,CAAC,CAAC;KACnD;IACD,MAAM,eAAe,GAAG,MAAM,OAAO,CAAC;IAEtC,IAAI,SAAS,GAAG,mBAAmB,CAAC;IACpC,IAAI,eAAe,EAAE;QACnB,eAAe,CAAC,MAAM,GAAG,YAAY,CAAC;QACtC,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;KACnE;IAED,IAAA,kBAAa,EAAC,IAAI,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IACtD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAA,CAAC;AA9BW,QAAA,SAAS,aA8BpB","sourcesContent":["import { readFileSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport * as prettier from \"prettier\";\nimport { EnumDeclaration, Project, SourceFile } from \"ts-morph\";\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 constObjectText = `export const ${enumName} = {\\n ${properties.join(\n \",\\n \"\n )},\\n} as const;\\nexport type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}];`;\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 // all return object types are defined as classes, where input types are defined as interfaces\n // therefore we can remove all classes.\n generatedFile.getClasses().forEach(node => {\n node.remove();\n });\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.getInterfaces().forEach(node => {\n node.setIsExported(true);\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 // eslint-disable-next-line no-console\n console.log(\"Execution time: \" + time);\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 scrubbedFileContent = await scrubFileContent(readFileSync(file, { encoding: \"utf-8\" }), 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 // eslint-disable-next-line no-console\n .catch((error: Error) => console.log(error));\n } catch (e) {\n // eslint-disable-next-line no-console\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 prettySrc = prettier.format(scrubbedFileContent, prettierOptions);\n }\n\n writeFileSync(file, prettySrc, { encoding: \"utf-8\" });\n return prettySrc;\n};\n"]}
|