@pega/custom-dx-components
Version:
Utility for building custom UI components
200 lines (163 loc) • 6.04 kB
JavaScript
import inquirer from 'inquirer';
import { Listr } from 'listr2';
import chalk from 'chalk';
import { buildLib, buildLibV3 } from '@pega/constellation-dx-components-build-utils/index.js';
import validate from '../validator/index.js';
import { lintComponent } from '../linter/index.js';
import {
getComponentDirectoryPath,
showVersion,
getComponents,
getC11NB2STokenAndStaticServer,
updateComponentDefaultVersion,
updateConfigVersion,
addDebugLog,
cleanUp,
checkLibraryAndArchives,
getConfigDevBuild,
checkJWTExpiration,
getConfigDefaults,
getLibraryBased,
getLibraryBasedCL
} from '../../util.js';
import bundleComponent from '../bundle/index.js';
import { getBuildComponentQuestions } from './helper.js';
export function getCustomTasks(componentKey, devBuild, buildVersion, currentOrgLib, token) {
addDebugLog("getCustomTasks", `componentKey: ${componentKey}, devBuild: ${devBuild}, buildVersion: ${buildVersion}, currentOrgLib: ${currentOrgLib}`,"");
return new Listr(
[
{
title: 'Validate config schema',
task: async () => {
await updateConfigVersion(componentKey, buildVersion, currentOrgLib, token);
await validate(componentKey);
},
skip: () => componentKey === 'shared'
},
{
title: 'Lint component',
task: async () => {
const targetDirectory = await getComponentDirectoryPath(componentKey);
// console.log(`in buildComponent Lint component task: componentKey: ${componentKey} targetDirectory: ${targetDirectory}`);
await lintComponent(targetDirectory);
}
}
],
{ concurrent: false, exitOnError: true }
);
}
export const localBuildLibrary = async(organization, library, version, devBuild) => {
addDebugLog("localBuildLibrary", `organization: ${organization}, library: ${library}, version: ${version}, devBuild: ${devBuild}`, "");
const components = await getComponents();
const tokenAndStaticServer = await getC11NB2STokenAndStaticServer();
const currentOrgLib = organization.concat('_').concat(library);
const isLibraryBasedCL = getLibraryBasedCL();
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);
}
if (!components || components.length === 0) {
console.log(
chalk.redBright(
'No components to build/publish.'
)
);
process.exit(1);
}
const buildLibName = organization.concat('_').concat(library);
await updateComponentDefaultVersion(version);
const buildVersion = devBuild && version.indexOf('-dev') < 0 ? version.concat('-dev') : version;
for await (const componentKey of components) {
console.log(chalk.bold.green(`Building ${componentKey}`));
// eslint-disable-next-line sonarjs/no-extra-arguments
const myCustomTasks = getCustomTasks(componentKey, devBuild, buildVersion, currentOrgLib, tokenAndStaticServer.C11NB2S);
try {
await myCustomTasks.run();
} catch (err) {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
}
console.log(''); // line break
}
// bundling the first component will actually compile all. We should
// compile to see if any errors, as buildLib doesn't catch all errors.
// Turning off stats (last param), so won't show warnings, as
// warnings are part of buildLib
// for now
// console.log(chalk.green.bold('Compile check...'));
// await bundleComponent(components[0], null, devBuild, false);
// console.log('');
// remove dist/lib stuff
await cleanUp();
console.log(chalk.green.bold('Building Library... '));
// 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.
addDebugLog("buildLib", "services call", "");
// buildLibV3(
// buildLibName,
// buildVersion,
// tokenAndStaticServer.C11NB2S,
// tokenAndStaticServer.appStaticContentServer
// );
if (isLibraryBasedCL) {
buildLibV3(
buildLibName,
buildVersion
);
}
else {
buildLib(
buildLibName,
buildVersion,
tokenAndStaticServer.C11NB2S,
tokenAndStaticServer.appStaticContentServer
);
}
addDebugLog("buildLib", "services end", "");
}
export default async options => {
const isLibraryBased = getLibraryBased();
if (!isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('library based')} components.`)
process.exit();
}
if (options.params.length != 7) {
// internal so already called
await showVersion();
await checkLibraryAndArchives();
await checkJWTExpiration();
}
addDebugLog("buildLibrary", "", "+");
const compDef = getConfigDefaults();
const organization = compDef.organization;
const library = compDef.library;
// const currentOrgLib = organization.concat('_').concat(library);
const currentOrgLib = compDef.currentOrgLib;
let version;
let devBuild;
if (options.params.length === 6) {
// internally called from publish
const orgLib = getConfigDefaults();
version = orgLib.version;
devBuild = await getConfigDevBuild();
}
else if (options.params.length >= 5) {
version = options.params[3];
const dBuild = options.params[4];
devBuild = !!(dBuild === 'Y' || dBuild === 'y' || dBuild === true || dBuild === 'true');
}
else {
console.log(`\nLibrary ${chalk.bold.green(currentOrgLib)} `);
const questions = await getBuildComponentQuestions(compDef.version);
const answers = await inquirer.prompt(questions);
({ version, devBuild } = answers);
}
await localBuildLibrary(organization, library, version, devBuild);
addDebugLog("buildLibrary", "END", "-");
return true;
};