UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

333 lines (275 loc) 9.22 kB
import path from 'path'; import fs from 'fs'; import { promisify } from 'util'; import { join } from 'path'; import inquirer from 'inquirer'; import ncp from 'ncp'; import chalk from 'chalk'; import { checkPathAccess, showVersion, getComponentsObj, getDirectoryFiles, getLibraryBased, deleteLocalLibrary, getC11NB2STokenAndStaticServer, updateComponentDefaultLibrary, updateDefaultOrganization, addDebugLog, checkLibraryAndArchives, getConfigDefaults } from '../../util.js'; import { TASKS_CONFIG_JSON_FILENAME, COMPONENTS_DIRECTORY_PATH, COMPONENTS_PATH } from '../../constants.js'; import { localBuildLibrary } from '@pega/custom-dx-components/src/tasks/build-lib/index.js'; export const DXCB_CONFIG_INTERNAL_JSON_FILENAME = 'src/dxcb.config.json'; const currentDirectory = process.cwd(); const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME); import { convertIntoPascalCase, getComponentDirectoryPath, getComponents, sanitize } from '../../util.js'; const copy = promisify(ncp); export const updateConfig = async ( { oldComponentKey, newComponentKey, library, organization, componentName, componentLabel, version, description, allowedApplications, targetDirectory }, options, onlyCompanion = false ) => { addDebugLog("updateConfig", "", ""); let configData = fs.readFileSync(join(targetDirectory, '/config.json'), { encoding: 'utf8' }); configData = configData && JSON.parse(configData); configData.name = newComponentKey; configData.label = componentLabel; configData.organization = organization; if (configData.componentKey) configData.componentKey = newComponentKey; configData.library = library; configData.version = version; configData.description = description != '' ? description : componentLabel; configData.allowedApplications = allowedApplications; // stringify "4", makes the json string look like JSON in the file, formated instead of a single line fs.writeFileSync(join(targetDirectory, '/config.json'), JSON.stringify(configData, null, 4), { encoding: 'utf8',flag:'w' }); }; export const updateFile = async ( fileName, oldComponentKeyPC, newComponentKeyPC, targetDirectory ) => { addDebugLog("updateFile", "", ""); let fileData = fs.readFileSync(join(targetDirectory, '/', fileName), { encoding: 'utf8' }); if (fileData.indexOf(oldComponentKeyPC) >= 0) { fileData = fileData.replaceAll(oldComponentKeyPC, newComponentKeyPC); fs.writeFileSync(join(targetDirectory, '/', fileName), fileData, { encoding: 'utf8',flag:'w' }); } }; export const renameLibrary = async ({ library, organization }, options) => { addDebugLog("renameLibrary", `library: ${library}, organization: ${organization}`, ""); const compDef = getConfigDefaults(); const currentOrganization = compDef.organization; const currentLibrary = compDef.library; const currentVersion = compDef.version; let compVersion; const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); if (tokenAndStaticServer.C11NB2S === undefined) { console.log( chalk.redBright( 'Need to authenticate, missing services token.\nBuilding a library requires authentication to acquire a token to build.' ) ); process.exit(1); } const currentOrgLib = currentOrganization.concat('_').concat(currentLibrary); const newOrgLib = organization.concat('_').concat(library); // rename all the components const components = await getComponents(); let wasDevBuild = false; for (const index in components) { const componentKey = components[index]; const componentName = componentKey.split('_')[2]; const currentDirectory = await getComponentDirectoryPath(componentKey); let configData = fs.readFileSync(join(currentDirectory, '/config.json'), { encoding: 'utf8' }); configData = configData && JSON.parse(configData); const componentLabel = configData.label; const description = configData.description; const version = configData.version; compVersion = version; // can only rename "devBuilds", so if version doesn't have "-dev", add it and will create dev version if (compVersion && compVersion.indexOf('-dev') != -1) { wasDevBuild = true; } const allowedApplications = configData.allowedApplications; await renameComponent( { library, organization, componentKey, componentName, componentLabel, version, description, allowedApplications }, options ); } // delete and rebuild library try { await deleteLocalLibrary(currentOrgLib); } catch (ex) {} // update config await updateComponentDefaultLibrary(library); await updateDefaultOrganization(organization); // call constellation-dx-components-build-utils build library, which will create a directory of the "buildLibName" // with subdirectories for each version. // If "buildDev", we append to version "-dev". Currently, only "-dev" versions, when published, can be deleted. if (newOrgLib && compVersion) { await localBuildLibrary(organization, library, currentVersion, wasDevBuild); } }; export const renameComponent = async ( { library, organization, componentKey, componentName, componentLabel, version, description, allowedApplications }, options, onlyCompanion = false ) => { addDebugLog("renameComponent", `library: ${library}, organization: ${organization}, componentKey: ${componentKey}`, ""); const newComponentKey = `${organization}_${library}_${componentName}`; const oldComponentKey = componentKey; const newComponentKeyPC = convertIntoPascalCase(newComponentKey); const oldComponentKeyPC = convertIntoPascalCase(oldComponentKey); const currentDirectory = await getComponentDirectoryPath(componentKey); const targetDirectory = await getComponentDirectoryPath(newComponentKey); // custom component try { fs.renameSync(currentDirectory, targetDirectory); } catch (err) { console.log(err); } const targetFileList = await getDirectoryFiles(targetDirectory); for (var fileIndex in targetFileList) { const fileName = targetFileList[fileIndex]; if (fileName === 'config.json') { await updateConfig( { oldComponentKey, newComponentKey, componentName, componentLabel, library, version, description, allowedApplications, organization, targetDirectory }, options ); } else { await updateFile(fileName, oldComponentKeyPC, newComponentKeyPC, targetDirectory); } } // for console.log(chalk.green(`Component ${oldComponentKey} renamed to ${newComponentKey}`)); }; export default async options => { const isLibraryBased = getLibraryBased(); if (!isLibraryBased) { console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`); process.exit(); } await showVersion(); await checkLibraryAndArchives(); addDebugLog("renameLibrary", "", "+"); await checkPathAccess(pegaConfigJsonPath); let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' }); data = data && JSON.parse(data); if (!data[COMPONENTS_DIRECTORY_PATH]) { console.error( `${chalk.red.bold('ERROR')} Could not able find components directory path in config.json` ); process.exit(1); } const componentData = data[COMPONENTS_PATH]; const { library } = componentData; const { organization } = JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')); let componentDefaults = getConfigDefaults(); componentDefaults.library = library; const localComponents = await getComponentsObj(); if (options.params.length >= 5) { const organization = options.params[3]; const library = options.params[4]; await renameLibrary( { library, organization }, options ); } else { const questions = [ { name: 'organization', message: 'Enter new component organization', default: organization, validate: value => { if (value) { return true; } return 'Please provide value for organization'; } }, { name: 'library', message: 'Enter new library name', default: componentDefaults.library, validate: value => { /* value should not be empty It should not have spaces It should not start with a number Only case-insensitive alphanumeric values are allowed */ if (value && !/^\d/.test(value) && value === sanitize(value)) { return true; } return 'Only alphanumeric values are allowed, starting with alphabets'; } } ]; await inquirer.prompt(questions).then(async answers => { const { organization, library } = answers; await renameLibrary( { library, organization }, options ); }); } addDebugLog("renameLibrary", "END", "-"); };