UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

204 lines (164 loc) 5.82 kB
import inquirer from 'inquirer'; import { Listr } from 'listr2'; import chalk from 'chalk'; import fs from 'fs'; import validate from '../validator/index.js'; import bundleComponent from '../bundle/index.js'; import { lintComponent } from '../linter/index.js'; import { getComponentDirectoryPath, showVersion, getLibraryBased, getUseInputConfig, addDebugLog, getPegaServerConfig, updateServerConfigRuleSetAndVersion, checkLibraryAndArchives, getConfigDevBuild, checkJWTExpiration, getConfigDefaults, getInputConfigForCommand } from '../../util.js'; import { getPublishComponentQuestions, publishComponentToServer, zipComponent } from './helper.js'; import { default as buildLibrary } from '../build-lib/index.js'; import { default as publishLibrary } from '../publish-lib/index.js'; import { TOKEN_PATH } from '../../constants.js'; export default async options => { await showVersion(); await checkLibraryAndArchives(); const isLibraryBased = getLibraryBased(); const useInputConfig = getUseInputConfig(); addDebugLog("publish", "", "+"); if (isLibraryBased) { await checkJWTExpiration(); const devBuild = await getConfigDevBuild(); let okToPublish = false; const orgLib = getConfigDefaults(); const currentOrgLib = orgLib.currentOrgLib; // const currentVersion = devBuild ? `${orgLib.version}-dev`: orgLib.version; const currentVersion = orgLib.buildVersion; if (useInputConfig) { const inputConfig = await getInputConfigForCommand("publish"); console.log("Input config: "); console.log(inputConfig); if (inputConfig.okToPublish ) { if (devBuild || (!devBuild && inputConfig.okToPublishPermanent)) { okToPublish = true; } } } else { const publishThisVersion = await inquirer.prompt([ { name: 'confirmPublish', type: 'confirm', message: `Publish ${chalk.bold.green(`${orgLib.displayLibVersion}`)}, proceed ?`, default: true } ]); if (publishThisVersion.confirmPublish) { okToPublish = true; } if (okToPublish && !devBuild) { okToPublish = false; console.log(""); const publishAnswers = await inquirer.prompt([ { name: 'confirmPublish', type: 'confirm', message: `You are about to publish a ${chalk.bold.yellow('PRODUCTION')} version which can ${chalk.bold.yellow('NOT')} be deleted, proceed ?`, default: false } ]); if (publishAnswers.confirmPublish) { okToPublish = true; } } } if (okToPublish) { // marking as internal, so no questions will be ask options.params.push("internal"); await buildLibrary(options); await publishLibrary(options); } return true; } let OauthData; try { OauthData = fs.readFileSync(TOKEN_PATH, 'utf8'); } catch (ex) { } if (!OauthData) { console.log(chalk.redBright("Need to authenticate, missing services token.")); process.exit(1); } // non library based publish let componentKey; let sourceMap; let devBuild; let content; let doFetch = true; // const defaultPegaConfig = await getPegaServerConfig(); const componentDefaults = getConfigDefaults(); if (options.params.length >= 7) { componentKey = options.params[3]; const rulesetName = options.params[4]; const rulesetVersion = options.params[5]; const dBuild = options.params[6]; devBuild = dBuild === 'Y' || dBuild === 'y' || dBuild === true || dBuild === 'true'; // eslint-disable-next-line no-unneeded-ternary,no-nested-ternary doFetch = options.params.length >= 7 ? (options.params[7] === 'noFetch' ? false : true) : true; content = { componentKey, rulesetName, rulesetVersion, devBuild }; } else { // VS Code pluggin would send in the component let paramComponentKey; if (options.params.length == 4) { paramComponentKey = options.params[3]; } const questions = await getPublishComponentQuestions(paramComponentKey); const answers = await inquirer.prompt(questions); ({ componentKey = paramComponentKey, sourceMap, devBuild } = answers); content = { ...answers }; } // update ruleset and version config for future if (componentDefaults.serverType === "infinity") { updateServerConfigRuleSetAndVersion(content.rulesetName, content.rulesetVersion); } const sDevBuild = devBuild ? '(dev build)' : ''; const tasks = 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 () => 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); } } ], { exitOnError: true } ); await tasks.run().catch(err => { console.log(chalk.bold.red(err.toString())); }); addDebugLog("publish", "END", "-"); return true; };