UNPKG

5.79 kBJavaScriptView Raw
1#! /usr/bin/env node
2"use strict";
3/*
4 * Copyright © 2018 Atomist, Inc.
5 *
6 * See LICENSE file.
7 */
8var __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};
17Object.defineProperty(exports, "__esModule", { value: true });
18const core_1 = require("@graphql-codegen/core");
19const typescriptPlugin = require("@graphql-codegen/typescript");
20const typescriptCompatibilityPlugin = require("@graphql-codegen/typescript-compatibility");
21const typescriptOperationsPlugin = require("@graphql-codegen/typescript-operations");
22const fs = require("fs-extra");
23const glob = require("glob");
24const graphql_1 = require("graphql");
25const graphql_toolkit_1 = require("graphql-toolkit");
26const graphql_tools_1 = require("graphql-tools");
27const path = require("path");
28const util = require("util");
29/* tslint:disable:no-console */
30/**
31 * Figure out whether the lib directory is named lib or src. lib is
32 * preferred, meaning if it exists, it is returned and if neither it
33 * nor src exists, it is returned.
34 *
35 * @param cwd directory to use as base for location of lib dir
36 * @return Resolved, full path to lib directory
37 */
38function 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 * Generate TypeScript typings for GraphQL schema entities.
55 */
56function main() {
57 return __awaiter(this, void 0, void 0, function* () {
58 try {
59 const cwd = process.cwd();
60 const lib = yield libDir(cwd);
61 // check if the project has a custom schema
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 // Make all properties optional to retain backwards compatibility
114 const typesContent = (yield core_1.codegen(config)).replace(/ ([a-zA-Z_\-0-9]+): Maybe/g, ` $1?: Maybe`);
115 // Write the new types.ts content back out
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}
130main()
131 .catch((err) => {
132 console.error(`Unhandled exception: ${err.message}`);
133 process.exit(101);
134});
135//# sourceMappingURL=gql-gen.js.map
\No newline at end of file