|
| 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 componentsPackagePath = path.join(componentsProjectRootPath, 'packages', 'react-sdk-components'); |
| 9 | + |
| 10 | + const readLineInterface = readline.createInterface({ |
| 11 | + input: process.stdin, |
| 12 | + output: process.stdout |
| 13 | + }); |
| 14 | + |
| 15 | + readLineInterface.question('Please enter the absolute path of react-sdk project: ', sdkProjectPathInput => { |
| 16 | + readLineInterface.close(); |
| 17 | + |
| 18 | + const sdkProjectRootPath = path.resolve(sdkProjectPathInput.trim()); |
| 19 | + |
| 20 | + if (!fs.existsSync(sdkProjectRootPath)) { |
| 21 | + throw new Error(`Path does not exist: ${sdkProjectRootPath}`); |
| 22 | + } |
| 23 | + |
| 24 | + if (!fs.existsSync(path.join(sdkProjectRootPath, 'package.json'))) { |
| 25 | + throw new Error(`No package.json found at: ${sdkProjectRootPath}`); |
| 26 | + } |
| 27 | + |
| 28 | + const existingTgzInComponents = fs |
| 29 | + .readdirSync(componentsPackagePath) |
| 30 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-react-sdk-components') > -1); |
| 31 | + |
| 32 | + if (existingTgzInComponents) { |
| 33 | + console.log(`---- Removing existing tgz: ${existingTgzInComponents} ----`); |
| 34 | + fs.unlinkSync(path.join(componentsPackagePath, existingTgzInComponents)); |
| 35 | + } |
| 36 | + |
| 37 | + console.log(`---- Building react-sdk-components at: ${componentsProjectRootPath} ----`); |
| 38 | + execFileSync('npm', ['run', 'build-sdk'], { cwd: componentsProjectRootPath, stdio: 'inherit' }); |
| 39 | + |
| 40 | + console.log(`---- Packing npm package in: ${componentsPackagePath} ----`); |
| 41 | + execFileSync('npm', ['pack'], { cwd: componentsPackagePath, stdio: 'inherit' }); |
| 42 | + |
| 43 | + const componentsTgzFile = fs |
| 44 | + .readdirSync(componentsPackagePath) |
| 45 | + .find(file => file.endsWith('.tgz') && file.indexOf('pega-react-sdk-components') > -1); |
| 46 | + |
| 47 | + if (!componentsTgzFile) { |
| 48 | + throw new Error('No react-sdk-components .tgz file found after packing.'); |
| 49 | + } |
| 50 | + |
| 51 | + const componentsTgzPath = path.join(componentsPackagePath, componentsTgzFile); |
| 52 | + const componentTargetTgzPath = path.join(sdkProjectRootPath, componentsTgzFile); |
| 53 | + |
| 54 | + const existingTgzInSDK = fs.readdirSync(sdkProjectRootPath).find(file => file.endsWith('.tgz') && file.indexOf('pega-react-sdk-components') > -1); |
| 55 | + |
| 56 | + if (existingTgzInSDK) { |
| 57 | + console.log(`---- Removing old package: ${existingTgzInSDK} ----`); |
| 58 | + fs.unlinkSync(path.join(sdkProjectRootPath, existingTgzInSDK)); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(`---- Copying ${componentsTgzFile} to react-sdk folder: ${sdkProjectRootPath} ----`); |
| 62 | + fs.copyFileSync(componentsTgzPath, componentTargetTgzPath); |
| 63 | + |
| 64 | + console.log(`---- Installing ${componentsTgzFile} in react-sdk ----`); |
| 65 | + execFileSync('npm', ['install', `./${componentsTgzFile}`], { cwd: sdkProjectRootPath, stdio: 'inherit' }); |
| 66 | + |
| 67 | + console.log("Done!!! 'react-sdk' now uses local build of react-sdk-components."); |
| 68 | + }); |
| 69 | +} catch (error) { |
| 70 | + console.error('Error:', error.message); |
| 71 | + process.exit(1); |
| 72 | +} |
0 commit comments