Files
trezor-suite/scripts/publish/replace-imports.sh
karliatto 1448cf568b fix(scripts): replace-imports to handle .d.ts files
The current implementation of replace-impots.sh that was modified
previously to the current stable connect version was not updating
imports in .d.ts files that was causing to have in connect in npm
imports like:

    import { Capability } from '@trezor/protobuf/src/messages';

But the released `@trezor/protobuf` package does not publish `src/` but
only `lib/` so it has to be handle also when publishing.
2025-12-19 10:35:11 +01:00

51 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euxo pipefail
# Usage:
# bash replace-imports.sh <directory> [module-type]
#
# Arguments:
# <directory> The path to the directory containing files to modify.
# <module-type> (Optional) The module system to use: "cjs" | "esm"
#
# Example:
# To replace imports in the ./lib directory using the CJS module type:
# bash replace-imports.sh ./lib cjs
#
# To replace imports in the ./libESM directory using the ESM module type:
# bash replace-imports.sh ./libESM esm
if [ "$#" -ne 2 ]; then
echo "Error, needs 2 arguments. Usage: $0 <directory> <module-type>"
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ "$2" == "esm" ]; then
BABEL_CONFIG="$SCRIPT_DIR/babel.config.esm.json"
LIB_DIR="libESM"
else
BABEL_CONFIG="$SCRIPT_DIR/babel.config.cjs.json"
LIB_DIR="lib"
fi
# Transform .js files using Babel
yarn run -T babel "$1" --out-dir "$1" --extensions ".js" --config-file "$BABEL_CONFIG"
# Determine the operating system
OS="$(uname)"
# Transform .d.ts files using sed
# It should be possible to solve this using babel but babel needs @babel/preset-typescript to parse .d.ts files
# and that preset there is the risk of stripping type declarations, which would break .d.ts files.
# Using sed is faster and it just works.
# Execute the appropriate command based on the OS
if [[ "$OS" == "Darwin" ]]; then
# macOS command with -i '' for in-place editing without backup and -E for extended regex
find "$1" -name "*.d.ts" -type f -exec sed -i '' "s|@trezor/\([^/]*\)/src|@trezor/\1/${LIB_DIR}|g" {} +
else
# Linux command with -i and -E for in-place editing without backup (GNU sed syntax) and extended regex
find "$1" -name "*.d.ts" -type f -exec sed -i "s|@trezor/\([^/]*\)/src|@trezor/\1/${LIB_DIR}|g" {} +
fi