Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 6x 2x 2x 6x 24x 24x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 40x 40x 40x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 12x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 12x 6x 6x 6x 6x 2x 2x | const child_process = require("child_process");
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const CONSTANTS = require('../constant');
const helper = require('../helper');
const chalk = require('chalk');
const ensureDirSync = (filePath) => {
const dir = path.dirname(filePath);
Iif (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
};
const buildComponentsListFile = (buildFolderName, packageVersion, notClean, pathToBuild) => {
Eif (!notClean)
helper.cleanUpBuildFolder(CONSTANTS.CURRENT_WORKING_DIR, buildFolderName, packageVersion);
console.log(chalk.magenta("== Aggregating components to build - Components List - Started =="));
const rootForCustomCmptDir = pathToBuild ? path.dirname(pathToBuild) : undefined;
const customCmptDir = pathToBuild ? path.basename(pathToBuild) : undefined;
let projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, CONSTANTS.PROJECT_ROOT).replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, rootForCustomCmptDir).replace(/\\/g, '/');
}
let componentsDir = path.join(projectPath, CONSTANTS.COMPONENTS_DIR).replace(/\\/g, '/');
Iif(pathToBuild && customCmptDir){
componentsDir = path.join(projectPath, customCmptDir).replace(/\\/g, '/');
}
let componentsLists;
Iif(pathToBuild){
componentsLists = getComponentsWithConfig(
componentsDir,
CONSTANTS.COMPONENT_CONFIG_FILE_NAME
);
} else{
componentsLists = fs.readdirSync(componentsDir).filter(function (file) {
if(fs.statSync(path.join(componentsDir, file).replace(/\\/g, '/')).isDirectory() && fs.existsSync(path.join(componentsDir, file, CONSTANTS.COMPONENT_CONFIG_FILE_NAME).replace(/\\/g, '/')))
return fs.statSync(path.join(componentsDir, file).replace(/\\/g, '/')).isDirectory() && fs.existsSync(path.join(componentsDir, file, CONSTANTS.COMPONENT_CONFIG_FILE_NAME).replace(/\\/g, '/'));
});
}
let cmpIndexPathObj ={};
componentsLists.forEach((component)=>{
CONSTANTS.ENTRY_POINT_FILES.forEach((indexFile) => {
Iif(pathToBuild){
if(!cmpIndexPathObj[component] && fs.existsSync(path.join(component, indexFile))){
let indexPath = path.join(component, indexFile).replace(/\\/g, '/');
cmpIndexPathObj[component] = indexPath;
}
} else{
if(!cmpIndexPathObj[component] && fs.existsSync(path.join(componentsDir, component, indexFile))){
let indexPath = path.join(CONSTANTS.COMPONENTS_DIR, component, indexFile).replace(/\\/g, '/');
Iif(pathToBuild && customCmptDir){
indexPath = path.join(customCmptDir, component, indexFile).replace(/\\/g, '/');
}
cmpIndexPathObj[component] = indexPath;
}
}
});
});
const componentListPath = path.join(projectPath, CONSTANTS.COMPONENT_LIST_FILE_NAME).replace(/\\/g, '/');
ensureDirSync(componentListPath);
fs.writeFileSync(componentListPath, JSON.stringify(cmpIndexPathObj), { encoding: 'utf8' });
const entryPointPath = path.join(projectPath, CONSTANTS.ENTRY_POINT_FILE).replace(/\\/g, '/');
ensureDirSync(entryPointPath);
fs.writeFileSync(entryPointPath, CONSTANTS.CONTENT_ENTRY_POINT_FILE, { encoding: 'utf8' });
console.log(chalk.green("== Aggregating components to build - Components List - Completed =="));
};
const buildComponentMapJsFile = (buildFolderFullPath, pathToBuild) => {
console.log(chalk.magenta("== Creating map from the list of components - Component Map - Started =="));
const cmpntDefs = [];
const cmpntLocalizations = [];
const data = fs.readFileSync(path.join(CONSTANTS.BUILD_TOOL_LIB_PATH, CONSTANTS.UTILS).replace(/\\/g, '/'), 'utf8');
const indexOfStartDelimeter = data.indexOf(CONSTANTS.START_DELIMETER);
const lenthOfStartDelimeter = CONSTANTS.START_DELIMETER.length;
const tillStartDelimeterData = data.substring(0, indexOfStartDelimeter + lenthOfStartDelimeter);
const indexOfEndDelimeter = data.indexOf(CONSTANTS.END_DELIMETER);
const fromEndDelimeterData = data.substring(indexOfEndDelimeter);
// component_map
let componentMapData = `\n const ComponentMap = {\n`;
const rootForCustomCmptDir = pathToBuild ? path.dirname(pathToBuild) : undefined;
const customCmptDir = pathToBuild ? path.basename(pathToBuild) : undefined;
let projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, CONSTANTS.PROJECT_ROOT).replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, rootForCustomCmptDir).replace(/\\/g, '/');
}
const componentListJsonPath = path.join(projectPath, CONSTANTS.COMPONENT_LIST_FILE_NAME).replace(/\\/g, '/');
const componentListJSON = fs.readFileSync(componentListJsonPath, { encoding: 'utf8' });
const componentListJSONData = JSON.parse(componentListJSON);
Object.keys(componentListJSONData).forEach((component) => {
//const componentName = component.split(":")[0];
const componentName = (pathToBuild && rootForCustomCmptDir) ? component.substring(component.lastIndexOf("/") + 1) : component.split(":")[0];
const overrideQueryParam = component.split(":")[1];
let componentPath = path.join(projectPath, componentListJSONData[component]).replace(/\\/g, '/');
Iif(projectPath && rootForCustomCmptDir){
componentPath = path.join(componentListJSONData[component]).replace(/\\/g, '/');
}
const parameterForLoadable = overrideQueryParam ? `'${componentName}','${overrideQueryParam}'` : `'${componentName}'`;
componentMapData += `\t${componentName}: {\n\t modules: [\n\t\t loadable (() =>\n\t\t\t import(\n\t\t\t\t/* webpackChunkName: '${componentName}' */\n\t\t\t\t '${componentPath}'\n\t\t\t\t)\n\t\t\t, ${parameterForLoadable})\n\t\t]\n\t},\n`;
});
// remove the last ,
componentMapData = componentMapData.slice(0, -2);
componentMapData += `\n};\n`;
const modifiedComponentMapFile = tillStartDelimeterData + componentMapData + fromEndDelimeterData;
const componentListOutputPath = path.join(projectPath, CONSTANTS.COMPONENT_LIST_FILE_NAME_OUTPUT).replace(/\\/g, '/');
ensureDirSync(componentListOutputPath);
fs.writeFileSync(componentListOutputPath, modifiedComponentMapFile, { encoding: 'utf8' });
const tempList = getAggregateCompListForProject({
dirToLookUp: path.join(projectPath).replace(/\\/g, '/')
}, customCmptDir);
Eif (tempList) {
cmpntDefs.push(...tempList);
let componentsConfigPath = path.join(projectPath, "../", buildFolderFullPath, 'componentsconfig.json').replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
componentsConfigPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, buildFolderFullPath, 'componentsconfig.json').replace(/\\/g, '/');
}
ensureDirSync(componentsConfigPath);
fs.writeFileSync(componentsConfigPath, JSON.stringify(cmpntDefs), { encoding: 'utf8' });
}
const tempCompLocalizationList = getAggregateCompLocalizationForProject({
dirToLookUp: path.join(projectPath).replace(/\\/g, '/')
}, customCmptDir);
Eif (tempCompLocalizationList) {
cmpntLocalizations.push(...tempCompLocalizationList);
let componentsLocalizationPath = path.join(projectPath, "../", buildFolderFullPath, 'componentslocalization.json').replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
componentsLocalizationPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, buildFolderFullPath, 'componentslocalization.json').replace(/\\/g, '/');
}
ensureDirSync(componentsLocalizationPath);
fs.writeFileSync(componentsLocalizationPath, JSON.stringify(cmpntLocalizations), { encoding: 'utf8' });
}
console.log(chalk.green("== Creating map from the list of components - Component Map - Completed =="));
};
const webpackBuild = (buildFolderFullPath, buildFolderName, packageVersion, tokenParams, appStaticSVCUrl, v3, pathToBuild) => {
console.log(chalk.magenta("==== Webpack - Starting ===="));
const appStaticUrl = require('../helper').getAppstaticEnd(appStaticSVCUrl);
const token = helper.getB2SToken(tokenParams);
let orgId = '';
Iif (token) {
orgId = helper.getPayloadFromTkn(token, 'customer_org');
}
Eif (!orgId) {
orgId = '{ORG_ID}';
}
const rootForCustomCmptDir = pathToBuild ? path.dirname(pathToBuild) : undefined;
const customCmptDir = pathToBuild ? path.basename(pathToBuild) : undefined;
let projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, CONSTANTS.PROJECT_ROOT).replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, rootForCustomCmptDir).replace(/\\/g, '/');
}
const webpackPath = path.join(CONSTANTS.BUILD_TOOL_ROOT_PATH).replace(/\\/g, '/');
child_process.execSync(`npx webpack --mode=production --env buildFolderFullPath=${buildFolderFullPath} customCmptDir=${customCmptDir} outputPath=${path.join(CONSTANTS.CURRENT_WORKING_DIR, buildFolderFullPath)} buildFolderName=${buildFolderName} packageVersion=${packageVersion} appStaticUrl=${appStaticUrl} orgId=${orgId} v3=${v3} --config ${webpackPath}/webpack.config.js`, {
stdio: [0, 1, 2],
cwd: (pathToBuild && rootForCustomCmptDir) ? path.join(projectPath).replace(/\\/g, '/') : path.join(CONSTANTS.CURRENT_WORKING_DIR).replace(/\\/g, '/'),
});
console.log(chalk.magenta("==== Cleanup ===="));
fs.unlink(path.join(projectPath, CONSTANTS.COMPONENT_LIST_FILE_NAME_OUTPUT).replace(/\\/g, '/'), (err) => {
if (err) throw err;
});
fs.unlink(path.join(projectPath, CONSTANTS.COMPONENT_LIST_FILE_NAME).replace(/\\/g, '/'), (err) => {
if (err) throw err;
});
fs.unlink(path.join(projectPath, CONSTANTS.ENTRY_POINT_FILE).replace(/\\/g, '/'), (err) => {
if (err) throw err;
});
};
const getComponentAssets = (componentAssets) => {
const assets = [];
componentAssets.forEach(ele => {
if (ele?.name && ele?.name.split('/').length == 2) {
const fileName = ele?.name.split('/')[1];
assets.push(fileName);
} else Eif (ele?.name && typeof ele?.name === "string") {
assets.push(ele?.name);
}
});
return assets;
}
const buildManifestFile = (buildFolderFullPath, v3, pathToBuild) => {
console.log(chalk.magenta("== Creating manifest for the library version - Started =="));
const rootForCustomCmptDir = pathToBuild ? path.dirname(pathToBuild) : undefined;
let projectPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, CONSTANTS.PROJECT_ROOT).replace(/\\/g, '/');
const data = {};
try {
// read stats.json file
let statsPath = path.join(projectPath, '../', buildFolderFullPath, CONSTANTS.STATS).replace(/\\/g, '/');
Iif(pathToBuild && rootForCustomCmptDir){
statsPath = path.join(CONSTANTS.CURRENT_WORKING_DIR, buildFolderFullPath, CONSTANTS.STATS).replace(/\\/g, '/');
}
const stats = fs.readFileSync(statsPath, 'utf-8');
const statsObj = JSON.parse(stats);
const components = {};
Object.keys(statsObj?.namedChunkGroups).forEach(component => {
if (component !== 'main') {
const value = [];
Eif (statsObj?.namedChunkGroups[component]?.assets?.length) {
value.push(...getComponentAssets(statsObj.namedChunkGroups[component].assets));
}
Eif (statsObj?.namedChunkGroups[component]?.auxiliaryAssets?.length) {
value.push(...getComponentAssets(statsObj.namedChunkGroups[component].auxiliaryAssets));
}
components[component] = value;
}
});
data.components = components;
const { stdout } = child_process.spawnSync('npm', ['list', '--depth=0'], { encoding: 'utf-8' });
const npmListOutput = stdout;
Iif (npmListOutput && npmListOutput.indexOf(CONSTANTS.COSMOS_PKG_PATTERN) !== -1) {
const cosmosVersion = helper.getVersionFromListOutput(npmListOutput, CONSTANTS.COSMOS_PKG_PATTERN);
data.versions = cosmosVersion;
}
let manifestPath;
if (!v3) {
manifestPath = path.join(projectPath, '../', buildFolderFullPath, 'chunks', CONSTANTS.MANIFEST).replace(/\\/g, '/');
} else E{
manifestPath = path.join(projectPath, '../', buildFolderFullPath, CONSTANTS.MANIFEST).replace(/\\/g, '/');
}
ensureDirSync(manifestPath);
fs.writeFileSync(manifestPath, JSON.stringify(data), { encoding: 'utf8' });
console.log(chalk.magenta("== Creating manifest for the library version - completed =="));
fs.unlink(path.join(projectPath, '../', buildFolderFullPath, CONSTANTS.STATS).replace(/\\/g, '/'), (err) => {
if (err) throw err;
});
}
catch (e) {
console.log('Warning in manifest file generation.');
console.log(e);
}
}
/**
* This function generate build for library
* @param {string} libraryName optional library name, else it will take value from package.json name
* @param {string} libraryVersion optional library version, else it will take value from package.json version
* @param {string} tokenParam optional B2S token for appstatic service, else it will take value from constant.js B2STOKEN
* @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
*/
const buildLib = (libraryName, libraryVersion, tokenParam, appStaticSVCUrl, v3, pathToBuild) => {
console.log(chalk.magenta("==== Library Build - Starting ===="));
const { buildFolderFullPath, buildFolderName, packageVersion } = helper.getBuildFolderName(libraryName, libraryVersion);
// build entry point and cmpt-list.json
buildComponentsListFile(buildFolderName, packageVersion, false, pathToBuild);
// build component map
buildComponentMapJsFile(buildFolderFullPath, pathToBuild);
// then build env
webpackBuild(buildFolderFullPath, buildFolderName, packageVersion, tokenParam, appStaticSVCUrl, v3, pathToBuild);
// generate manifest file
buildManifestFile(buildFolderFullPath, v3, pathToBuild);
};
const buildLibV3 = (libraryName, libraryVersion, tokenParam, appStaticSVCUrl, pathToBuild) => { //src/app/_components/custom-constellation
buildLib(libraryName, libraryVersion, tokenParam, appStaticSVCUrl, true, pathToBuild);
};
const getAggregateCompListForProject = (options, customCmptDir) => {
const { dirToLookUp } = options;
const cmpntDefs = [];
const componentList = glob.sync(path.join(dirToLookUp, CONSTANTS.COMPONENT_LIST_FILE_NAME).replace(/\\/g, '/'));
Iif (!componentList || componentList.length === 0) {
console.error(chalk.red('No component-list.json found'));
process.exit(1);
}
try {
const compMap = fs.readFileSync(componentList[0], 'utf-8');
const compMapObj = JSON.parse(compMap);
let definitions = glob.sync(`${dirToLookUp}/components/**/config.json`.replace(/\\/g, '/'));
Iif(customCmptDir){
definitions = glob.sync(`${dirToLookUp}/${customCmptDir}/**/config.json`.replace(/\\/g, '/'));
}
Iif (!definitions) {
console.info(chalk.magenta(`No components found`));
process.exit(1);
}
definitions.forEach((config) => {
config = config.replace(/\\/g, '/');
let cmpntNamePath = config.replace(`${dirToLookUp}/components/`, '').replace('/config.json', '');
Iif(customCmptDir){
cmpntNamePath = config.replace(`${dirToLookUp}/${customCmptDir}/`, '').replace('/config.json', '');
}
const cmpntName = Object.keys(compMapObj).find(key => compMapObj[key].includes(cmpntNamePath));
Eif (cmpntName) {
const data = fs.readFileSync(config, 'utf-8');
try {
const cfg = JSON.parse(data);
cmpntDefs.push(cfg);
} catch (ex) {
console.log(ex);
}
}
});
} catch (err) {
console.log(err);
process.exit(1);
}
return cmpntDefs;
};
const getAggregateCompLocalizationForProject = (options, customCmptDir) => {
const cmpntLocalizations = [];
const { dirToLookUp } = options;
const componentList = glob.sync(path.join(dirToLookUp, CONSTANTS.COMPONENT_LIST_FILE_NAME).replace(/\\/g, '/'));
Iif (!componentList || componentList.length === 0) {
console.error(chalk.red('No component-list.json found'));
return;
}
try {
const compMap = fs.readFileSync(componentList[0], 'utf-8');
const compMapObj = JSON.parse(compMap);
let definitions = glob.sync(`${dirToLookUp}/components/**/localizations.json`.replace(/\\/g, '/'));
Iif(customCmptDir){
definitions = glob.sync(`${dirToLookUp}/${customCmptDir}/**/localizations.json`.replace(/\\/g, '/'));
}
Iif (!definitions || definitions.length === 0) {
console.info(chalk.magenta(`No components localization found`));
return;
}
definitions.forEach((config) => {
// get cmpnt name
let cmpntNamePath = config.replace(`${dirToLookUp}/components/`.replace(/\\/g, '/'), '').replace('/localizations.json'.replace(/\\/g, '/'), '');
Iif(customCmptDir){
cmpntNamePath = config.replace(`${dirToLookUp}/${customCmptDir}/`.replace(/\\/g, '/'), '').replace('/localizations.json'.replace(/\\/g, '/'), '');
}
const cmpntName = Object.keys(compMapObj).find(key => compMapObj[key].includes(cmpntNamePath));
// read cmpnt localization file content
Eif (cmpntName) {
const data = fs.readFileSync(config, 'utf-8');
// key : value
const cmpntLocalization = { [cmpntName]: JSON.parse(data) };
// push to []
cmpntLocalizations.push(cmpntLocalization);
}
});
} catch (err) {
console.log(err);
}
// return
return cmpntLocalizations;
}
function getComponentsWithConfig(dir, configFileName) {
const results = [];
function recurse(currentDir) {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
const subDir = path.join(currentDir, entry.name);
const configPath = path.join(subDir, configFileName);
if (fs.existsSync(configPath)) {
results.push(subDir);
}
recurse(subDir); // Keep searching deeper
}
}
}
recurse(dir);
return results;
}
module.exports = {
buildLib,
buildLibV3,
buildComponentsListFile,
buildComponentMapJsFile
} |