UNPKG

4.14 kBJavaScriptView Raw
1#! /usr/bin/env node
2"use strict";
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const spawn = require("cross-spawn");
13const fs = require("fs-extra");
14const glob = require("glob");
15const path = require("path");
16const util = require("util");
17const logger_1 = require("../internal/util/logger");
18logger_1.LoggingConfig.format = "cli";
19/**
20 * Figure out whether the lib directory is named lib or src. lib is
21 * preferred, meaning if it exists, it is returned and if neither it
22 * nor src exists, it is returned.
23 *
24 * @param cwd directory to use as base for location of lib dir
25 * @return Resolved, full path to lib directory
26 */
27function libDir(cwd) {
28 const lib = path.resolve(cwd, "lib");
29 const src = path.resolve(cwd, "src");
30 if (fs.existsSync(lib)) {
31 return lib;
32 }
33 else if (fs.existsSync(src)) {
34 return src;
35 }
36 else {
37 return lib;
38 }
39}
40/**
41 * Generate TypeScript typings for GraphQL schema entities.
42 */
43function main() {
44 return __awaiter(this, void 0, void 0, function* () {
45 try {
46 const cwd = process.cwd();
47 const lib = libDir(cwd);
48 // check if the project has a custom schema
49 const customSchemaLocation = path.join(lib, "graphql", "schema.json");
50 const defaultSchemaLocation = path.join(cwd, "node_modules", "@atomist", "automation-client", "graph", "schema.cortex.json");
51 const schema = fs.existsSync(customSchemaLocation) ? customSchemaLocation : defaultSchemaLocation;
52 const gqlGenCmd = path.join(cwd, "node_modules", ".bin", "gql-gen") +
53 ((process.platform === "win32") ? ".cmd" : "");
54 const gqlGenOutput = path.join(lib, "typings", "types.ts");
55 const gqlGenArgs = [
56 "--file", schema,
57 "--template", "typescript",
58 "--no-schema",
59 "--out", gqlGenOutput,
60 ];
61 const opts = {
62 cwd,
63 env: process.env,
64 stdio: "inherit",
65 };
66 const graphQlGlob = `${lib}/**/*.graphql`;
67 const graphqlFiles = yield util.promisify(glob)(graphQlGlob);
68 if (graphqlFiles && graphqlFiles.length > 0) {
69 gqlGenArgs.push(graphQlGlob);
70 }
71 else {
72 logger_1.logger.info("No GraphQL files found in project, generating default types");
73 }
74 const cp = spawn(gqlGenCmd, gqlGenArgs, opts);
75 cp.on("exit", (code, signal) => {
76 if (code === 0) {
77 process.exit(code);
78 }
79 else if (code) {
80 logger_1.logger.error(`Generating GraphQL failed with non-zero status: ${code}`);
81 process.exit(code);
82 }
83 else {
84 logger_1.logger.error(`Generating GraphQL exited due to signal: ${signal}`);
85 process.exit(128 + 2);
86 }
87 });
88 cp.on("error", err => {
89 logger_1.logger.error(`Generating GraphQL types errored: ${err.message}`);
90 process.exit(2);
91 });
92 }
93 catch (e) {
94 logger_1.logger.error(`Generating GraphQL types failed: ${e.message}`);
95 process.exit(1);
96 }
97 });
98}
99main()
100 .catch((err) => {
101 logger_1.logger.error(`Unhandled exception: ${err.message}`);
102 process.exit(101);
103});
104//# sourceMappingURL=gql-gen.js.map
\No newline at end of file