UNPKG

@prismatic-io/spectral

Version:

Utility library for building Prismatic connectors and code-native integrations

94 lines (93 loc) 4.17 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDataSources = void 0; const path_1 = __importDefault(require("path")); const getInputs_1 = require("./getInputs"); const getImports_1 = require("./getImports"); const helpers_1 = require("./helpers"); const createTemplate_1 = require("../utils/createTemplate"); const createTypeInterface_1 = require("../utils/createTypeInterface"); const createImport_1 = require("../utils/createImport"); const createDataSources = (_a) => __awaiter(void 0, [_a], void 0, function* ({ component, dryRun, verbose, sourceDir, destinationDir, }) { var _b, _c; if (verbose) { console.info("Creating data sources..."); } const dataSourceIndex = yield renderDataSourcesIndex({ imports: Object.entries((_b = component.dataSources) !== null && _b !== void 0 ? _b : {}).map(([dataSourceKey, dataSource]) => { return { import: (0, createImport_1.createImport)(dataSource.key || dataSourceKey), }; }), dryRun, verbose, sourceDir, destinationDir, }); const dataSources = yield Promise.all(Object.entries((_c = component.dataSources) !== null && _c !== void 0 ? _c : {}).map((_a) => __awaiter(void 0, [_a], void 0, function* ([dataSourceKey, dataSource]) { const inputs = (0, getInputs_1.getInputs)({ inputs: dataSource.inputs, }); const imports = (0, getImports_1.getImports)({ inputs }); return yield renderDataSource({ dataSource: { type: (0, createTypeInterface_1.createTypeInterface)(dataSource.key || dataSourceKey), import: (0, createImport_1.createImport)(dataSource.key || dataSourceKey), key: dataSource.key || dataSourceKey, label: dataSource.display.label, description: dataSource.display.description, dataSourceType: dataSource.dataSourceType, inputs, }, imports, dryRun, verbose, sourceDir, destinationDir, }); }))); if (verbose) { console.info(""); } return Promise.resolve({ dataSourceIndex, dataSources, }); }); exports.createDataSources = createDataSources; const renderDataSourcesIndex = (_a) => __awaiter(void 0, [_a], void 0, function* ({ imports, dryRun, verbose, sourceDir, destinationDir, }) { return yield (0, createTemplate_1.createTemplate)({ source: path_1.default.join(sourceDir, "dataSources", "index.ts.ejs"), destination: path_1.default.join(destinationDir, "dataSources", "index.ts"), data: { imports, }, dryRun, verbose, }); }); const renderDataSource = (_a) => __awaiter(void 0, [_a], void 0, function* ({ dataSource, dryRun, imports, verbose, sourceDir, destinationDir, }) { return yield (0, createTemplate_1.createTemplate)({ source: path_1.default.join(sourceDir, "dataSources", "dataSource.ts.ejs"), destination: path_1.default.join(destinationDir, "dataSources", `${dataSource.import}.ts`), data: { dataSource, helpers: helpers_1.helpers, imports, }, dryRun, verbose, }); });