|
| 1 | +/** |
| 2 | + * bundle.mjs — Single command to build CLI + CDK constructs into one tarball. |
| 3 | + * |
| 4 | + * This is a testing-only workflow. It does NOT modify the default build or |
| 5 | + * deployment flow. The normal `npm run build` + `npm pack` pipeline is unchanged. |
| 6 | + * |
| 7 | + * What this script does differently: after building both packages normally, it |
| 8 | + * packs the CDK constructs into a tarball and places it in the CLI's dist/assets/. |
| 9 | + * At `agentcore create` time, CDKRenderer detects this tarball and installs it |
| 10 | + * after the normal `npm install`, overriding the registry version. |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * node scripts/bundle.mjs |
| 14 | + * npm run bundle |
| 15 | + * |
| 16 | + * Environment variables: |
| 17 | + * AGENTCORE_CDK_PATH — absolute path to the agentcore-l3-cdk-constructs repo |
| 18 | + */ |
| 19 | +import { execFileSync } from 'node:child_process'; |
| 20 | +import * as fs from 'node:fs'; |
| 21 | +import * as path from 'node:path'; |
| 22 | +import { fileURLToPath } from 'node:url'; |
| 23 | + |
| 24 | +const __filename = fileURLToPath(import.meta.url); |
| 25 | +const __dirname = path.dirname(__filename); |
| 26 | +const cliRoot = path.resolve(__dirname, '..'); |
| 27 | + |
| 28 | +const CDK_REPO_URL = 'https://github.com/aws/agentcore-l3-cdk-constructs.git'; |
| 29 | + |
| 30 | +function log(msg) { |
| 31 | + console.log(`\n[bundle] ${msg}`); |
| 32 | +} |
| 33 | + |
| 34 | +function run(cmd, args = [], opts = {}) { |
| 35 | + const display = [cmd, ...args].join(' '); |
| 36 | + console.log(` > ${display}`); |
| 37 | + execFileSync(cmd, args, { stdio: 'inherit', ...opts }); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Resolve the CDK constructs repo path. Priority: |
| 42 | + * 1. AGENTCORE_CDK_PATH env var |
| 43 | + * 2. Sibling directory ../agentcore-l3-cdk-constructs |
| 44 | + * 3. Clone from GitHub into a temp directory under the CLI repo |
| 45 | + */ |
| 46 | +function resolveCdkPath() { |
| 47 | + // 1. Env var |
| 48 | + if (process.env.AGENTCORE_CDK_PATH) { |
| 49 | + const p = path.resolve(process.env.AGENTCORE_CDK_PATH); |
| 50 | + if (fs.existsSync(path.join(p, 'package.json'))) { |
| 51 | + log(`Using CDK constructs from AGENTCORE_CDK_PATH: ${p}`); |
| 52 | + return p; |
| 53 | + } |
| 54 | + console.warn(` WARNING: AGENTCORE_CDK_PATH=${p} does not contain package.json, ignoring.`); |
| 55 | + } |
| 56 | + |
| 57 | + // 2. Sibling directory |
| 58 | + const sibling = path.resolve(cliRoot, '..', 'agentcore-l3-cdk-constructs'); |
| 59 | + if (fs.existsSync(path.join(sibling, 'package.json'))) { |
| 60 | + log(`Using CDK constructs from sibling directory: ${sibling}`); |
| 61 | + return sibling; |
| 62 | + } |
| 63 | + |
| 64 | + // 3. Clone latest from GitHub |
| 65 | + const cloneDir = path.join(cliRoot, '.cdk-constructs-clone'); |
| 66 | + log(`CDK constructs repo not found locally. Cloning latest from GitHub...`); |
| 67 | + |
| 68 | + if (fs.existsSync(cloneDir)) { |
| 69 | + log('Pulling latest changes...'); |
| 70 | + run('git', ['pull', 'origin', 'main'], { cwd: cloneDir }); |
| 71 | + } else { |
| 72 | + run('git', ['clone', '--depth', '1', CDK_REPO_URL, cloneDir]); |
| 73 | + } |
| 74 | + |
| 75 | + return cloneDir; |
| 76 | +} |
| 77 | + |
| 78 | +// --------------------------------------------------------------------------- |
| 79 | +// Main |
| 80 | +// --------------------------------------------------------------------------- |
| 81 | + |
| 82 | +log('Starting bundle process...'); |
| 83 | + |
| 84 | +const timestamp = Math.floor(Date.now() / 1000); |
| 85 | +log(`Bundle timestamp: ${timestamp}`); |
| 86 | + |
| 87 | +// Helper to bump a package version with a unique e2e timestamp tag. |
| 88 | +// Saves the original version so it can be restored after packing. |
| 89 | +function bumpVersion(pkgDir) { |
| 90 | + const pkgJsonPath = path.join(pkgDir, 'package.json'); |
| 91 | + const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')); |
| 92 | + const originalVersion = pkg.version; |
| 93 | + const baseVersion = originalVersion.split('-')[0]; |
| 94 | + pkg.version = `${baseVersion}-${timestamp}`; |
| 95 | + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 96 | + log(`Bumped ${pkg.name} version: ${originalVersion} -> ${pkg.version}`); |
| 97 | + return { pkgJsonPath, originalVersion, bumpedVersion: pkg.version }; |
| 98 | +} |
| 99 | + |
| 100 | +function restoreVersion({ pkgJsonPath, originalVersion }) { |
| 101 | + const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')); |
| 102 | + pkg.version = originalVersion; |
| 103 | + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 104 | +} |
| 105 | + |
| 106 | +// Step 1: Resolve and build CDK constructs |
| 107 | +const cdkPath = resolveCdkPath(); |
| 108 | + |
| 109 | +log('Installing CDK constructs dependencies...'); |
| 110 | +run('npm', ['install'], { cwd: cdkPath }); |
| 111 | + |
| 112 | +log('Building CDK constructs...'); |
| 113 | +run('npm', ['run', 'build'], { cwd: cdkPath }); |
| 114 | + |
| 115 | +// Step 2: Bump CDK version and pack into a tarball |
| 116 | +const cdkVersionInfo = bumpVersion(cdkPath); |
| 117 | +try { |
| 118 | + log('Packing CDK constructs...'); |
| 119 | + run('npm', ['pack'], { cwd: cdkPath }); |
| 120 | +} finally { |
| 121 | + restoreVersion(cdkVersionInfo); |
| 122 | +} |
| 123 | + |
| 124 | +const cdkTarballName = `aws-agentcore-cdk-${cdkVersionInfo.bumpedVersion}.tgz`; |
| 125 | +const cdkTarballSrc = path.join(cdkPath, cdkTarballName); |
| 126 | + |
| 127 | +if (!fs.existsSync(cdkTarballSrc)) { |
| 128 | + console.error(`ERROR: Expected CDK tarball at ${cdkTarballSrc} but not found.`); |
| 129 | + process.exit(1); |
| 130 | +} |
| 131 | + |
| 132 | +// Step 3: Build CLI normally (no modifications to copy-assets) |
| 133 | +log('Installing CLI dependencies...'); |
| 134 | +run('npm', ['install'], { cwd: cliRoot }); |
| 135 | + |
| 136 | +log('Building CLI...'); |
| 137 | +run('npm', ['run', 'build'], { cwd: cliRoot }); |
| 138 | + |
| 139 | +// Step 4: Copy CDK tarball into dist/assets/ so CDKRenderer can detect it |
| 140 | +const bundledTarballDest = path.join(cliRoot, 'dist', 'assets', 'bundled-agentcore-cdk.tgz'); |
| 141 | +fs.copyFileSync(cdkTarballSrc, bundledTarballDest); |
| 142 | +log(`Placed CDK tarball at ${bundledTarballDest}`); |
| 143 | + |
| 144 | +// Step 5: Bump CLI version and pack into final tarball (includes the bundled CDK tarball) |
| 145 | +const cliVersionInfo = bumpVersion(cliRoot); |
| 146 | +try { |
| 147 | + log('Packing CLI tarball...'); |
| 148 | + run('npm', ['pack'], { cwd: cliRoot }); |
| 149 | +} finally { |
| 150 | + restoreVersion(cliVersionInfo); |
| 151 | +} |
| 152 | + |
| 153 | +const cliTarballName = `aws-agentcore-${cliVersionInfo.bumpedVersion}.tgz`; |
| 154 | +const cliTarballPath = path.join(cliRoot, cliTarballName); |
| 155 | + |
| 156 | +if (fs.existsSync(cliTarballPath)) { |
| 157 | + log(`Done! Tarball: ${cliTarballPath}`); |
| 158 | + log(`Install with: npm install ${cliTarballPath}`); |
| 159 | + log('When you run agentcore create, the bundled CDK constructs will be installed automatically.'); |
| 160 | +} else { |
| 161 | + log(`Done! Check ${cliRoot} for the .tgz file.`); |
| 162 | +} |
0 commit comments