移除 Angular 無用的 *.spec.ts 檔案

Angular Library 目前是使用 karma + Jasmine 運行單元測試。在使用 ng g c 指令時,預設會新增測試檔案,如果團隊沒有單元測試的習慣,時間久了專案資料夾內會堆積許多冗餘的檔案,因此我希望能透寫個於腳本來幫我快速人工檢查在專案內的測試檔案,哪些是可以移除的?哪些又是可以保留的。

使用 ChatGPT 快速幫我產生想要的內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

# 指定目錄路徑
directory_path="projects/"

# 指定要檢查的代碼片段
code_to_check="expect(service).toBeTruthy()"

# 使用 find 獲取目錄下的所有文件
files=$(find "$directory_path" -type f -name "*.spec.ts")

# 遍曆每個文件並使用 grep 檢查內容
for file in $files; do
echo "$file"
if grep "$code_to_check" "$file"; then
echo "文件 $file 包含指定的代碼片段。"

# 顯示文件內容
echo "文件內容:"
cat "$file"

# 詢問用戶是否刪除文件
read -p "是否刪除文件?(y/n): " response
if [ "$response" == "y" ]; then
rm "$file"
echo "文件已刪除。"
fi
fi
done

大致上看過 shell 語法沒問題,我們就可以直接運行代碼,整理專案中的檔案內容。