Last active 10 months ago

這段 Bash 腳本建立一個專案目錄,初始化基本的前端專案結構(包括 index.html 和 src/components 資料夾),並透過 npm 安裝 lit、vite 和 serve,最後輸出目錄結構(排除 node_modules)。

Revision 4bc572e4fc3b4b496040f1c3f919d8554242a717

create_project_structure.sh Raw
1#!/bin/bash
2
3# Check if the user provided a directory name as an argument
4if [ -z "$1" ]; then
5 echo "Usage: $0 <directory_name>"
6 exit 1
7fi
8
9# Set the user-provided parameter to the PROJECT_DIR variable
10PROJECT_DIR="$1"
11
12# Try to create a directory named PROJECT_DIR
13if ! mkdir "$PROJECT_DIR"; then
14 echo "Failed: Unable to create directory $PROJECT_DIR. Exiting..."
15 exit 1
16fi
17
18# Change to the newly created directory
19cd "$PROJECT_DIR"
20
21# Create an empty index.html file
22touch index.html
23
24# Create a directory named src
25mkdir src
26
27# Create a subdirectory named components inside the src directory
28mkdir src/components
29
30# Create a file named my-element.js in the components directory
31touch src/components/my-element.js
32
33# Create a file named main.js
34touch src/main.js
35
36# Initialize a new npm project
37npm init -y
38
39# Install lit as a dependency
40npm install lit --save
41
42# Install vite as a development dependency
43npm install vite --save-dev
44
45# Install serve as a development dependency
46npm install serve --save-dev
47
48# List the directory structure excluding node_modules
49tree -I "node_modules"
50