UNPKG

graphql-cli-prepare

Version:

Plugin for graphql-cli to bundle schemas and generate bindings

298 lines 14.9 kB
"use strict"; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var chalk_1 = require("chalk"); var fs = require("fs-extra"); var graphql_import_1 = require("graphql-import"); var graphql_static_binding_1 = require("graphql-static-binding"); var lodash_1 = require("lodash"); var path = require("path"); var Prepare = /** @class */ (function () { function Prepare(context, argv) { var _this = this; this.context = context; this.argv = argv; this.projectDisplayName = function () { return chalk_1.default.green(_this.projectName); }; } Prepare.prototype.handle = function () { return __awaiter(this, void 0, void 0, function () { var _a, projects, _i, _b, projectName, project; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = this; return [4 /*yield*/, this.context.getConfig() // Get projects ]; case 1: _a.config = _c.sent(); projects = this.getProjectConfig(); // Process each project for (_i = 0, _b = Object.keys(projects); _i < _b.length; _i++) { projectName = _b[_i]; project = projects[projectName]; this.setCurrentProject(project, projectName); if (this.argv.bundle) { this.bundle(); } if (this.argv.bindings) { this.bindings(); } this.save(); } return [2 /*return*/]; } }); }); }; Prepare.prototype.setCurrentProject = function (project, projectName) { this.project = project; this.projectName = projectName; this.bundleExtensionConfig = undefined; }; Prepare.prototype.bindings = function () { var bindingExtensionConfig; if (this.argv.project || (!this.argv.project && (lodash_1.has(this.project.config, 'extensions.prepare-binding') || lodash_1.has(this.project.config, 'extensions.binding')))) { this.context.spinner.start("Generating bindings for project " + this.projectDisplayName() + "..."); bindingExtensionConfig = this.processBindings(this.bundleExtensionConfig ? this.bundleExtensionConfig['prepare-bundle'] : undefined); lodash_1.merge(this.project.extensions, bindingExtensionConfig); this.context.spinner.succeed("Bindings for project " + this.projectDisplayName() + " written to " + chalk_1.default.green(bindingExtensionConfig['prepare-binding'].output)); } else if (this.argv.verbose) { this.context.spinner.info("Binding not configured for project " + this.projectDisplayName() + ". Skipping"); } }; Prepare.prototype.bundle = function () { if (this.argv.project || (!this.argv.project && (lodash_1.has(this.project.config, 'extensions.prepare-bundle') || lodash_1.has(this.project.config, 'extensions.bundle')))) { this.context.spinner.start("Processing schema imports for project " + this.projectDisplayName() + "..."); this.bundleExtensionConfig = this.processBundle(); lodash_1.merge(this.project.extensions, this.bundleExtensionConfig); this.context.spinner.succeed("Bundled schema for project " + this.projectDisplayName() + " written to " + chalk_1.default.green(this.bundleExtensionConfig['prepare-bundle'])); } else if (this.argv.verbose) { this.context.spinner.info("Bundling not configured for project " + this.projectDisplayName() + ". Skipping"); } }; Prepare.prototype.save = function () { if (this.argv.save) { var configFile = path.basename(this.config.configPath); this.context.spinner.start("Saving configuration for project " + this.projectDisplayName() + " to " + chalk_1.default.green(configFile) + "..."); this.saveConfig(); this.context.spinner.succeed("Configuration for project " + this.projectDisplayName() + " saved to " + chalk_1.default.green(configFile)); } }; Prepare.prototype.getProjectConfig = function () { var _this = this; var projects; if (this.argv.project) { if (Array.isArray(this.argv.project)) { projects = {}; this.argv.project.map(function (p) { return lodash_1.merge(projects, (_a = {}, _a[p] = _this.config.getProjectConfig(p), _a)); var _a; }); } else { // Single project mode projects = (_a = {}, _a[this.argv.project] = this.config.getProjectConfig(this.argv.project), _a); } } else { // Process all projects projects = this.config.getProjects(); } if (!projects) { throw new Error('No projects defined in config file'); } return projects; var _a; }; Prepare.prototype.processBundle = function () { var outputPath = this.determineBundleOutputPath(); var schemaPath = this.determineSchemaPath(); var finalSchema = graphql_import_1.importSchema(schemaPath); fs.writeFileSync(outputPath, finalSchema, { flag: 'w' }); return { 'prepare-bundle': outputPath }; }; Prepare.prototype.processBindings = function (schemaPath) { var generator = this.determineGenerator(); // TODO: This does not support custom generators var extension = generator.endsWith('ts') ? 'ts' : 'js'; var outputPath = this.determineBindingOutputPath(extension); var schema = this.determineInputSchema(schemaPath); var schemaContents = graphql_import_1.importSchema(schema); var finalSchema = graphql_static_binding_1.generateCode(schemaContents, generator); fs.writeFileSync(outputPath, finalSchema, { flag: 'w' }); return { 'prepare-binding': { output: outputPath, generator: generator } }; }; Prepare.prototype.saveConfig = function () { if (lodash_1.has(this.project.config, 'extensions.bundle')) { delete this.project.config.extensions.bundle; } if (lodash_1.has(this.project.config, 'extensions.binding')) { delete this.project.config.extensions.binding; } this.config.saveConfig(this.project.config, this.projectName); }; /** * Determine input schema path for binding. It uses the resulting schema from bundling (if available), * then looks at bundle extension (in case bundling ran before), then takes the project schemaPath. * Also checks if the file exists, otherwise it throws and error. * * @param {(string | undefined)} schemaPath Schema path from bundling * @returns {string} Input schema path to be used for binding generatio. */ Prepare.prototype.determineInputSchema = function (schemaPath) { var bundleDefined = lodash_1.has(this.project.config, 'extensions.prepare-bundle.output'); var oldBundleDefined = lodash_1.has(this.project.config, 'extensions.bundle.output'); // schemaPath is only set when bundle ran if (!schemaPath) { if (bundleDefined) { // Otherwise, use bundle output schema if defined schemaPath = lodash_1.get(this.project.config, 'extensions.prepare-bundle.output'); } else if (oldBundleDefined) { schemaPath = lodash_1.get(this.project.config, 'extensions.bundle.output'); } else if (this.project.schemaPath) { // Otherwise, use project schemaPath schemaPath = this.project.schemaPath; } else { throw new Error("Input schema cannot be determined."); } } if (fs.existsSync(schemaPath)) { return schemaPath; } else { throw new Error("Schema '" + schemaPath + "' not found." + (bundleDefined ? ' Did you run bundle first?' : '')); } }; /** * Determine input schema path for bundling. * * @returns {string} Input schema path for bundling */ Prepare.prototype.determineSchemaPath = function () { if (this.project.schemaPath) { return this.project.schemaPath; } throw new Error("No schemaPath defined for project '" + this.projectName + "' in config file."); }; /** * Determine generator. Provided generator takes precedence over value from config * * @param {string} generator Command line parameter for generator * @returns {string} Generator to be used */ Prepare.prototype.determineGenerator = function () { if (this.argv.generator) { return this.argv.generator; } if (lodash_1.has(this.project.config, 'extensions.binding.generator')) { if (!this.argv.save) { this.context.spinner.warn("Deprecated extension key 'binding.generator' found in config file. Use '--save' to update to 'prepare-binding.generator'."); } return lodash_1.get(this.project.config, 'extensions.binding.generator'); } if (lodash_1.has(this.project.config, 'extensions.prepare-binding.generator')) { return lodash_1.get(this.project.config, 'extensions.prepare-binding.generator'); } throw new Error('Generator cannot be determined. No existing configuration found and no generator parameter specified.'); }; /** * Determine output path for binding. Provided path takes precedence over value from config * * @param {string} extension File extension for output file * @returns Output path */ Prepare.prototype.determineBindingOutputPath = function (extension) { var outputPath; if (this.argv.output) { outputPath = path.join(this.argv.output, this.projectName + "." + extension); } else if (lodash_1.has(this.project.config, "extensions.binding.output")) { if (!this.argv.save) { this.context.spinner.warn("Deprecated extension key 'binding.output' found in config file. Use '--save' to update to 'prepare-binding.output'."); } outputPath = lodash_1.get(this.project.config, "extensions.binding.output"); } else if (lodash_1.has(this.project.config, "extensions.prepare-binding.output")) { outputPath = lodash_1.get(this.project.config, "extensions.prepare-binding.output"); } else { throw new Error('Output path cannot be determined. No existing configuration found and no output parameter specified.'); } fs.ensureDirSync(path.dirname(outputPath)); return outputPath; }; /** * Determine output path for bundle. Provided path takes precedence over value from config * * @returns Output path */ Prepare.prototype.determineBundleOutputPath = function () { var outputPath; if (this.argv.output) { outputPath = path.join(this.argv.output, this.projectName + ".graphql"); } else if (lodash_1.has(this.project.config, "extensions.bundle")) { if (!this.argv.save) { this.context.spinner.warn("Deprecated extension key 'bundle' found in config file. Use '--save' to update to 'prepare-bundle'."); } outputPath = lodash_1.get(this.project.config, "extensions.bundle"); } else if (lodash_1.has(this.project.config, "extensions.prepare-bundle")) { outputPath = lodash_1.get(this.project.config, "extensions.prepare-bundle"); } else { throw new Error('Output path cannot be determined. No existing configuration found and no output parameter specified.'); } fs.ensureDirSync(path.dirname(outputPath)); return outputPath; }; return Prepare; }()); exports.Prepare = Prepare; //# sourceMappingURL=prepare.js.map