1 | #! /usr/bin/env node
|
2 | "use strict";
|
3 | var __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 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const spawn = require("cross-spawn");
|
13 | const fs = require("fs-extra");
|
14 | const glob = require("glob");
|
15 | const path = require("path");
|
16 | const util = require("util");
|
17 | const logger_1 = require("../internal/util/logger");
|
18 | logger_1.LoggingConfig.format = "cli";
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function 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 |
|
42 |
|
43 | function main() {
|
44 | return __awaiter(this, void 0, void 0, function* () {
|
45 | try {
|
46 | const cwd = process.cwd();
|
47 | const lib = libDir(cwd);
|
48 |
|
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 | }
|
99 | main()
|
100 | .catch((err) => {
|
101 | logger_1.logger.error(`Unhandled exception: ${err.message}`);
|
102 | process.exit(101);
|
103 | });
|
104 |
|
\ | No newline at end of file |