|
| 1 | +const { execSync } = require('child_process') |
| 2 | +const path = require('path') |
| 3 | +const fs = require('fs') |
| 4 | + |
| 5 | +const SCRIPT_DIR = __dirname |
| 6 | +const ROOT_DIR = path.resolve(SCRIPT_DIR, '../../../') |
| 7 | +const CHAT_CLIENT_PATH = path.resolve(ROOT_DIR, 'chat-client') |
| 8 | +const CODE_WHISPERER_RUNTIMES_PATH = path.resolve(ROOT_DIR, 'app/aws-lsp-codewhisperer-runtimes') |
| 9 | +const CUSTOM_WEBPACK_PATH = path.join(SCRIPT_DIR, '../custom-webpack-config.js') |
| 10 | + |
| 11 | +function executeCommand(command, cwd) { |
| 12 | + try { |
| 13 | + console.log(`Executing: ${command}`) |
| 14 | + execSync(`${command}`, { |
| 15 | + cwd, |
| 16 | + stdio: 'pipe', |
| 17 | + }) |
| 18 | + } catch (error) { |
| 19 | + console.error(`Error executing command "${command}": ${error.message}`) |
| 20 | + if (error.signal === 'SIGINT' || error.signal === 'SIGTERM') { |
| 21 | + cleanupInterruptedState() |
| 22 | + } |
| 23 | + if (error.message.includes('Command failed: npm run clean')) { |
| 24 | + // ignore this error as it is expected to fail in case clean is run without install |
| 25 | + return |
| 26 | + } |
| 27 | + process.exit(1) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function handleWebpackConfig(action) { |
| 32 | + const webpackPath = path.join(SCRIPT_DIR, '../webpack.config.js') |
| 33 | + const backupPath = path.join(SCRIPT_DIR, '../webpack.config.js.backup') |
| 34 | + |
| 35 | + if (action === 'backup' && fs.existsSync(webpackPath)) { |
| 36 | + fs.renameSync(webpackPath, backupPath) |
| 37 | + fs.copyFileSync(CUSTOM_WEBPACK_PATH, webpackPath) |
| 38 | + } else if (action === 'restore' && fs.existsSync(backupPath)) { |
| 39 | + if (fs.existsSync(webpackPath)) { |
| 40 | + fs.unlinkSync(webpackPath) |
| 41 | + } |
| 42 | + fs.renameSync(backupPath, webpackPath) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function cleanupInterruptedState() { |
| 47 | + const webpackPath = path.join(SCRIPT_DIR, '../webpack.config.js') |
| 48 | + const backupPath = path.join(SCRIPT_DIR, '../webpack.config.js.backup') |
| 49 | + |
| 50 | + if (fs.existsSync(backupPath)) { |
| 51 | + console.log('Found backup webpack config from previous interrupted run. Restoring...') |
| 52 | + if (fs.existsSync(webpackPath)) { |
| 53 | + fs.unlinkSync(webpackPath) |
| 54 | + } |
| 55 | + fs.renameSync(backupPath, webpackPath) |
| 56 | + console.log('Restored original webpack config.') |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function createServerArtifact() { |
| 61 | + try { |
| 62 | + // Clean up any interrupted state from previous runs |
| 63 | + cleanupInterruptedState() |
| 64 | + |
| 65 | + if (!fs.existsSync(ROOT_DIR)) { |
| 66 | + throw new Error(`Directory not found: ${ROOT_DIR}`) |
| 67 | + } |
| 68 | + |
| 69 | + if (!fs.existsSync(CUSTOM_WEBPACK_PATH)) { |
| 70 | + throw new Error(`Custom webpack config not found: ${CUSTOM_WEBPACK_PATH}`) |
| 71 | + } |
| 72 | + |
| 73 | + console.log('\nStep 1: Running clean in root directory...') |
| 74 | + executeCommand('npm run clean', ROOT_DIR) |
| 75 | + |
| 76 | + console.log('\nStep 2: Installing dependencies in root directory...') |
| 77 | + executeCommand('npm i', ROOT_DIR) |
| 78 | + |
| 79 | + console.log('\nStep 3: Running compile in root directory...') |
| 80 | + executeCommand('npm run compile', ROOT_DIR) |
| 81 | + |
| 82 | + console.log('\nStep 4: Running package in target directory...') |
| 83 | + |
| 84 | + handleWebpackConfig('backup') |
| 85 | + |
| 86 | + try { |
| 87 | + executeCommand('npm run package', CODE_WHISPERER_RUNTIMES_PATH) |
| 88 | + } finally { |
| 89 | + handleWebpackConfig('restore') |
| 90 | + } |
| 91 | + |
| 92 | + console.log('\nServer artifact created successfully! 🎉') |
| 93 | + } catch (error) { |
| 94 | + console.error('\nServer artifact creation failed:', error.message) |
| 95 | + process.exit(1) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +function createClientArtifact() { |
| 100 | + try { |
| 101 | + if (!fs.existsSync(CHAT_CLIENT_PATH)) { |
| 102 | + throw new Error(`Chat client path not found: ${CHAT_CLIENT_PATH}`) |
| 103 | + } |
| 104 | + executeCommand('npm run compile', CHAT_CLIENT_PATH) |
| 105 | + console.log('\nClient artifact created successfully! 🎉') |
| 106 | + } catch (error) { |
| 107 | + console.error('\nClient artifact creation failed:', error.message) |
| 108 | + process.exit(1) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +try { |
| 113 | + createServerArtifact() |
| 114 | + createClientArtifact() |
| 115 | + console.log( |
| 116 | + '\nServer artifact created at: language-servers/app/aws-lsp-codewhisperer-runtimes/build/aws-lsp-codewhisperer.js' |
| 117 | + ) |
| 118 | + console.log('\nClient artifact created at: language-servers/chat-client/build/amazonq-ui.js') |
| 119 | +} catch (er) { |
| 120 | + console.error('\nArtifacts creation failed:', er.message) |
| 121 | + process.exit(1) |
| 122 | +} |
0 commit comments