@atomist/automation-client
Version:
Atomist API for software low-level client
134 lines • 5.75 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, Inc.
*
* See LICENSE file.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@graphql-codegen/core");
const typescriptPlugin = require("@graphql-codegen/typescript");
const typescriptCompatibilityPlugin = require("@graphql-codegen/typescript-compatibility");
const typescriptOperationsPlugin = require("@graphql-codegen/typescript-operations");
const fg = require("fast-glob");
const fs = require("fs-extra");
const graphql_1 = require("graphql");
const graphql_toolkit_1 = require("graphql-toolkit");
const graphql_tools_1 = require("graphql-tools");
const path = require("path");
/* tslint:disable:no-console */
/**
* Figure out whether the lib directory is named lib or src. lib is
* preferred, meaning if it exists, it is returned and if neither it
* nor src exists, it is returned.
*
* @param cwd directory to use as base for location of lib dir
* @return Resolved, full path to lib directory
*/
function libDir(cwd) {
return __awaiter(this, void 0, void 0, function* () {
const lib = path.resolve(cwd, "lib");
const src = path.resolve(cwd, "src");
if (yield fs.pathExists(lib)) {
return lib;
}
else if (yield fs.pathExists(src)) {
return src;
}
else {
return lib;
}
});
}
/**
* Generate TypeScript typings for GraphQL schema entities.
*/
function main() {
return __awaiter(this, void 0, void 0, function* () {
try {
const cwd = process.cwd();
const lib = yield libDir(cwd);
// check if the project has a custom schema
const customSchemaLocation = path.join(lib, "graphql", "schema.json");
const defaultSchemaLocation = path.join(cwd, "node_modules", "@atomist", "automation-client", "lib", "graph", "schema.json");
const schema = (yield fs.pathExists(customSchemaLocation)) ? customSchemaLocation : defaultSchemaLocation;
const transform = new graphql_tools_1.RenameTypes(name => {
switch (name) {
case "Fingerprint":
case "PushImpact":
return `Deprecated${name}`;
default:
return undefined;
}
});
const gqlGenOutput = path.join(lib, "typings", "types.ts");
const graphQlGlob = `${lib}/graphql/!(ingester)/*.graphql`;
const config = {
schema: graphql_1.parse(graphql_1.printSchema(transform.transformSchema(yield graphql_toolkit_1.loadSchema(schema)))),
filename: gqlGenOutput,
plugins: [{
typescript: {
namingConvention: {
enumValues: "keep",
},
},
}, {
typescriptOperations: {},
}, {
typescriptCompatibility: {
preResolveTypes: true,
},
}],
pluginMap: {
typescript: typescriptPlugin,
typescriptOperations: typescriptOperationsPlugin,
typescriptCompatibility: typescriptCompatibilityPlugin,
},
documents: [],
config: {},
};
const graphqlFiles = yield fg(graphQlGlob);
const documents = [];
if (graphqlFiles && graphqlFiles.length > 0) {
for (const graphqlFile of graphqlFiles) {
const content = (yield fs.readFile(graphqlFile)).toString();
const document = graphql_1.parse(content);
documents.push({
content: document,
filePath: graphqlFile,
});
}
config.documents = documents;
yield fs.ensureDir(path.dirname(gqlGenOutput));
// Make all properties optional to retain backwards compatibility
const typesContent = (yield core_1.codegen(config)).replace(/ ([a-zA-Z_\-0-9]+): Maybe/g, ` $1?: Maybe`);
// Write the new types.ts content back out
yield fs.writeFile(gqlGenOutput, `/* tslint:disable */\n\n${typesContent}`, "utf8");
}
else {
console.info("No GraphQL files found in project, skipping type generation.");
}
process.exit(0);
}
catch (e) {
console.error(`Generating GraphQL types failed: ${e.message}`);
process.exit(1);
}
throw new Error("Should never get here, process.exit() called above");
});
}
main()
.catch((err) => {
console.error(`Unhandled exception: ${err.message}`);
process.exit(101);
});
//# sourceMappingURL=gql-gen.js.map