UNPKG

@kontent-ai/model-generator

Version:

This utility generates strongly-typed models for Delivery JS SDK, Migration toolkit or just general scripting to improve the experience when referencing Kontent.ai related objects.

99 lines 4.84 kB
import chalk from "chalk"; import { environmentEntities, } from "../../core/core.models.js"; import { uniqueFilter } from "../../core/core.utils.js"; import { getManagementKontentFetcher } from "../../fetch/management-kontent-fetcher.js"; import { getFileManager } from "../../files/file-manager.js"; import { getEnvironmentGenerator } from "./environment.generator.js"; export async function generateEnvironmentModelsAsync(config) { console.log(chalk.green("Model generator started \n")); console.log(`Generating '${chalk.yellow("environment")}' models\n`); const { environmentFiles, environmentInfo } = await getFilesAsync(config); const fileManager = getFileManager({ ...config, environmentInfo, }); const setFiles = await fileManager.getSetFilesAsync([environmentFiles]); if (config.createFiles) { fileManager.createFiles(setFiles); } console.log(chalk.green("\nCompleted")); return setFiles; } async function getFilesAsync(config) { const kontentFetcher = getManagementKontentFetcher({ environmentId: config.environmentId, managementApiKey: config.managementApiKey, baseUrl: config.managementBaseUrl, }); const entitiesToCreate = config.entities ?? environmentEntities; // default to all entities export const environmentInfo = await kontentFetcher.getEnvironmentInfoAsync(); const entities = await getEntitiesAsync({ kontentFetcher, entitiesConfig: entitiesToCreate, }); return { environmentInfo, environmentFiles: getEnvironmentGenerator({ environmentInfo, environmentEntities: entities, entitiesToCreate: entitiesToCreate, }).generateEnvironmentModels(), moduleFileExtension: config.moduleFileExtension, }; } async function getEntitiesAsync({ kontentFetcher, entitiesConfig, }) { const entitiesToFetch = getEntitiesToFetchFromApi(entitiesConfig); const [languages, taxonomies, contentTypes, snippets, collections, workflows, webhooks, assetFolders, roles, customApps, spaces, previewUrls,] = await Promise.all([ fetchEntity({ canFetch: () => entitiesToFetch.includes("languages"), fetch: () => kontentFetcher.getLanguagesAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("taxonomies"), fetch: () => kontentFetcher.getTaxonomiesAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("contentTypes"), fetch: () => kontentFetcher.getTypesAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("snippets"), fetch: () => kontentFetcher.getSnippetsAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("collections"), fetch: () => kontentFetcher.getCollectionsAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("workflows"), fetch: () => kontentFetcher.getWorkflowsAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("webhooks"), fetch: () => kontentFetcher.getWebhooksAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("assetFolders"), fetch: () => kontentFetcher.getAssetFoldersAsync(), }), fetchEntity({ canFetch: () => entitiesToFetch.includes("roles"), fetch: () => kontentFetcher.getRolesAsync() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("customApps"), fetch: () => kontentFetcher.getCustomApps() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("spaces"), fetch: () => kontentFetcher.getSpaces() }), fetchEntity({ canFetch: () => entitiesToFetch.includes("previewUrls"), fetch: () => kontentFetcher.getPreviewUrlConfiguration(), }), ]); return { assetFolders, collections, languages, roles, snippets, taxonomies, contentTypes, webhooks, workflows, customApps, previewUrls, spaces, }; } function getEntitiesToFetchFromApi(entityTypes) { return [ ...entityTypes, // when requesting snippets or content types, we need to fetch taxonomies & (snippets or types) as well // this is is because we need these entities to narrow down types for elements ...(entityTypes.includes("contentTypes") || entityTypes.includes("snippets") ? ["taxonomies", "snippets", "contentTypes"] : []), // when requesting preview urls, we need to fetch spaces & content types as well ...(entityTypes.includes("previewUrls") ? ["spaces", "contentTypes"] : []), ].filter(uniqueFilter); } function fetchEntity({ canFetch, fetch, }) { if (!canFetch()) { return Promise.resolve(undefined); } return fetch(); } //# sourceMappingURL=environment-func.js.map