-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs.ts
More file actions
131 lines (103 loc) · 3.28 KB
/
docs.ts
File metadata and controls
131 lines (103 loc) · 3.28 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
import fs from 'node:fs/promises';
import path from 'node:path';
import { dump, load } from 'js-yaml';
import { type DocumentationEntry, type Page, verbose } from './util';
export async function readDocs(folder: string): Promise<Record<string, DocumentationEntry>> {
const docs: Record<string, DocumentationEntry> = {};
const folderPath = path.join(process.cwd(), folder);
try {
await fs.access(folderPath);
} catch {
console.log(`📄 Read from ${folder}/*.yml (0 - folder does not exist)`);
return docs;
}
const files = await fs.readdir(folderPath);
console.log(`📄 Read from ${folder}/*.yml (${files.length})`);
for (let i = 0; i < files.length; i++) {
if (files[i].endsWith('.yml')) {
if (verbose) {
console.log(` ${files[i]}`);
}
const content = await fs.readFile(path.join(folderPath, files[i]), {
encoding: 'utf8',
});
const parsed = load(content) as DocumentationEntry & { gid: string };
if (parsed.gid in docs) {
throw new Error(`Duplicate doc GID: ${parsed.gid}`);
}
docs[parsed.gid] = parsed;
} else if (verbose) {
console.log(` 🚫 ${files[i]}`);
}
}
return docs;
}
export async function moveOldDocs(folder: string, gidsInUse: Set<string>): Promise<void> {
const folderPath = path.join(process.cwd(), folder);
const unusedFolder = `${folder}-unused`;
const unusedPath = path.join(process.cwd(), unusedFolder);
try {
await fs.access(folderPath);
} catch {
return;
}
const allFiles = await fs.readdir(folderPath);
const files = allFiles.filter(
(file) => file.endsWith('.yml') && !gidsInUse.has(file.replace(/\.yml$/, ''))
);
if (files.length === 0) {
return;
}
await fs.mkdir(unusedPath, { recursive: true });
console.log(`🚚 Move to ${unusedFolder}/*.yml (${files.length})`);
for (let i = 0; i < files.length; i++) {
if (verbose) {
console.log(` ${files[i]}`);
}
await fs.rename(path.join(folderPath, files[i]), path.join(unusedPath, files[i]));
}
}
export async function writeNewDocs(
folder: string,
gids: Set<string>,
pages: Record<string, Page>
): Promise<void> {
const folderPath = path.join(process.cwd(), folder);
const pageFiles: any[] = [];
await fs.mkdir(folderPath, { recursive: true });
let existingGids: Record<string, boolean> = {};
try {
const files = await fs.readdir(folderPath);
existingGids = files.reduce((memo: Record<string, boolean>, file) => {
memo[file.replace(/\.yml$/, '')] = true;
return memo;
}, {});
} catch {}
let newCount = 0;
console.log(`📝 Write to ${folder}/*.yml (${gids.size})`);
gids.forEach((gid) => {
if (!existingGids[gid]) {
newCount++;
}
pageFiles.push({
gid,
url: pages[gid]?.url,
title: pages[gid]?.title || '',
deprecated: pages[gid]?.deprecated || undefined,
description: pages[gid]?.description || '',
examples: pages[gid]?.examples || [],
show_in_navigation:
pages[gid]?.documentation?.show_in_navigation ??
(gid.startsWith('type.') && gid.split('.').length === 2),
});
});
for (let i = 0; i < pageFiles.length; i++) {
const pageFile = pageFiles[i];
const filename = `${pageFile.gid}.yml`;
if (verbose) {
console.log(` ${filename}`);
}
await fs.writeFile(path.join(folderPath, filename), dump(pageFile, { noRefs: true }));
}
console.log(` 🆕 New (${newCount})`);
}