From 037116cdc0cf8fb20293394666fc2832f32e112b Mon Sep 17 00:00:00 2001 From: Keshav Mohta <68001229+keshavmohta09@users.noreply.github.com> Date: Tue, 25 Feb 2025 18:18:02 +0530 Subject: [PATCH] Feature: Generate TS From JSON (#19823) --- generate_ts.sh | 59 +++++++++++++++++++ ...erate-typescript-types-from-json-schema.md | 22 ++++++- 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100755 generate_ts.sh diff --git a/generate_ts.sh b/generate_ts.sh new file mode 100755 index 00000000000..ff607822f46 --- /dev/null +++ b/generate_ts.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# Copyright 2022 Collate. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Define color codes +BRIGHT_RED='\033[1;31m' +BRIGHT_GREEN='\033[1;32m' +BRIGHT_CYAN='\033[1;36m' +NC='\033[0m' # No Color + +# Get the script's base directory (assuming it's inside OpenMetadata repo) +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OPENMETADATA_BASE="$(dirname "$SCRIPT_DIR")" # Adjust if script is placed deeper +UI_RESOURCES_PATH="$OPENMETADATA_BASE/OpenMetadata/openmetadata-ui/src/main/resources/ui" + +# Check if a file path is provided +if [[ -z "$1" ]]; then + echo -e "${BRIGHT_RED}Error: No file path provided. Please pass the file path as an argument.${NC}" + exit 1 +fi + +FILE_PATH="$1" + +echo -e "${BRIGHT_CYAN}Detected OpenMetadata base path: $OPENMETADATA_BASE${NC}" +echo -e "${BRIGHT_CYAN}Changing directory to OpenMetadata UI resources...${NC}" + +# Change to the UI resources directory +cd "$UI_RESOURCES_PATH" || { + echo -e "${BRIGHT_RED}Failed to change directory to OpenMetadata UI resources.${NC}"; + exit 1 +} + +echo -e "${BRIGHT_CYAN}Running 'sh json2ts.sh $FILE_PATH'...${NC}" + +# Run the json2ts.sh script with the given file path +sh json2ts.sh "$FILE_PATH" +if [[ $? -eq 0 ]]; then + echo -e "${BRIGHT_GREEN}'json2ts.sh' executed successfully.${NC}" +else + echo -e "${BRIGHT_RED}Error during 'json2ts.sh' execution.${NC}" + exit 1 +fi + +# Revert back to the original directory +cd - > /dev/null || { + echo -e "${BRIGHT_RED}Failed to revert to the original directory.${NC}"; + exit 1 +} + +echo -e "${BRIGHT_CYAN}Reverted to the original directory.${NC}" diff --git a/openmetadata-docs/content/v1.7.x-SNAPSHOT/developers/contribute/build-code-and-run-tests/generate-typescript-types-from-json-schema.md b/openmetadata-docs/content/v1.7.x-SNAPSHOT/developers/contribute/build-code-and-run-tests/generate-typescript-types-from-json-schema.md index dfa81943c7e..63fe00ac0a4 100644 --- a/openmetadata-docs/content/v1.7.x-SNAPSHOT/developers/contribute/build-code-and-run-tests/generate-typescript-types-from-json-schema.md +++ b/openmetadata-docs/content/v1.7.x-SNAPSHOT/developers/contribute/build-code-and-run-tests/generate-typescript-types-from-json-schema.md @@ -22,7 +22,8 @@ Navigate to the `openmetadata-ui` directory and install dependencies: cd openmetadata-ui/src/main/resources/ui yarn install ``` -### Step 2: Stage Files + +### Step 2: Option A - Generate Types for Staged Files Return to the root folder, add the relevant files to the staging area, and execute the following command: @@ -32,12 +33,29 @@ changed_files=$(git diff --cached --name-only --diff-filter=ACM | grep 'openmeta This command identifies all staged files located in the `openmetadata-spec/src/main/resources/json/schema/` path and stores the file paths in the changed_files variable. +### Step 2: Option B - Generate Types for Specific JSON File + +From the OpenMetadata root folder, you can generate types for a specific JSON schema file using: + +```bash +./generate_ts.sh +``` + +Examples: +```bash +./generate_ts.sh openmetadata-spec/src/main/resources/json/schema/entity/services/ingestionPipelines/ingestionPipeline.json +``` + +Note: The path should be relative to the OpenMetadata directory. + ### Step 3: Generate TypeScript Types -Run the following script to generate TypeScript types for the identified JSON schema files: +If using Option A, run the following script to generate TypeScript types for the identified JSON schema files: ```bash ./openmetadata-ui/src/main/resources/ui/json2ts.sh $changed_files ``` This script processes the staged JSON schema files and generates the corresponding TypeScript types. + +The generated TypeScript types will be available in the appropriate location within the UI project structure.