@pega/custom-dx-components
Version:
Utility for building custom UI components
247 lines (209 loc) • 8.39 kB
JavaScript
import fs from 'fs';
import { join } from 'path';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { Listr } from 'listr2';
import validate from '../validator/index.js';
import bundleComponent from '../bundle/index.js';
import { lintComponent } from '../linter/index.js';
import {
checkPathAccess,
showVersion,
addDebugLog,
getComponents,
getConfigDefaults,
getPegaServerConfig,
getLibraryBased,
getComponentDirectoryPath,
getServerType
} from '../../util.js';
import { TASKS_CONFIG_JSON_FILENAME, PACKAGE_JSON_FILENAME } from '../../constants.js';
import { getFilePathQuestions, updateSavedExportFilePath, zipComponent, zipRC, getComponentInfoQuestions } from './helper.js';
import inquirerFuzzyPath from '@pega/inquirer-fuzzy-path';
const currentDirectory = process.cwd();
const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
export default async options => {
await showVersion();
await checkPathAccess(pegaConfigJsonPath);
const isLibraryBasedCL = getLibraryBased();
if (isLibraryBasedCL) {
console.log(`Command only supported for ${chalk.bold.green('non library mode')} components.`);
process.exit();
}
const serverType = getServerType();
if (serverType != 'launchpad') {
console.log(`Command only supported for ${chalk.green('Launchpad')}.`);
process.exit(1);
}
addDebugLog('export RC', '', '+');
let exportTo = '';
if (options.params.length >= 8) {
// params pick full, what library/name and path
// vs no params then we will select the current library
// let fullArchive = options.params[3];
// const libraryName = options.params[4];
// const libraryVersion = options.params[5];
// const filePath = options.params[6];
// exportTo = "Directory";
// const currentOrArchive = options.params[7];
// await exportArch(filePath, libraryName, libraryVersion, fullArchive, currentOrArchive );
} else {
let orgLibName = '';
let selectedVersion = '';
let fullArchive = '';
// if (options.params.length >= 5) {
// fullArchive = options.params[3];
// orgLibName = options.params[4];
// if (options.params.length >= 6) {
// selectedVersion = options.params[5];
// }
// }
// disable question to build "CL"
// if (fullArchive === "" ) {
// const fullArchiveQuestions = await getFullArchiveQuestions();
// const fullArchiveAnswers = await inquirer.prompt(fullArchiveQuestions);
// ({ fullArchive } = fullArchiveAnswers);
// }
// force full archive, binary deprecated
fullArchive = 'Full';
let filePathQuestions;
let filePathAnswers;
let filePath;
let content;
const localComponents = await getComponents();
const defaultPegaConfig = await getPegaServerConfig();
const configDef = getConfigDefaults();
if (localComponents.length > 0) {
const compAnswers = await inquirer.prompt([
{
name: 'componentKey',
type: 'rawlist',
message: 'Select component to export as a runtime component',
choices: localComponents
},
{
name: 'devBuild',
type: 'confirm',
message: 'Generate development build ?',
default: defaultPegaConfig.devBuild
}
]);
content = {
...compAnswers
};
inquirer.registerPrompt('fuzzypath', inquirerFuzzyPath);
const filePathQuestions = await getFilePathQuestions();
const filePathAnswers = await inquirer.prompt(filePathQuestions);
const exportPath = filePathAnswers.filePath;
const componentKey = compAnswers.componentKey;
const devBuild = compAnswers.devBuild;
if (!fs.existsSync(exportPath)) {
console.log(`\Path: ${chalk.yellow(`${exportPath}`)} doesn't exist.`);
const newPathAnswer = await inquirer.prompt([
{
name: 'createNewPath',
type: 'confirm',
message: 'Create path',
default: true
}
]);
const createNewPath = newPathAnswer.createNewPath;
if (createNewPath) {
fs.mkdirSync(exportPath, { recursive: true });
await updateSavedExportFilePath(exportPath);
} else {
console.log(chalk.green('Store not exported.'));
process.exit();
}
}
const archiveFileName = `${componentKey}_${configDef.version}.RC.zip`;
if (fs.existsSync(join(exportPath, archiveFileName))) {
console.log(`\nFile: ${chalk.yellow(`${archiveFileName}`)} already exists in ${chalk.yellow(`${exportPath}`)}.`);
const overwriteQuestion = await inquirer.prompt([
{
name: 'overWriteFile',
type: 'confirm',
message: 'Overwrite',
default: true
}
]);
if (!overwriteQuestion.overWriteFile) {
console.log(chalk.green('Archive not created.'));
process.exit();
}
}
console.log(chalk.yellow(`\n>> You are about to create a zip file containing an independent component `));
console.log(chalk.yellow(`>> that others can import directly into Launchpad.`));
console.log(chalk.yellow(`>> You will be asked a series of questions for the benefit of others`));
console.log(chalk.yellow(`>> who might choose to use your component.`));
console.log(chalk.yellow(`\n>> These answers will be recorded in a json file within the zip file.`));
console.log(chalk.yellow(`>> These questions will NOT be used as a gate to prevent the creation of this component.\n`));
const currentDirectory = process.cwd();
const packagePath = join(currentDirectory, PACKAGE_JSON_FILENAME);
let retrievedPackageData;
try {
retrievedPackageData = fs.readFileSync(packagePath);
} catch (ex) {
console.log(chalk.red(`\nUnable to retrieve package.json.`));
process.exit(1);
}
const rPackage = JSON.parse(retrievedPackageData);
const componentInfoQuestions = await getComponentInfoQuestions(rPackage, configDef.library);
let componentInfo = await inquirer.prompt(componentInfoQuestions);
const currentDate = new Date(Date.now());
const dateStamp = currentDate.toString();
const timeStamp = currentDate.toJSON();
componentInfo = { ...componentInfo, dependencies: rPackage.dependencies, buildDate: dateStamp, timeStamp };
console.log('\nBuilding RC...');
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, defaultPegaConfig.sourceMap, devBuild),
skip: () => (options.skipBundle ? 'Skipped bundling component' : undefined)
},
{
title: 'Create internal zip of component',
task: async () => {
const output = await zipComponent(componentKey);
content = { ...content, ...output, componentInfo: componentInfo };
}
},
{
title: 'Create zip file',
task: async () => {
const output = await zipRC(content, exportPath, archiveFileName);
content = { ...content, ...output };
}
}
],
{
exitOnError: true
}
);
await tasks.run().catch(err => {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
});
console.log(`\nRuntime component ${chalk.green(`${archiveFileName}`)} has been created in ${chalk.green(`${exportPath}`)}.\n`);
} else {
console.log(chalk.bold.red(`\nNo components to export.\n`));
}
}
addDebugLog('export RC', 'END', '-');
};