UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

242 lines (178 loc) 7.6 kB
import inquirer from 'inquirer'; import chalk from 'chalk'; // eslint-disable-next-line import/order import { showVersion, updateComponentDefaultLibrary, updateComponentDefaultVersion, getLibraryBased, addDebugLog, zipVersionAndArchive, hasArchives, clearSrcComponentsDirectory, hasLibraryAndVersion, getConfigDefaults, forceDefaultsUpdate, cleanUpTemp, restoreTopLevelConfigFilesFromTemplate, getHaveDependencyDifference, getUseInputConfig, getInputConfigForCommand, convertYNToBoolean } from '../../util.js'; import { getCreateLibComponentQuestions, getOkToProceedQuestion, updateAllComponentsConfig, getCurrentOrOriginalQuestion } from './helper.js'; import { showCurrentStatus } from '../show-status/index.js'; export default async options => { const isLibraryBased = getLibraryBased(); const useInputConfig = getUseInputConfig(); if (!isLibraryBased) { console.log(`Command only supported for ${chalk.bold.green('library based')} components.`); process.exit(); } await showVersion(); await cleanUpTemp(); // if we have choosen to use INPUT CONFIG FILE // process and exit if (useInputConfig) { const inputConfig = await getInputConfigForCommand("createLib"); if (convertYNToBoolean(inputConfig.okToContinue)) { const libraryName = inputConfig.libraryName; let version = inputConfig.libraryVersion; const currentOrOriginal = inputConfig.currentOrOriginal; const isPermanent = inputConfig.isPermanent; const permBuild = convertYNToBoolean(isPermanent); // if have store, then that means there is a current library, so // store it before clearing out /src/components const hasArch = await hasArchives(); const orgLib = getConfigDefaults(); const newOrgLib = `${orgLib.organization}_${libraryName}`; if (hasArch) { const hasLib = await hasLibraryAndVersion(libraryName, version, !permBuild); if (hasLib) { console.log(`${chalk.bold.red(`Library ${orgLib.organization}_${libraryName} already exists.`)}`); console.log("You can switch to that library by entering 'npm run switchLib'.\n") process.exit(); } const currentOrgLib = orgLib.currentOrgLib; const currentVersion = orgLib.buildVersion; await zipVersionAndArchive(currentOrgLib, currentVersion); await clearSrcComponentsDirectory(); } if (!permBuild) { version = version.concat("-dev"); } if (currentOrOriginal === 'Original') { await restoreTopLevelConfigFilesFromTemplate(); } // update library name in config and create a blank archive await updateComponentDefaultLibrary(libraryName); await zipVersionAndArchive(newOrgLib, version); // if didn't have an archive, in case you currently have components there // update the config jsons if (!hasArch) { await updateAllComponentsConfig(version); } console.log(`${chalk.bold.green(`\nLibrary ${newOrgLib}/${version} has been created!\n`)}`); await forceDefaultsUpdate(); await showCurrentStatus(); if (currentOrOriginal === 'Original') { if (getHaveDependencyDifference()) { console.log('\n***************************************************************'); console.log(chalk.bold.yellow('Dependencies have changed between libraries, recommend updating.')); console.log(`>>> PLEASE run ${chalk.bold.yellow("'npm update'")}.`); console.log('***************************************************************\n'); } } } process.exit(); } addDebugLog("createLibrary", "", "+"); let libraryName = ""; let version; // permBuild is the opposite of devBuild let permBuild; const orgLib = getConfigDefaults(); const hasArch = await hasArchives(); let currentOrOriginal = ""; if (hasArch && options.params.length < 6) { console.log(chalk.bold.yellow('\nBe sure to SAVE all changes before creating a new library, unsaved changes will be LOST.')) console.log(chalk.bold.yellow('Current library/version will be stored and the newly created library/version will be your default.')) const okQuestion = await getOkToProceedQuestion(); const okAnswers = await inquirer.prompt(okQuestion); const { okToContinue } = okAnswers; if (!okToContinue) { return true; } } if (options.params.length >= 7) { libraryName = options.params[3]; version = options.params[4]; const dPerm = options.params[5]; currentOrOriginal = options.params[6]; permBuild = !!(dPerm === 'Y' || dPerm === 'y' || dPerm === true || dPerm === 'true'); } else { if (!hasArch) { // if no archives, get library name from tasks.config as default libraryName = orgLib.library; } const questions = await getCreateLibComponentQuestions(libraryName); const answers = await inquirer.prompt(questions); ({ libraryName, version, permBuild } = answers); } // if have store, then that means there is a current library, so // store it before clearing out /src/components if (hasArch) { const hasLib = await hasLibraryAndVersion(libraryName, version, !permBuild); if (hasLib) { console.log(`${chalk.bold.red(`Library ${orgLib.organization}_${libraryName} already exists.`)}`); console.log("You can switch to that library by entering 'npm run switchLib'.\n") process.exit(); } const currentOrgLib = orgLib.currentOrgLib; const currentVersion = orgLib.buildVersion; await zipVersionAndArchive(currentOrgLib, currentVersion); await clearSrcComponentsDirectory(); } const newOrgLib = `${orgLib.organization}_${libraryName}`; if (orgLib.version == null || orgLib.version !== version) { await updateComponentDefaultVersion(version); } if (!permBuild) { version = version.concat("-dev"); } if (hasArch && currentOrOriginal === "") { console.log(`\n${chalk.yellow(`You may have updated package.json, and other config files.`)}`); console.log(`${chalk.yellow(`Do you wish to keep those CURRENT changes OR revert back to ORIGINAL install configs.`)}\n`); // need to ask the question const curOrgQuestion = await getCurrentOrOriginalQuestion(); const currAnswers = await inquirer.prompt(curOrgQuestion); ({ currentOrOriginal } = currAnswers); } else { currentOrOriginal = 'Original'; } if (currentOrOriginal === 'Original') { await restoreTopLevelConfigFilesFromTemplate(); } // update library name in config and create a blank archive await updateComponentDefaultLibrary(libraryName); await zipVersionAndArchive(newOrgLib, version); // if didn't have an archive, in case you currently have components there // update the config jsons if (!hasArch) { await updateAllComponentsConfig(version); } console.log(`${chalk.bold.green(`\nLibrary ${newOrgLib}/${version} has been created!\n`)}`); await forceDefaultsUpdate(); await showCurrentStatus(); if (currentOrOriginal === 'Original') { if (getHaveDependencyDifference()) { console.log('\n***************************************************************'); console.log(chalk.bold.yellow('Dependencies have changed between libraries, recommend updating.')); console.log(`>>> PLEASE run ${chalk.bold.yellow("'npm update'")}.`); console.log('***************************************************************\n'); } } addDebugLog("createLibrary", "END", "-"); return true; };