UNPKG

@vendure/cli

Version:

A modern, headless ecommerce framework

57 lines 2.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSchema = generateSchema; const prompts_1 = require("@clack/prompts"); const graphql_1 = require("@nestjs/graphql"); const core_1 = require("@vendure/core"); const fs_extra_1 = require("fs-extra"); const graphql_2 = require("graphql"); const node_path_1 = __importDefault(require("node:path")); const load_vendure_config_file_1 = require("../../../shared/load-vendure-config-file"); const shared_prompts_1 = require("../../../shared/shared-prompts"); const vendure_config_ref_1 = require("../../../shared/vendure-config-ref"); const cancelledMessage = 'Generate schema cancelled'; async function generateSchema(options) { var _a, _b; (0, core_1.resetConfig)(); try { const { project, vendureTsConfig } = await (0, shared_prompts_1.analyzeProject)({ cancelledMessage }); const vendureConfig = new vendure_config_ref_1.VendureConfigRef(project, options.config); prompts_1.log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot()); const config = await (0, load_vendure_config_file_1.loadVendureConfigFile)(vendureConfig, vendureTsConfig); await (0, core_1.setConfig)(config); const apiType = options.api === 'shop' ? 'shop' : 'admin'; const typePaths = apiType === 'shop' ? core_1.VENDURE_SHOP_API_TYPE_PATHS : core_1.VENDURE_ADMIN_API_TYPE_PATHS; const runtimeConfig = await (0, core_1.runPluginConfigurations)((0, core_1.getConfig)()); const typesLoader = new graphql_1.GraphQLTypesLoader(); const schema = await (0, core_1.getFinalVendureSchema)({ config: runtimeConfig, typePaths, typesLoader, apiType, }); const format = options.format === 'json' ? 'json' : 'sdl'; const ext = format === 'sdl' ? 'graphql' : 'json'; const fileName = (_a = options.fileName) !== null && _a !== void 0 ? _a : `schema${apiType === 'shop' ? '-shop' : ''}.${ext}`; const outFile = node_path_1.default.join((_b = options.outputDir) !== null && _b !== void 0 ? _b : process.cwd(), fileName); if (format === 'sdl') { (0, fs_extra_1.writeFileSync)(outFile, (0, graphql_2.printSchema)(schema)); } else { const jsonSchema = (0, graphql_2.graphqlSync)({ schema, source: (0, graphql_2.getIntrospectionQuery)(), }).data; (0, fs_extra_1.writeFileSync)(outFile, JSON.stringify(jsonSchema)); } prompts_1.log.info(`Generated schema: ${outFile}`); } catch (e) { prompts_1.log.error(e instanceof Error ? e.message : String(e)); process.exit(1); } } //# sourceMappingURL=generate-schema.js.map