@pega/constellation-dx-components-build-utils
Version:
This tool uses a 'v3' approach to group components in a library, create a component map, employ webpack, and load the library like Pega-generated components, constellation app-static.
152 lines (148 loc) • 6.47 kB
JavaScript
const chalk = require('chalk');
const CONSTANTS = require('../lib/constant.js');
const { formatEndPointURL, getHttpsAgent, getHeaders, getAppStaticServerUrl } = require('../lib/helper.js');
/*
Interactions with ConstellationAppStatic Service
*/
const AppStaticService = {
/*
libId = libName:libVersions
Fetches all the library versions available for a library
*/
async fetchLibraryNameOrVersions(libId='', tokenParams, appStaticSVCUrl){
try{
const appStaticServerUrl = getAppStaticServerUrl(appStaticSVCUrl);
if(!appStaticServerUrl){
console.log(chalk.red('App static service url not available, exiting fetch operation.'));
return;
}
// Add libId to the url later on
// If libId is not null, then it's fetch the versions of a libname, not the libnames
const URL = formatEndPointURL(appStaticServerUrl, CONSTANTS.FETCH_ENDPOINT_URL, libId);
const headers = getHeaders(tokenParams);
if(!headers) { return; }
const response = await fetch(URL, {
method: 'GET',
headers: headers,
agent: getHttpsAgent()
});
const data = await response.json();
const status = response.status;
if (status === 401) {
throw new Error('Error occurred in authentication. Please regenerate using authenticate');
} else if (status === 200 || status === 201) {
return data;
} else if(status === 400){
console.log(chalk.redBright(`Bad Request: ${data}.`));
process.exit(1);
} else if(status === 403){
console.log(chalk.red(`JWT Token has been expired.. please update token : ${data}`));
process.exit(1);
} else if(status === 404){
return [];
}
else {
//console.log(response);
throw new Error(`${response}`);
}
}
catch(e){
console.log("appstatic.service: fetchLibraryNameOrVersions(): error occured", e);
if (e.code === 'ECONNREFUSED') {
console.log(chalk.red("Error: Connection refused"));
process.exit(1);
} else {
console.log(chalk.red(`Error: Connection error: ${JSON.stringify(e)}`));
process.exit(1);
}
}
},
async fetchLibraryManifestData(libId, libVersion, tokenParams, appStaticSVCUrl, cmptcfg){
try{
const appStaticServerUrl = getAppStaticServerUrl(appStaticSVCUrl);
if(!appStaticServerUrl){
console.log(chalk.red('App static service url not available, exiting fetch operation.'));
return;
}
let URL;
if(!cmptcfg){
URL = formatEndPointURL(appStaticServerUrl, CONSTANTS.ENDPOINTURL, 'component', libId+':'+libVersion, 'chunks', CONSTANTS.MANIFEST);
} else{
URL = formatEndPointURL(appStaticServerUrl, CONSTANTS.ENDPOINTURL, 'componentsconfig', libId+':'+libVersion);
}
const headers = getHeaders(tokenParams);
if(!headers) { return; }
const response = await fetch(URL, {
method: 'GET',
headers: headers,
agent: getHttpsAgent()
});
const status = response.status;
if (status === 401) {
throw new Error('Error occurred in authentication. Please regenerate using authenticate');
} else if(status === 404){
console.log(chalk.red(`Manifest not available for version selected`));
return;
}
const data = await response.json();
if (status === 200 || status === 201) {
return data;
} else if(status === 400){
console.log(chalk.redBright(`Bad Request: ${data}.`));
process.exit(1);
} else if(status === 403){
console.log(chalk.red(`JWT Token has been expired.. please update token : ${data}`));
} else {
throw new Error(`${response}`);
}
}
catch(e){
console.log("appstatic.service: fetchLibraryManifestData(): error occured", e);
if (e.code === 'ECONNREFUSED') {
console.log(chalk.red("Error: Connection refused"));
process.exit(1);
} else {
console.log(chalk.red(`Error: Connection error: ${JSON.stringify(e)}`));
process.exit(1);
}
}
},
/**
* Delete a library entirely, this is done by making calls with all the versions available and deleting them all
*/
async deleteLibraryOrVersions(libVersion, tokenParams, appStaticSVCUrl){
try{
const appStaticServerUrl = getAppStaticServerUrl(appStaticSVCUrl);
if(!appStaticServerUrl){
console.log(chalk.red('App static service url not available, exiting delete operation.'));
process.exit(1);
}
const URL = formatEndPointURL(appStaticServerUrl, CONSTANTS.DELETE_ENDPOINT_URL, libVersion);
const headers = getHeaders(tokenParams);
if(!headers) { return; }
const response = await fetch(URL, {
method: 'DELETE',
headers: headers,
agent: getHttpsAgent()
});
const status = await response.status;
if (status === 401) {
throw new Error('Error occurred in authentication. Please regenerate using authenticate');
}
else if(status === 400){
console.log(chalk.redBright(`Bad Request.`));
process.exit(1);
}
else if(status === 403){
console.log(chalk.red(`JWT Token has been expired.. please update token :`));
process.exit(1);
}
return status === 200 ? true : false;
}
catch(err){
console.log("appstatic.service: deleteLibraryOrVersions() : error occured", err);
process.exit(1);
}
}
};
module.exports = { AppStaticService }