|
| 1 | +import os |
| 2 | +import json |
| 3 | + |
| 4 | +# Paths |
| 5 | +CSS_COMPONENTS_DIR = "src/css/components" |
| 6 | +CSS_UTILS_DIR = "src/css/utils" |
| 7 | +CSS_BASE_FILES = ["src/css/design-tokens.css", "src/css/base.css"] |
| 8 | +JS_COMPONENTS_DIR = "src/js/components" |
| 9 | +BUNDLE_CSS_PATH = "src/css/bundle.css" |
| 10 | +BUNDLE_JS_PATH = "src/js/bundle.js" |
| 11 | +PACKAGE_JSON_PATH = "package.json" |
| 12 | + |
| 13 | +def get_package_metadata(): |
| 14 | + """Reads and extracts metadata from package.json""" |
| 15 | + try: |
| 16 | + with open(PACKAGE_JSON_PATH, "r", encoding="utf-8") as f: |
| 17 | + package_data = json.load(f) |
| 18 | + metadata = { |
| 19 | + "name": package_data.get("name", "Unknown"), |
| 20 | + "version": package_data.get("version", "0.0.0"), |
| 21 | + "description": package_data.get("description", "No description available."), |
| 22 | + "author": package_data.get("author", "Unknown"), |
| 23 | + "license": package_data.get("license", "Unlicensed"), |
| 24 | + } |
| 25 | + return metadata |
| 26 | + except FileNotFoundError: |
| 27 | + print(f"Error: {PACKAGE_JSON_PATH} not found.") |
| 28 | + return None |
| 29 | + |
| 30 | +def add_file_to_bundle(file_path, bundle_file): |
| 31 | + """Appends the content of a file to the bundle""" |
| 32 | + if os.path.exists(file_path): |
| 33 | + with open(file_path, "r", encoding="utf-8", errors="replace") as f: |
| 34 | + bundle_file.write(f"/* --- {os.path.basename(file_path)} --- */\n") |
| 35 | + bundle_file.write(f.read()) |
| 36 | + bundle_file.write("\n\n") |
| 37 | + else: |
| 38 | + print(f"Warning: {file_path} not found and will not be included in the bundle.") |
| 39 | + |
| 40 | +def add_directory_to_bundle(directory, bundle_file, file_extension): |
| 41 | + """Appends the content of all files in a directory to the bundle""" |
| 42 | + for root, _, files in os.walk(directory): |
| 43 | + for file in sorted(files): # Sort files alphabetically for consistency |
| 44 | + if file.endswith(file_extension): |
| 45 | + file_path = os.path.join(root, file) |
| 46 | + add_file_to_bundle(file_path, bundle_file) |
| 47 | + |
| 48 | +def create_css_bundle(bundle_path, metadata): |
| 49 | + """Creates the CSS bundle with the specified order of files""" |
| 50 | + try: |
| 51 | + with open(bundle_path, "w", encoding="utf-8") as bundle_file: |
| 52 | + # Write metadata header |
| 53 | + bundle_file.write(f"/*\n") |
| 54 | + bundle_file.write(f"Project: {metadata['name']}\n") |
| 55 | + bundle_file.write(f"Version: {metadata['version']}\n") |
| 56 | + bundle_file.write(f"Description: {metadata['description']}\n") |
| 57 | + bundle_file.write(f"Author: {metadata['author']}\n") |
| 58 | + bundle_file.write(f"License: {metadata['license']}\n") |
| 59 | + bundle_file.write(f"*/\n\n") |
| 60 | + |
| 61 | + # Add base CSS files |
| 62 | + for base_file in CSS_BASE_FILES: |
| 63 | + add_file_to_bundle(base_file, bundle_file) |
| 64 | + |
| 65 | + # Add utility CSS files |
| 66 | + add_directory_to_bundle(CSS_UTILS_DIR, bundle_file, ".css") |
| 67 | + |
| 68 | + # Add component CSS files |
| 69 | + add_directory_to_bundle(CSS_COMPONENTS_DIR, bundle_file, ".css") |
| 70 | + |
| 71 | + print(f"{bundle_path} created successfully.") |
| 72 | + except Exception as e: |
| 73 | + print(f"Error creating {bundle_path}: {e}") |
| 74 | + |
| 75 | +def create_js_bundle(directory, bundle_path, metadata): |
| 76 | + """Creates a JS bundle by combining all files in the directory""" |
| 77 | + try: |
| 78 | + with open(bundle_path, "w", encoding="utf-8") as bundle_file: |
| 79 | + # Write metadata header |
| 80 | + bundle_file.write(f"/*\n") |
| 81 | + bundle_file.write(f"Project: {metadata['name']}\n") |
| 82 | + bundle_file.write(f"Version: {metadata['version']}\n") |
| 83 | + bundle_file.write(f"Description: {metadata['description']}\n") |
| 84 | + bundle_file.write(f"Author: {metadata['author']}\n") |
| 85 | + bundle_file.write(f"License: {metadata['license']}\n") |
| 86 | + bundle_file.write(f"*/\n\n") |
| 87 | + |
| 88 | + # Add content from each file in the directory |
| 89 | + add_directory_to_bundle(directory, bundle_file, ".js") |
| 90 | + |
| 91 | + print(f"{bundle_path} created successfully.") |
| 92 | + except Exception as e: |
| 93 | + print(f"Error creating {bundle_path}: {e}") |
| 94 | + |
| 95 | +def main(): |
| 96 | + # Fetch package metadata |
| 97 | + metadata = get_package_metadata() |
| 98 | + if not metadata: |
| 99 | + return |
| 100 | + |
| 101 | + # Create CSS bundle |
| 102 | + create_css_bundle(BUNDLE_CSS_PATH, metadata) |
| 103 | + |
| 104 | + # Create JS bundle |
| 105 | + create_js_bundle(JS_COMPONENTS_DIR, BUNDLE_JS_PATH, metadata) |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments