|
| 1 | +#!/usr/bin/env node |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +// Can be used in postinstall script like so: |
| 6 | +// "postinstall": "node ./scripts/install-peer-dependency.js collection-hooks:property-filter-token-groups" |
| 7 | +// where "collection-hooks" is the package to fetch and "property-filter-token-groups" is the branch name in GitHub. |
| 8 | + |
| 9 | +const { execSync } = require('child_process'); |
| 10 | +const os = require('os'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +const getModules = packageName => { |
| 14 | + switch (packageName) { |
| 15 | + case 'components': |
| 16 | + return ['components', 'design-tokens']; |
| 17 | + case 'theming-core': |
| 18 | + return ['theming-build', 'theming-runtime']; |
| 19 | + default: |
| 20 | + return [packageName]; |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +const getArtifactPath = moduleName => { |
| 25 | + switch (moduleName) { |
| 26 | + case 'components': |
| 27 | + return '/lib/components/*'; |
| 28 | + case 'design-tokens': |
| 29 | + return '/lib/design-tokens/*'; |
| 30 | + case 'board-components': |
| 31 | + return '/lib/components/*'; |
| 32 | + case 'theming-build': |
| 33 | + return '/lib/node/*'; |
| 34 | + case 'theming-runtime': |
| 35 | + return '/lib/browser/*'; |
| 36 | + default: |
| 37 | + return '/lib/*'; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const args = process.argv.slice(2); |
| 42 | +if (args.length < 1) { |
| 43 | + console.error('Usage: install-peer-dependency.js <package-name>:<target-branch>'); |
| 44 | + process.exit(1); |
| 45 | +} |
| 46 | +const [packageName, targetBranch] = args[0].split(':'); |
| 47 | +const targetRepository = `https://github.com/cloudscape-design/${packageName}.git`; |
| 48 | +const nodeModulesPath = path.join(process.cwd(), 'node_modules', '@cloudscape-design'); |
| 49 | +const tempDir = path.join(os.tmpdir(), `temp-${packageName}`); |
| 50 | + |
| 51 | +// Clone the repository and checkout the branch |
| 52 | +console.log(`Cloning ${packageName}:${targetBranch}...`); |
| 53 | +execCommand(`git clone ${targetRepository} ${tempDir}`); |
| 54 | +process.chdir(tempDir); |
| 55 | +execCommand(`git checkout ${targetBranch}`); |
| 56 | + |
| 57 | +// Install dependencies and build |
| 58 | +console.log(`Installing dependencies and building ${packageName}...`); |
| 59 | +execCommand('npm install'); |
| 60 | +execCommand('npm run build'); |
| 61 | + |
| 62 | +// Remove existing peer dependency in node_modules |
| 63 | +for (const moduleName of getModules(packageName)) { |
| 64 | + const modulePath = path.join(nodeModulesPath, moduleName); |
| 65 | + const artifactPath = getArtifactPath(moduleName); |
| 66 | + |
| 67 | + console.log(`Removing existing ${moduleName} from node_modules...`, modulePath); |
| 68 | + execCommand(`rm -rf ${modulePath}`); |
| 69 | + |
| 70 | + // Copy built peer dependency to node_modules |
| 71 | + console.log(`Copying built ${moduleName} to node_modules...`, modulePath, `${tempDir}${artifactPath}`); |
| 72 | + execCommand(`mkdir -p ${modulePath}`); |
| 73 | + execCommand(`cp -R ${tempDir}${artifactPath} ${modulePath}`); |
| 74 | +} |
| 75 | + |
| 76 | +// Clean up |
| 77 | +console.log('Cleaning up...'); |
| 78 | +execCommand(`rm -rf ${tempDir}`); |
| 79 | + |
| 80 | +console.log(`${packageName} has been successfully installed from branch ${targetBranch}!`); |
| 81 | + |
| 82 | +function execCommand(command, options = {}) { |
| 83 | + try { |
| 84 | + execSync(command, { stdio: 'inherit', ...options }); |
| 85 | + } catch (error) { |
| 86 | + console.error(`Error executing command: ${command}`); |
| 87 | + console.error(`Error message: ${error.message}`); |
| 88 | + console.error(`Stdout: ${error.stdout && error.stdout.toString()}`); |
| 89 | + console.error(`Stderr: ${error.stderr && error.stderr.toString()}`); |
| 90 | + throw error; |
| 91 | + } |
| 92 | +} |
0 commit comments