Skip to content

Commit 7df7eb7

Browse files
committed
fix: action
1 parent 6f4c459 commit 7df7eb7

3 files changed

Lines changed: 29 additions & 32 deletions

File tree

.github/workflows/station-check.yml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,19 @@ jobs:
2020
token: ${{ secrets.GITHUB_TOKEN }}
2121
fetch-depth: 0
2222
persist-credentials: true
23+
sparse-checkout: |
24+
resource/station-dev.csv
25+
resource/station.csv
26+
scripts/station-check.ts
27+
sparse-checkout-cone-mode: false
2328

2429
- name: Setup Bun
2530
uses: oven-sh/setup-bun@v2
2631
with:
2732
bun-version: latest
2833

29-
- name: Cache Bun dependencies
30-
uses: actions/cache@v4
31-
with:
32-
path: |
33-
~/.bun/install/cache
34-
node_modules
35-
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}-${{ hashFiles('**/package.json') }}
36-
restore-keys: |
37-
${{ runner.os }}-bun-${{ hashFiles('**/package.json') }}
38-
${{ runner.os }}-bun-
39-
40-
- name: Install dependencies
41-
run: bun install
42-
4334
- name: Run station check
44-
run: bun scripts/station-check.ts
35+
run: bun run scripts/station-check.ts
4536

4637
- name: Copy station-dev.csv to station.csv
4738
if: success()

resource/station-dev.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
loc_code,id,lat,lon,floor,code,net,time,work
2-
1,126E0A8,23,121,1,711,3,2025-12-12 10:00:00,1
2+
1,126E0A8,23,121,1,711,3,2025-12-12,1

scripts/station-check.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function parseCSV(filePath: string): StationRecord[] {
5050
const values = parseCSVLine(lines[i]);
5151

5252
if (values.length !== headers.length) {
53-
throw new Error(`第 ${i + 1} 行欄位數量不匹配: 期望 ${headers.length} 個欄位,實際 ${values.length} 個欄位`);
53+
throw new Error(`第 ${i + 1} 行欄位數量不匹配: 期望 ${headers.length} 個欄位,實際 ${values.length} 個欄位。原始行: ${lines[i]}`);
5454
}
5555

5656
const record: any = {};
@@ -93,10 +93,10 @@ function parseCSV(filePath: string): StationRecord[] {
9393
}
9494

9595
function validateDate(dateStr: string): { valid: boolean; error?: string } {
96-
// 解析時間格式 YYYY-MM-DD 或 YYYY-MM-DD HH:mm:ss
97-
const dateMatch = dateStr.match(/^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2}):(\d{2}))?$/);
96+
// 解析時間格式必須是 YYYY-MM-DD
97+
const dateMatch = dateStr.match(/^(\d{4})-(\d{2})-(\d{2})$/);
9898
if (!dateMatch) {
99-
return { valid: false, error: `時間格式錯誤: ${dateStr}` };
99+
return { valid: false, error: `時間格式必須是 YYYY-MM-DD,當前值: ${dateStr}` };
100100
}
101101

102102
const year = parseInt(dateMatch[1], 10);
@@ -140,17 +140,18 @@ function validateRecords(records: StationRecord[]): { valid: boolean; errors: st
140140
}
141141
});
142142

143-
// 檢查 2: loc_code 不能重複
144-
const locCodeMap = new Map<string, number[]>();
143+
// 檢查 2: code + loc_code 的組合不能重複
144+
const codeLocCodeMap = new Map<string, { code: string; locCode: string; lineNumbers: number[] }>();
145145
records.forEach((record, index) => {
146-
if (!locCodeMap.has(record.loc_code)) {
147-
locCodeMap.set(record.loc_code, []);
146+
const key = `${record.code}::${record.loc_code}`;
147+
if (!codeLocCodeMap.has(key)) {
148+
codeLocCodeMap.set(key, { code: record.code, locCode: record.loc_code, lineNumbers: [] });
148149
}
149-
locCodeMap.get(record.loc_code)!.push(index + 2);
150+
codeLocCodeMap.get(key)!.lineNumbers.push(index + 2);
150151
});
151-
locCodeMap.forEach((lineNumbers, locCode) => {
152-
if (lineNumbers.length > 1) {
153-
errors.push(`loc_code 重複: ${locCode} (出現在第 ${lineNumbers.join(', ')} 行)`);
152+
codeLocCodeMap.forEach((info, key) => {
153+
if (info.lineNumbers.length > 1) {
154+
errors.push(`code + loc_code 組合重複: code=${info.code}, loc_code=${info.locCode} (出現在第 ${info.lineNumbers.join(', ')} 行)`);
154155
}
155156
});
156157

@@ -179,7 +180,7 @@ function validateRecords(records: StationRecord[]): { valid: boolean; errors: st
179180
errors.push(`第 ${lineNum} 行: lng 超出範圍 (114 < ${record.lon} < 122.2)`);
180181
}
181182

182-
// 檢查 5: 時間必須是 YYYY-MM-DD UTC+8 不能是未來時間
183+
// 檢查 5: 時間必須是 YYYY-MM-DD 格式,不能是未來時間
183184
const dateValidation = validateDate(record.time);
184185
if (!dateValidation.valid) {
185186
errors.push(`第 ${lineNum} 行: ${dateValidation.error}`);
@@ -190,9 +191,14 @@ function validateRecords(records: StationRecord[]): { valid: boolean; errors: st
190191
errors.push(`第 ${lineNum} 行: floor 必須是正整數,當前值: ${record.floor}`);
191192
}
192193

193-
// 檢查 8: code 1 || 2 || 3 三選一
194-
if (record.code !== '1' && record.code !== '2' && record.code !== '3') {
195-
errors.push(`第 ${lineNum} 行: code 必須是 1、2 或 3,當前值: ${record.code}`);
194+
// 檢查 8: net 1 || 2 || 3 三選一
195+
if (record.net !== '1' && record.net !== '2' && record.net !== '3') {
196+
errors.push(`第 ${lineNum} 行: net 必須是 1、2 或 3,當前值: ${record.net}`);
197+
}
198+
199+
// 檢查 9: work 必須是 0 或 1
200+
if (record.work !== 0 && record.work !== 1) {
201+
errors.push(`第 ${lineNum} 行: work 必須是 0 或 1,當前值: ${record.work}`);
196202
}
197203
});
198204

0 commit comments

Comments
 (0)