|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +PAGES_DIR="src/pages" |
| 4 | +INDEX_FILE="$PAGES_DIR/index.ts" |
| 5 | + |
| 6 | +page_name=$1 |
| 7 | +page_name_lowercase=$(echo "$page_name" | tr '[:upper:]' '[:lower:]') |
| 8 | +capitalized_page_name=$(echo "$page_name" | awk '{print toupper(substr($0, 1, 1)) tolower(substr($0, 2))}') |
| 9 | +page_dir="$PAGES_DIR/$page_name_lowercase" |
| 10 | + |
| 11 | +# Function to check if a directory exists |
| 12 | +function directory_exists() { |
| 13 | + [ -d $1 ] |
| 14 | +} |
| 15 | + |
| 16 | +# Function to create a directory if it doesn't exist |
| 17 | +function create_directory() { |
| 18 | + if ! directory_exists $1; then |
| 19 | + mkdir -p $1 |
| 20 | + fi |
| 21 | +} |
| 22 | + |
| 23 | +# Function to create the index.ts file |
| 24 | +function create_index_file() { |
| 25 | + echo "export * from \"./$capitalized_page_name\";" > "$2/index.ts" |
| 26 | +} |
| 27 | + |
| 28 | +# Function to create the CSS file |
| 29 | +function create_css_file() { |
| 30 | + echo ".${capitalized_page_name}Page {" > "$2/$capitalized_page_name.css" |
| 31 | + echo " /* Rules here. */" >> "$2/$capitalized_page_name.css" |
| 32 | + echo "}" >> "$2/$capitalized_page_name.css" |
| 33 | +} |
| 34 | + |
| 35 | +# Function to create the stories.tsx file |
| 36 | +function create_stories_file() { |
| 37 | + cat > "$2/$capitalized_page_name.stories.tsx" <<EOF |
| 38 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 39 | +
|
| 40 | +import { ${capitalized_page_name}Page } from "./$capitalized_page_name"; |
| 41 | +
|
| 42 | +const meta: Meta<typeof ${capitalized_page_name}Page> = { |
| 43 | + title: "Pages/${capitalized_page_name}", |
| 44 | + component: ${capitalized_page_name}Page, |
| 45 | +}; |
| 46 | +
|
| 47 | +export default meta; |
| 48 | +type Story = StoryObj<typeof meta>; |
| 49 | +
|
| 50 | +export const ${page_name_lowercase}Page: Story = { |
| 51 | + args: { |
| 52 | + children: "The quick brown fox jumps over the lazy dog.", |
| 53 | + }, |
| 54 | +}; |
| 55 | +EOF |
| 56 | +} |
| 57 | + |
| 58 | +# Function to create the page file |
| 59 | +function create_page_file() { |
| 60 | + cat > "$2/$capitalized_page_name.tsx" <<EOF |
| 61 | +import React from "react"; |
| 62 | +
|
| 63 | +import "./$capitalized_page_name.css"; |
| 64 | +
|
| 65 | +export type ${capitalized_page_name}Props = React.ComponentProps<"main"> & { |
| 66 | + // Props here. |
| 67 | +}; |
| 68 | +
|
| 69 | +/** |
| 70 | + * ${capitalized_page_name} page |
| 71 | + */ |
| 72 | +export function ${capitalized_page_name}Page({ children, ...props }: ${capitalized_page_name}Props) { |
| 73 | + return ( |
| 74 | + <main className="${capitalized_page_name}Page" {...props}> |
| 75 | + {children} |
| 76 | + </main> |
| 77 | + ); |
| 78 | +} |
| 79 | +EOF |
| 80 | +} |
| 81 | + |
| 82 | +# Function to update the index.ts file in pages |
| 83 | +function update_index_file() { |
| 84 | + echo "// Auto-generated file. Do not modify manually." > "$INDEX_FILE" |
| 85 | + for page_dir in "$PAGES_DIR"/*/; do |
| 86 | + page_name=$(basename "$page_dir") |
| 87 | + echo "export * from \"./$page_name\";" >> "$INDEX_FILE" |
| 88 | + done |
| 89 | +} |
| 90 | + |
| 91 | +# Main script |
| 92 | + |
| 93 | +# Check if $PAGES_DIR directory exists, if not create it |
| 94 | +create_directory $PAGES_DIR |
| 95 | + |
| 96 | +# Check if a page name is provided |
| 97 | +if [ -z "$1" ]; then |
| 98 | + echo "Error: Please provide a page name." |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +# Check if page already exists |
| 103 | +if directory_exists $page_dir; then |
| 104 | + echo "Error: page '$capitalized_page_name' already exists." |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +# Create page directory |
| 109 | +create_directory $page_dir |
| 110 | + |
| 111 | +# Create individual files |
| 112 | +create_index_file $capitalized_page_name $page_dir |
| 113 | +create_css_file $capitalized_page_name $page_dir |
| 114 | +create_stories_file $capitalized_page_name $page_dir |
| 115 | +create_page_file $capitalized_page_name $page_dir |
| 116 | + |
| 117 | +# Update pages/index.ts file |
| 118 | +update_index_file |
| 119 | + |
| 120 | +echo "page '$capitalized_page_name' created successfully." |
0 commit comments