|
| 1 | +const { execFileSync } = require('child_process'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const readline = require('readline'); |
| 5 | + |
| 6 | +try { |
| 7 | + const componentsProjectRootPath = process.cwd(); |
| 8 | + const componentsInPackagePath = path.join(componentsProjectRootPath, 'packages', 'angular-sdk-components'); |
| 9 | + const overridesPackagePath = path.join(componentsProjectRootPath, 'packages', 'angular-sdk-overrides'); |
| 10 | + const distInComponentsPath = path.join(componentsProjectRootPath, 'dist'); |
| 11 | + const componentsInDistPath = path.join(distInComponentsPath, 'angular-sdk-components'); |
| 12 | + |
| 13 | + /** Create readline interface to ask for angular-sdk project path */ |
| 14 | + const readLineInterface = readline.createInterface({ |
| 15 | + input: process.stdin, |
| 16 | + output: process.stdout |
| 17 | + }); |
| 18 | + |
| 19 | + readLineInterface.question('Please enter the absolute path of angular-sdk project: ', sdkProjectPathInput => { |
| 20 | + readLineInterface.close(); |
| 21 | + const sdkProjectRootPath = path.resolve(sdkProjectPathInput); |
| 22 | + |
| 23 | + /** Delete existing dist folder before build */ |
| 24 | + if (fs.existsSync(distInComponentsPath)) { |
| 25 | + console.log(`---- Removing existing dist folder: ${distInComponentsPath} ----`); |
| 26 | + fs.rmSync(distInComponentsPath, { recursive: true, force: true }); |
| 27 | + } |
| 28 | + |
| 29 | + /** Build angular-sdk-components in packages folder */ |
| 30 | + console.log(`---- Building angular-sdk-components at: ${componentsInPackagePath} ----`); |
| 31 | + execFileSync('ng', ['build', 'angular-sdk-components'], { cwd: componentsInPackagePath, stdio: 'inherit' }); |
| 32 | + |
| 33 | + /** Package angular-sdk-components in dist folder */ |
| 34 | + console.log(`---- Packing npm package in: ${componentsInDistPath} ----`); |
| 35 | + execFileSync('npm', ['pack'], { cwd: componentsInDistPath, stdio: 'inherit' }); |
| 36 | + |
| 37 | + /** Find the generated 'angular-sdk-components' .tgz file */ |
| 38 | + const componentsTgzFile = fs |
| 39 | + .readdirSync(componentsInDistPath) |
| 40 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-angular-sdk-components') > -1); |
| 41 | + if (!componentsTgzFile) { |
| 42 | + throw new Error('No .tgz file found in dist folder!'); |
| 43 | + } |
| 44 | + |
| 45 | + const componentsTgzPath = path.join(path.join(distInComponentsPath, 'angular-sdk-components'), componentsTgzFile); |
| 46 | + const componentTargetTgzPath = path.join(sdkProjectRootPath, componentsTgzFile); |
| 47 | + |
| 48 | + /** Delete components .tgz file if exists in angular-sdk folder */ |
| 49 | + const existingTgzInSDK = fs |
| 50 | + .readdirSync(sdkProjectRootPath) |
| 51 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-angular-sdk-components') > -1); |
| 52 | + if (existingTgzInSDK) { |
| 53 | + console.log(`---- Removing old package: ${existingTgzInSDK} ----`); |
| 54 | + fs.unlinkSync(path.join(sdkProjectRootPath, existingTgzInSDK)); |
| 55 | + } |
| 56 | + |
| 57 | + console.log(`---- Copying ${componentsTgzFile} to angular-sdk folder: ${sdkProjectRootPath} ----`); |
| 58 | + fs.copyFileSync(componentsTgzPath, componentTargetTgzPath); |
| 59 | + |
| 60 | + /** Build and package 'angular-sdk-overrides' */ |
| 61 | + |
| 62 | + /** Delete existing .tgz file before building */ |
| 63 | + const existingOverridesTgzInComponents = fs |
| 64 | + .readdirSync(overridesPackagePath) |
| 65 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-angular-sdk-overrides') > -1); |
| 66 | + if (existingOverridesTgzInComponents) { |
| 67 | + console.log(`---- Removing existing tgz: ${existingOverridesTgzInComponents} ----`); |
| 68 | + fs.unlinkSync(path.join(overridesPackagePath, existingOverridesTgzInComponents)); |
| 69 | + } |
| 70 | + |
| 71 | + console.log('Building angular-sdk-overrides.......'); |
| 72 | + execFileSync('npm', ['run', 'build-sdk'], { cwd: componentsProjectRootPath, stdio: 'inherit' }); |
| 73 | + execFileSync('npm', ['run', 'build-overrides'], { cwd: componentsProjectRootPath, stdio: 'inherit' }); |
| 74 | + execFileSync('npm', ['pack'], { cwd: overridesPackagePath, stdio: 'inherit' }); |
| 75 | + |
| 76 | + const overridesTgzFile = fs |
| 77 | + .readdirSync(overridesPackagePath) |
| 78 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-angular-sdk-overrides') > -1); |
| 79 | + if (!overridesTgzFile) { |
| 80 | + throw new Error('No overrides .tgz file found'); |
| 81 | + } |
| 82 | + |
| 83 | + const overridesTgzPath = path.join(overridesPackagePath, overridesTgzFile); |
| 84 | + const overridesTargetTgzPath = path.join(sdkProjectRootPath, overridesTgzFile); |
| 85 | + |
| 86 | + const existingOverridesTgzInSDK = fs |
| 87 | + .readdirSync(sdkProjectRootPath) |
| 88 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-angular-sdk-overrides') > -1); |
| 89 | + if (existingOverridesTgzInSDK) { |
| 90 | + console.log(`---- Removing old package: ${existingOverridesTgzInSDK} ----`); |
| 91 | + fs.unlinkSync(path.join(sdkProjectRootPath, existingOverridesTgzInSDK)); |
| 92 | + } |
| 93 | + |
| 94 | + console.log(`---- Copying ${overridesTgzFile} to angular-sdk folder: ${sdkProjectRootPath} ----`); |
| 95 | + fs.copyFileSync(overridesTgzPath, overridesTargetTgzPath); |
| 96 | + |
| 97 | + /** Install the built packages in angular-sdk project */ |
| 98 | + console.log(`---- Installing ${componentsTgzFile} in angular-sdk ----`); |
| 99 | + execFileSync('npm', ['install', `./${componentsTgzFile}`], { cwd: sdkProjectRootPath, stdio: 'inherit' }); |
| 100 | + console.log("Done!!! 'angular-sdk' now uses local build of angular-sdk-components."); |
| 101 | + console.log(`---- Installing ${overridesTgzFile} in angular-sdk ----`); |
| 102 | + execFileSync('npm', ['install', `./${overridesTgzFile}`], { cwd: sdkProjectRootPath, stdio: 'inherit' }); |
| 103 | + console.log("Done!!! 'angular-sdk' now uses local build of angular-sdk-overrides."); |
| 104 | + }); |
| 105 | +} catch (error) { |
| 106 | + console.error('Error:', error.message); |
| 107 | + process.exit(1); |
| 108 | +} |
0 commit comments