|
| 1 | +import { parse, stringify } from "jsr:@std/yaml"; |
| 2 | + |
| 3 | +// YAMLファイルの`.re`ファイルをカウントする関数 |
| 4 | +function countChapters(data: unknown): number { |
| 5 | + if (Array.isArray(data)) { |
| 6 | + return data.reduce((count, item) => count + countChapters(item), 0); |
| 7 | + } else if (typeof data === "object" && data !== null) { |
| 8 | + return Object.values(data).reduce((count, value) => count + countChapters(value), 0); |
| 9 | + } else if (typeof data === "string" && data.endsWith(".re")) { |
| 10 | + return 1; |
| 11 | + } |
| 12 | + return 0; |
| 13 | +} |
| 14 | + |
| 15 | +// Slack通知を送信する関数 |
| 16 | +async function sendSlackNotification(webhookUrl: string, message: string) { |
| 17 | + const response = await fetch(webhookUrl, { |
| 18 | + method: "POST", |
| 19 | + headers: { "Content-Type": "application/json" }, |
| 20 | + body: JSON.stringify({ text: message }), |
| 21 | + }); |
| 22 | + |
| 23 | + if (!response.ok) { |
| 24 | + console.error("Failed to send Slack notification:", await response.text()); |
| 25 | + Deno.exit(1); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +async function main(statsFile: string, yamlFile: string) { |
| 30 | + const webhookUrl = Deno.env.get("SLACK_WEBHOOK_URL"); |
| 31 | + if (!webhookUrl) { |
| 32 | + console.error("SLACK_WEBHOOK_URL is not set."); |
| 33 | + Deno.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + // 前回のデータを読み込む |
| 37 | + let previousStats: { chapterCount: number } = { chapterCount: 0 }; |
| 38 | + try { |
| 39 | + const statsText = await Deno.readTextFile(statsFile); |
| 40 | + previousStats = JSON.parse(statsText); |
| 41 | + } catch { |
| 42 | + console.warn("No previous stats found, using defaults."); |
| 43 | + } |
| 44 | + |
| 45 | + // YAMLファイルから現在の`.re`ファイル数を取得 |
| 46 | + const yamlText = await Deno.readTextFile(yamlFile); |
| 47 | + const yamlData = parse(yamlText); |
| 48 | + const currentChapterCount = countChapters(yamlData); |
| 49 | + |
| 50 | + console.log(`Previous .re file count: ${previousStats.chapterCount}`); |
| 51 | + console.log(`Current .re file count: ${currentChapterCount}`); |
| 52 | + |
| 53 | + // 比較と通知 |
| 54 | + if (currentChapterCount > previousStats.chapterCount) { |
| 55 | + const diff = currentChapterCount - previousStats.chapterCount; |
| 56 | + const message = `:tada: 『ワンストップ学び』現在${currentChapterCount}章(昨日より${diff}章増えました) |
| 57 | +https://github.com/onestop-techbook/learning |
| 58 | + ` |
| 59 | + console.log("Change detected. Sending notification..."); |
| 60 | + await sendSlackNotification(webhookUrl, message); |
| 61 | + } else { |
| 62 | + console.log("No changes in .re file count."); |
| 63 | + } |
| 64 | + |
| 65 | + // 新しいデータを保存 |
| 66 | + const newStats = { ...previousStats, chapterCount: currentChapterCount }; |
| 67 | + await Deno.writeTextFile(statsFile, JSON.stringify(newStats, null, 2)); |
| 68 | + console.log("Updated stats saved."); |
| 69 | +} |
| 70 | + |
| 71 | +// スクリプトの引数からファイル名を取得 |
| 72 | +if (Deno.args.length !== 2) { |
| 73 | + console.error("Usage: deno run --allow-read --allow-env --allow-net stats.ts <statsFile> <yamlFile>"); |
| 74 | + Deno.exit(1); |
| 75 | +} |
| 76 | + |
| 77 | +const [statsFile, yamlFile] = Deno.args; |
| 78 | +await main(statsFile, yamlFile); |
0 commit comments