|
| 1 | +const { exec } = require('child_process'); |
| 2 | + |
| 3 | +// Array of folder names |
| 4 | +const foldersToBundle = [ |
| 5 | + 'core', |
| 6 | + 'client', |
| 7 | + 'client-frameworks-support/testing-utilities', |
| 8 | + 'client-frameworks-support/client-support-angular', |
| 9 | + 'container', |
| 10 | + 'plugins' |
| 11 | +]; |
| 12 | + |
| 13 | +const foldersToBuild = ['test/e2e-test-application']; |
| 14 | + |
| 15 | +let timeToBundle = 0; |
| 16 | +let timeToBuild = 0; |
| 17 | + |
| 18 | +// Check for verbose flag |
| 19 | +const verboseFlagIndex = process.argv.indexOf('--verbose'); |
| 20 | +let isVerbose = verboseFlagIndex !== -1; |
| 21 | + |
| 22 | +// Function to run 'npm run <operation>' in a folder |
| 23 | +function runCommand(folder, index, totalFolders, operation) { |
| 24 | + const startTime = new Date(); |
| 25 | + |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + const command = `cd ${folder} && npm run ${operation}`; |
| 28 | + |
| 29 | + exec(command, (error, stdout, stderr) => { |
| 30 | + const endTime = new Date(); |
| 31 | + const elapsedTime = (endTime - startTime) / 1000; |
| 32 | + timeToBundle += operation === 'bundle' ? elapsedTime : 0; |
| 33 | + timeToBuild += operation === 'build' ? elapsedTime : 0; |
| 34 | + |
| 35 | + if (error) { |
| 36 | + console.error(`\x1b[31mError when trying to ${operation} packages in ${folder} \x1b[0m`); |
| 37 | + reject(error); |
| 38 | + } else { |
| 39 | + console.log( |
| 40 | + `\x1b[36m[${index + |
| 41 | + 1}/${totalFolders}]\x1b[0m: \x1b[33m${folder}\x1b[0m ${operation} successful (\x1b[33m${elapsedTime.toFixed( |
| 42 | + 2 |
| 43 | + )}s\x1b[0m)` |
| 44 | + ); |
| 45 | + // Print logs if needed |
| 46 | + if (isVerbose) { |
| 47 | + console.log('VERBOSE:', stdout); |
| 48 | + } |
| 49 | + resolve(); |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +// Function to run 'npm run bundle' in all specified folders |
| 56 | +async function runCommandInAllFolders(folders, operation) { |
| 57 | + console.log( |
| 58 | + `\x1b[36m\n\nRunning (npm run ${operation}) in these folders in the following order:\x1b[0m ${ |
| 59 | + isVerbose ? '\x1b[41m\x1b[37m(VERBOSE)\x1b[0m' : '' |
| 60 | + }` |
| 61 | + ); |
| 62 | + for (const folder of folders) { |
| 63 | + console.log('- ' + folder); |
| 64 | + } |
| 65 | + |
| 66 | + for (let i = 0; i < folders.length; i++) { |
| 67 | + console.log(`\n\x1b[37m${operation} \x1b[33m${folders[i]}\x1b[0m ...`); |
| 68 | + await runCommand(folders[i], i, folders.length, operation); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Run the 'npm run bundle' command in the specified folders |
| 73 | +runCommandInAllFolders(foldersToBundle, 'bundle').then(() => { |
| 74 | + console.log(`Bundle finished in ${timeToBundle.toFixed(2)}s`); |
| 75 | + |
| 76 | + // Run the 'npm run build' command in the specified folders |
| 77 | + runCommandInAllFolders(foldersToBuild, 'build').then(() => { |
| 78 | + console.log(`Build finished in ${timeToBuild.toFixed(2)}s\n`); |
| 79 | + console.log(`\nBuild+Bundle finished in ${(timeToBuild + timeToBundle).toFixed(2)}s\n`); |
| 80 | + }, errorHandler); |
| 81 | +}, errorHandler); |
| 82 | + |
| 83 | +/** |
| 84 | + * Function to handle the error case for promises |
| 85 | + * @param {*} error error |
| 86 | + */ |
| 87 | +function errorHandler(error) { |
| 88 | + console.error('Stopping execution of the process due to error:', error); |
| 89 | + process.exit(1); |
| 90 | +} |
0 commit comments