@pega/custom-dx-components
Version:
Utility for building custom UI components
69 lines (50 loc) • 2.04 kB
JavaScript
import fs from 'fs';
import path from 'path';
import archiver from 'archiver';
import chalk from 'chalk';
import { getComponentDirectoryPath, getComponents, getPegaServerConfig, addDebugLog } from '../../util.js';
export const getBuildComponentQuestions = async () => {
addDebugLog("getBuildComponentQuestions", "", "");
const components = await getComponents();
const defaultPegaConfig = await getPegaServerConfig();
if (components.length == 0) {
console.log(chalk.redBright('No components to build'));
process.exit();
}
return [
{
name: 'devBuild',
type: 'confirm',
message: 'Generate development build ?',
default: defaultPegaConfig.devBuild
}
];
};
export const zipComponent = async componentKey => {
addDebugLog("zipComponent", `componentKey: ${componentKey}`, "");
const srcDirectory = await getComponentDirectoryPath(componentKey);
const buildDirectory = await path.join(path.resolve(), 'dist/components', componentKey);
const configJson = `${srcDirectory}/config.json`;
const localizationJson = `${srcDirectory}/localizations.json`;
const archive = archiver('zip', { zlib: { level: 9 } });
const zipChunks = [];
archive.on('data', chunk => zipChunks.push(chunk));
// Add src directory
archive.directory(srcDirectory, 'src');
// Add config.json file
if (fs.existsSync(configJson)) {
archive.file(configJson, { name: path.basename(configJson) });
}
// Add localizations.json file if it exists
if (fs.existsSync(localizationJson)) {
archive.file(localizationJson, { name: path.basename(localizationJson) });
}
// Add build directory
archive.directory(buildDirectory, false);
await archive.finalize();
const zipBuffer = Buffer.concat(zipChunks);
console.log(chalk.green(`component zipped with size: ${Math.ceil(zipBuffer.length / 1024)} KB`));
const zipContent = zipBuffer.toString('base64');
const configContent = Buffer.from(fs.readFileSync(configJson)).toString();
return { zipContent, configContent };
};