@pega/custom-dx-components
Version:
Utility for building custom UI components
121 lines (100 loc) • 3.46 kB
JavaScript
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 {
getComponentDirectoryPath,
showVersion,
getComponents,
addDebugLog,
cleanUp,
getLibraryBased,
checkLibraryAndArchives,
getConfigDefaults,
zipVersionAndArchive } from '../../util.js';
import { getBuildComponentQuestions } from './helper.js';
export function getCustomTasks(componentKey, sourceMap, devBuild, options, showStats, skipBuild) {
addDebugLog("getCustomTasks", `componentKey: ${componentKey}, sourceMap: ${sourceMap}, devBuild: ${devBuild}, skipBuild: ${skipBuild}`, "");
const sDevBuild = devBuild ? '(dev build)' : '';
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, showStats),
enabled: () => !skipBuild
}
],
{ concurrent: false, exitOnError: true }
);
}
export default async options => {
await showVersion();
await checkLibraryAndArchives();
const isLibraryBased = getLibraryBased();
addDebugLog("buildAllComponents", "", "+");
const components = await getComponents();
let sourceMap;
let devBuild;
let showStats = false;
let skipBuild = true;
let firstComponent = null;
if (options.params.length >= 4) {
const dBuild = options.params[3];
devBuild = !!(dBuild === 'Y' || dBuild === 'y' || dBuild === true || dBuild === 'true');
} else {
if (!isLibraryBased) {
const questions = await getBuildComponentQuestions();
const answers = await inquirer.prompt(questions);
({ sourceMap, devBuild } = answers);
showStats = true;
skipBuild = false;
}
else {
devBuild = false;
}
}
for await (const componentKey of components) {
console.log(chalk.bold.green(`Building ${componentKey}`));
// capture first component
if (firstComponent == null) {
firstComponent = componentKey;
}
const myCustomTasks = getCustomTasks(componentKey, sourceMap, devBuild, options, showStats, skipBuild);
try {
await myCustomTasks.run();
} catch (err) {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
}
}
// if skipBuild, then do a build here as opposed to every step (v1 is every step, v2, just once)
if (skipBuild && firstComponent) {
console.log();
console.log(chalk.bold.green("Validation compile..."));
await bundleComponent(firstComponent, sourceMap, devBuild, showStats);
const confDef = getConfigDefaults();
await zipVersionAndArchive(confDef.currentOrgLib, confDef.buildVersion);
}
// once done, remove leftover directories
await cleanUp();
console.log();
console.log(chalk.bold.green("Done."));
addDebugLog("buildAllComponents", "END", "-");
return true;
};