1 | #! /usr/bin/env node
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
9 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
10 | return new (P || (P = Promise))(function (resolve, reject) {
|
11 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
12 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
13 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
14 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
15 | });
|
16 | };
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const core_1 = require("@graphql-codegen/core");
|
19 | const typescriptPlugin = require("@graphql-codegen/typescript");
|
20 | const typescriptCompatibilityPlugin = require("@graphql-codegen/typescript-compatibility");
|
21 | const typescriptOperationsPlugin = require("@graphql-codegen/typescript-operations");
|
22 | const fs = require("fs-extra");
|
23 | const glob = require("glob");
|
24 | const graphql_1 = require("graphql");
|
25 | const graphql_toolkit_1 = require("graphql-toolkit");
|
26 | const graphql_tools_1 = require("graphql-tools");
|
27 | const path = require("path");
|
28 | const util = require("util");
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function libDir(cwd) {
|
39 | return __awaiter(this, void 0, void 0, function* () {
|
40 | const lib = path.resolve(cwd, "lib");
|
41 | const src = path.resolve(cwd, "src");
|
42 | if (yield fs.pathExists(lib)) {
|
43 | return lib;
|
44 | }
|
45 | else if (yield fs.pathExists(src)) {
|
46 | return src;
|
47 | }
|
48 | else {
|
49 | return lib;
|
50 | }
|
51 | });
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | function main() {
|
57 | return __awaiter(this, void 0, void 0, function* () {
|
58 | try {
|
59 | const cwd = process.cwd();
|
60 | const lib = yield libDir(cwd);
|
61 |
|
62 | const customSchemaLocation = path.join(lib, "graphql", "schema.json");
|
63 | const defaultSchemaLocation = path.join(cwd, "node_modules", "@atomist", "automation-client", "lib", "graph", "schema.json");
|
64 | const schema = (yield fs.pathExists(customSchemaLocation)) ? customSchemaLocation : defaultSchemaLocation;
|
65 | const transform = new graphql_tools_1.RenameTypes(name => {
|
66 | switch (name) {
|
67 | case "Fingerprint":
|
68 | case "PushImpact":
|
69 | return `Deprecated${name}`;
|
70 | default:
|
71 | return undefined;
|
72 | }
|
73 | });
|
74 | const gqlGenOutput = path.join(lib, "typings", "types.ts");
|
75 | yield fs.ensureDir(path.dirname(gqlGenOutput));
|
76 | const graphQlGlob = `${lib}/graphql/!(ingester)/*.graphql`;
|
77 | const config = {
|
78 | schema: graphql_1.parse(graphql_1.printSchema(transform.transformSchema(yield graphql_toolkit_1.loadSchema(schema)))),
|
79 | filename: gqlGenOutput,
|
80 | plugins: [{
|
81 | typescript: {
|
82 | namingConvention: {
|
83 | enumValues: "keep",
|
84 | },
|
85 | },
|
86 | }, {
|
87 | typescriptOperations: {},
|
88 | }, {
|
89 | typescriptCompatibility: {
|
90 | preResolveTypes: true,
|
91 | },
|
92 | }],
|
93 | pluginMap: {
|
94 | typescript: typescriptPlugin,
|
95 | typescriptOperations: typescriptOperationsPlugin,
|
96 | typescriptCompatibility: typescriptCompatibilityPlugin,
|
97 | },
|
98 | documents: [],
|
99 | config: {},
|
100 | };
|
101 | const graphqlFiles = yield util.promisify(glob)(graphQlGlob);
|
102 | const documents = [];
|
103 | if (graphqlFiles && graphqlFiles.length > 0) {
|
104 | for (const graphqlFile of graphqlFiles) {
|
105 | const content = (yield fs.readFile(graphqlFile)).toString();
|
106 | const document = graphql_1.parse(content);
|
107 | documents.push({
|
108 | content: document,
|
109 | filePath: graphqlFile,
|
110 | });
|
111 | }
|
112 | config.documents = documents;
|
113 |
|
114 | const typesContent = (yield core_1.codegen(config)).replace(/ ([a-zA-Z_\-0-9]+): Maybe/g, ` $1?: Maybe`);
|
115 |
|
116 | yield fs.writeFile(gqlGenOutput, `/* tslint:disable */\n\n${typesContent}`, "utf8");
|
117 | }
|
118 | else {
|
119 | console.info("No GraphQL files found in project, skipping type generation.");
|
120 | }
|
121 | process.exit(0);
|
122 | }
|
123 | catch (e) {
|
124 | console.error(`Generating GraphQL types failed: ${e.message}`);
|
125 | process.exit(1);
|
126 | }
|
127 | throw new Error("Should never get here, process.exit() called above");
|
128 | });
|
129 | }
|
130 | main()
|
131 | .catch((err) => {
|
132 | console.error(`Unhandled exception: ${err.message}`);
|
133 | process.exit(101);
|
134 | });
|
135 |
|
\ | No newline at end of file |