|
| 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 | +import { execSync } from "child_process"; |
| 10 | +import process from "node:process"; |
| 11 | +import os from "os"; |
| 12 | +import path from "path"; |
| 13 | + |
| 14 | +const getModules = (packageName) => { |
| 15 | + switch (packageName) { |
| 16 | + case "components": |
| 17 | + return ["components", "design-tokens"]; |
| 18 | + default: |
| 19 | + return [packageName]; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +const getArtifactPath = (moduleName) => { |
| 24 | + switch (moduleName) { |
| 25 | + case "components": |
| 26 | + return "/lib/components/*"; |
| 27 | + case "design-tokens": |
| 28 | + return "/lib/design-tokens/*"; |
| 29 | + case '"board-components': |
| 30 | + return "/lib/components/*"; |
| 31 | + default: |
| 32 | + return "/lib/*"; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +const args = process.argv.slice(2); |
| 37 | +if (args.length < 1) { |
| 38 | + console.error("Usage: install-peer-dependency.js <package-name>:<target-branch>"); |
| 39 | + process.exit(1); |
| 40 | +} |
| 41 | +const [packageName, targetBranch] = args[0].split(":"); |
| 42 | +const targetRepository = `https://github.com/cloudscape-design/${packageName}.git`; |
| 43 | +const nodeModulesPath = path.join(process.cwd(), "node_modules", "@cloudscape-design"); |
| 44 | +const tempDir = path.join(os.tmpdir(), `temp-${packageName}`); |
| 45 | + |
| 46 | +// Clone the repository and checkout the branch |
| 47 | +console.log(`Cloning ${packageName}:${targetBranch}...`); |
| 48 | +execCommand(`git clone ${targetRepository} ${tempDir}`); |
| 49 | +process.chdir(tempDir); |
| 50 | +execCommand(`git checkout ${targetBranch}`); |
| 51 | + |
| 52 | +// Install dependencies and build |
| 53 | +console.log(`Installing dependencies and building ${packageName}...`); |
| 54 | +execCommand("npm install"); |
| 55 | +execCommand("npm run build"); |
| 56 | + |
| 57 | +// Remove existing peer dependency in node_modules |
| 58 | +for (const moduleName of getModules(packageName)) { |
| 59 | + const modulePath = path.join(nodeModulesPath, moduleName); |
| 60 | + const artifactPath = getArtifactPath(moduleName); |
| 61 | + |
| 62 | + console.log(`Removing existing ${moduleName} from node_modules...`, modulePath); |
| 63 | + execCommand(`rm -rf ${modulePath}`); |
| 64 | + |
| 65 | + // Copy built peer dependency to node_modules |
| 66 | + console.log(`Copying built ${moduleName} to node_modules...`, modulePath, `${tempDir}${artifactPath}`); |
| 67 | + execCommand(`mkdir -p ${modulePath}`); |
| 68 | + execCommand(`cp -R ${tempDir}${artifactPath} ${modulePath}`); |
| 69 | +} |
| 70 | + |
| 71 | +// Clean up |
| 72 | +console.log("Cleaning up..."); |
| 73 | +execCommand(`rm -rf ${tempDir}`); |
| 74 | + |
| 75 | +console.log(`${packageName} has been successfully installed from branch ${targetBranch}!`); |
| 76 | + |
| 77 | +function execCommand(command, options = {}) { |
| 78 | + try { |
| 79 | + execSync(command, { stdio: "inherit", ...options }); |
| 80 | + } catch (error) { |
| 81 | + console.error(`Error executing command: ${command}`); |
| 82 | + console.error(`Error message: ${error.message}`); |
| 83 | + console.error(`Stdout: ${error.stdout && error.stdout.toString()}`); |
| 84 | + console.error(`Stderr: ${error.stderr && error.stderr.toString()}`); |
| 85 | + throw error; |
| 86 | + } |
| 87 | +} |
0 commit comments