-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckDocs.ts
More file actions
168 lines (153 loc) · 5.23 KB
/
checkDocs.ts
File metadata and controls
168 lines (153 loc) · 5.23 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
mainブランチにpushされた際にGitHub Actionが --write 引数をつけて実行し、
その場合は public/docs/ 以下のドキュメントの各セクションについて、
現在のパス、md5ハッシュ、コミットIDなどを ./public/docs/revisions.yml に追記
セクションIDとページパスの対応関係をデータベースに反映
過去に存在したページが削除されている場合、エラーになります。
その場合は手動でrevisions.ymlを編集し、古いページ名の記述を新しいページ名に置き換える必要がある
(できれば自動化したいが、いい方法が思いつかない)
Dockerfile内で --check-diff 引数をつけて実行され、
revisions.ymlが最新の状態でないならexit(1)をし、dockerのビルドを停止します
なにも引数をつけずに実行した場合(npm run checkDocs)、
変更があってもなにもせず正常終了します
エラーなく全ドキュメントを取得できるかどうかの確認に使います
*/
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import {
getMarkdownSections,
getPagesList,
RevisionYmlEntry,
} from "@/lib/docs";
import yaml from "js-yaml";
import { execFileSync } from "node:child_process";
import { existsSync } from "node:fs";
import { getDrizzle } from "@/lib/drizzle";
import { section as sectionTable } from "@/schema/chat";
import "dotenv/config";
import { sql } from "drizzle-orm";
let doWrite = false;
let doCheckDiff = false;
if (process.argv[2] === "--write") {
doWrite = true;
} else if (process.argv[2] === "--check-diff") {
doCheckDiff = true;
} else if (process.argv[2]) {
throw new Error(`Unknown arg: ${process.argv[2]}`);
}
const docsDir = join(process.cwd(), "public", "docs");
let commit = "";
if (doWrite) {
commit = execFileSync("git", ["rev-parse", "--short", "HEAD"], {
encoding: "utf8",
}).trim();
}
let hasNewRevision = false;
const langEntries = await getPagesList();
const revisionsPrevYml = existsSync(join(docsDir, "revisions.yml"))
? await readFile(join(docsDir, "revisions.yml"), "utf-8")
: "{}";
const revisions = yaml.load(revisionsPrevYml) as Record<
string,
RevisionYmlEntry
>;
for (const lang of langEntries) {
for (const page of lang.pages) {
const sections = await getMarkdownSections(lang.id, page.slug);
for (const section of sections) {
if (section.id in revisions) {
revisions[section.id].page = `${lang.id}/${page.slug}`;
if (!revisions[section.id].rev.some((r) => r.md5 === section.md5)) {
// ドキュメントが変更された場合
console.warn(`${section.id} has new md5: ${section.md5}`);
if (doWrite) {
hasNewRevision = true;
revisions[section.id].rev.push({
md5: section.md5,
git: commit,
path: `public/docs/${lang.id}/${page.slug}/${section.file}`,
});
} else if (doCheckDiff) {
process.exit(1);
}
}
} else {
// ドキュメントが新規追加された場合
console.warn(`${section.id} is new section`);
if (doWrite) {
hasNewRevision = true;
revisions[section.id] = {
rev: [
{
md5: section.md5,
git: commit,
path: `public/docs/${lang.id}/${page.slug}/${section.file}`,
},
],
page: `${lang.id}/${page.slug}`,
};
} else if (doCheckDiff) {
process.exit(1);
}
}
}
}
}
for (const id in revisions) {
if (!existsSync(join(docsDir, revisions[id].page))) {
console.warn(
`The page slug ${revisions[id].page} previously used by section ${id} does not exist. ` +
`Please replace 'page: ${revisions[id].page}' in public/docs/revisions.yml with new page path manually.`
);
if (doWrite || doCheckDiff) {
process.exit(1);
}
}
}
if (doWrite) {
const drizzle = await getDrizzle();
const batchSize = 100;
const entries = Object.entries(revisions);
for (let i = 0; i < entries.length; i += batchSize) {
await drizzle
.insert(sectionTable)
.values(
entries.slice(i, i + batchSize).map(([id, data]) => ({
sectionId: id,
pagePath: data.page,
}))
)
.onConflictDoUpdate({
target: sectionTable.sectionId,
set: { pagePath: sql`excluded."pagePath"` },
});
}
const revisionsYml = yaml.dump(revisions, {
sortKeys: true,
noArrayIndent: true,
});
await writeFile(
join(docsDir, "revisions.yml"),
"# This file will be updated by CI. Do not edit manually, unless CI failed.\n" +
revisionsYml,
"utf-8"
);
if (hasNewRevision) {
const commitsYml = yaml.load(
await readFile(join(docsDir, "commits.yml"), "utf-8")
) as Record<string, string>;
commitsYml[commit] = execFileSync(
"git",
["show", "-s", "--format=%cI", commit],
{
encoding: "utf8",
}
).trim();
await writeFile(
join(docsDir, "commits.yml"),
"# This file will be updated by CI. Do not edit manually.\n" +
yaml.dump(commitsYml, { sortKeys: true }),
"utf-8"
);
}
}