@atomist/automation-client
Version:
Atomist API for software low-level client
104 lines • 4.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const spawn = require("cross-spawn");
const fs = require("fs-extra");
const glob = require("glob");
const path = require("path");
const util = require("util");
const logger_1 = require("../internal/util/logger");
logger_1.LoggingConfig.format = "cli";
/**
* 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) {
const lib = path.resolve(cwd, "lib");
const src = path.resolve(cwd, "src");
if (fs.existsSync(lib)) {
return lib;
}
else if (fs.existsSync(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 = 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", "graph", "schema.cortex.json");
const schema = fs.existsSync(customSchemaLocation) ? customSchemaLocation : defaultSchemaLocation;
const gqlGenCmd = path.join(cwd, "node_modules", ".bin", "gql-gen") +
((process.platform === "win32") ? ".cmd" : "");
const gqlGenOutput = path.join(lib, "typings", "types.ts");
const gqlGenArgs = [
"--file", schema,
"--template", "typescript",
"--no-schema",
"--out", gqlGenOutput,
];
const opts = {
cwd,
env: process.env,
stdio: "inherit",
};
const graphQlGlob = `${lib}/**/*.graphql`;
const graphqlFiles = yield util.promisify(glob)(graphQlGlob);
if (graphqlFiles && graphqlFiles.length > 0) {
gqlGenArgs.push(graphQlGlob);
}
else {
logger_1.logger.info("No GraphQL files found in project, generating default types");
}
const cp = spawn(gqlGenCmd, gqlGenArgs, opts);
cp.on("exit", (code, signal) => {
if (code === 0) {
process.exit(code);
}
else if (code) {
logger_1.logger.error(`Generating GraphQL failed with non-zero status: ${code}`);
process.exit(code);
}
else {
logger_1.logger.error(`Generating GraphQL exited due to signal: ${signal}`);
process.exit(128 + 2);
}
});
cp.on("error", err => {
logger_1.logger.error(`Generating GraphQL types errored: ${err.message}`);
process.exit(2);
});
}
catch (e) {
logger_1.logger.error(`Generating GraphQL types failed: ${e.message}`);
process.exit(1);
}
});
}
main()
.catch((err) => {
logger_1.logger.error(`Unhandled exception: ${err.message}`);
process.exit(101);
});
//# sourceMappingURL=gql-gen.js.map