UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

148 lines (123 loc) 4.16 kB
import inquirer from 'inquirer'; import { Listr } from 'listr2'; import chalk from 'chalk'; import validate from '../validator/index.js'; import bundleComponent from '../bundle/index.js'; import { lintComponent } from '../linter/index.js'; import { getComponents, getComponentDirectoryPath, checkAccessTokenExpiration, showVersion, getLibraryBased, addDebugLog, getPegaServerConfig, updateServerConfigRuleSetAndVersion, checkLibraryAndArchives, checkForBranch } from '../../util.js'; import { getPublishComponentQuestions, publishComponentToServer, zipComponent, cleanUp } from './helper.js'; export function getCustomTasks(componentKey, sourceMap, devBuild, content, options) { addDebugLog("getCustomTasks", `componentKey: ${componentKey}, sourceMap: ${sourceMap}, devBuild: ${devBuild}`, ""); const sDevBuild = devBuild ? '(dev build)' : ''; // eslint-disable no-unneeded-ternary,no-nested-ternary const doFetch = options.params.length >= 7 ? (options.params[6] === 'noFetch' ? false : true) : true; return new Listr( [ { title: 'Validate config schema', task: async () => { await validate(componentKey); } }, { title: 'Lint component', task: async () => { const targetDirectory = await getComponentDirectoryPath(componentKey); // console.log(`in buildComponent Lint component task: componentKey: ${componentKey} targetDirectory: ${targetDirectory}`); await lintComponent(targetDirectory); } }, { title: `Bundle Component ${sDevBuild}`, task: async () => { await bundleComponent(componentKey, sourceMap, devBuild); }, skip: () => (options.skipBundle ? 'Skipped bundling component' : undefined) }, { title: 'Zip Component', task: async () => { const output = await zipComponent(componentKey); content = { ...content, ...output }; } }, { title: 'Publish Component', task: async () => { await publishComponentToServer(content, doFetch); } } ], { concurrent: false, exitOnError: true } ); } export default async options => { const isLibraryBased = getLibraryBased(); if (isLibraryBased) { console.log(`Command only supported for ${chalk.bold.green('non library mode')} components.`) process.exit(); } await showVersion(); await checkLibraryAndArchives(); await checkAccessTokenExpiration(); addDebugLog("publishAll", "", "+"); const components = await getComponents(); const defaultPegaConfig = await getPegaServerConfig(); if (components.length === 0) { console.log(chalk.redBright('No components to publish')); process.exit(); } let componentKey; let sourceMap; let devBuild; let content; if (options.params.length >= 6) { const rulesetName = options.params[3]; const rulesetVersion = options.params[4]; const dBuild = options.params[5]; devBuild = dBuild === 'Y' || dBuild === 'y' || dBuild === true || dBuild === 'true'; content = { componentKey, rulesetName, rulesetVersion, devBuild }; } else { const questions = await getPublishComponentQuestions(); const answers = await inquirer.prompt(questions); ({ sourceMap, devBuild } = answers); content = { ...answers }; } await checkForBranch(content.rulesetName); // update ruleset and version config for future if (defaultPegaConfig.serverType === "infinity") { updateServerConfigRuleSetAndVersion(content.rulesetName, content.rulesetVersion); } // eslint-disable-next-line no-shadow for await (const componentKey of components) { console.log(chalk.bold.green(`Publishing: ${componentKey}`)); const myCustomTasks = getCustomTasks(componentKey, sourceMap, devBuild, content, options); try { await myCustomTasks.run(); } catch (err) { console.log(chalk.bold.red(err.toString())); process.exit(1); } } // once done, remove leftover directories await cleanUp(); addDebugLog("publishAll", "END", "-"); return true; };