forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_db_generator.js
More file actions
229 lines (202 loc) · 6.13 KB
/
example_db_generator.js
File metadata and controls
229 lines (202 loc) · 6.13 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
const { globSync, readdirSync, readFileSync, mkdirSync, existsSync, rmSync } = require('node:fs');
const { resolve, dirname, join } = require('node:path');
const { DatabaseSync } = require('node:sqlite');
const { z } = require('zod');
/**
* A simple YAML front matter parser.
*
* This function extracts the YAML block enclosed by `---` at the beginning of a string
* and parses it into a JavaScript object. It is not a full YAML parser and only
* supports simple key-value pairs and string arrays.
*
* @param content The string content to parse.
* @returns A record containing the parsed front matter data.
*/
function parseFrontmatter(content) {
const match = content.match(/^---\r?\n(.*?)\r?\n---/s);
if (!match) {
return {};
}
const frontmatter = match[1];
const data = {};
const lines = frontmatter.split(/\r?\n/);
let currentKey = '';
let isArray = false;
const arrayValues = [];
for (const line of lines) {
const keyValueMatch = line.match(/^([^:]+):\s*(.*)/);
if (keyValueMatch) {
if (currentKey && isArray) {
data[currentKey] = arrayValues.slice();
arrayValues.length = 0;
}
const [, key, value] = keyValueMatch;
currentKey = key.trim();
isArray = value.trim() === '';
if (!isArray) {
const trimmedValue = value.trim();
if (trimmedValue === 'true') {
data[currentKey] = true;
} else if (trimmedValue === 'false') {
data[currentKey] = false;
} else {
data[currentKey] = trimmedValue;
}
}
} else {
const arrayItemMatch = line.match(/^\s*-\s*(.*)/);
if (arrayItemMatch && currentKey && isArray) {
let value = arrayItemMatch[1].trim();
// Unquote if the value is quoted.
if (
(value.startsWith("'") && value.endsWith("'")) ||
(value.startsWith('"') && value.endsWith('"'))
) {
value = value.slice(1, -1);
}
arrayValues.push(value);
}
}
}
if (currentKey && isArray) {
data[currentKey] = arrayValues;
}
return data;
}
function generate(inPath, outPath) {
const dbPath = outPath;
mkdirSync(dirname(outPath), { recursive: true });
if (existsSync(dbPath)) {
rmSync(dbPath);
}
const db = new DatabaseSync(dbPath);
// Create a table to store metadata.
db.exec(`
CREATE TABLE metadata (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL
);
`);
db.exec(`
INSERT INTO metadata (key, value) VALUES
('schema_version', '1'),
('created_at', '${new Date().toISOString()}');
`);
// Create a relational table to store the structured example data.
db.exec(`
CREATE TABLE examples (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
summary TEXT NOT NULL,
keywords TEXT,
required_packages TEXT,
related_concepts TEXT,
related_tools TEXT,
experimental INTEGER NOT NULL DEFAULT 0,
content TEXT NOT NULL
);
`);
// Create an FTS5 virtual table to provide full-text search capabilities.
db.exec(`
CREATE VIRTUAL TABLE examples_fts USING fts5(
title,
summary,
keywords,
required_packages,
related_concepts,
related_tools,
content,
content='examples',
content_rowid='id',
tokenize = 'porter ascii'
);
`);
// Create triggers to keep the FTS table synchronized with the examples table.
db.exec(`
CREATE TRIGGER examples_after_insert AFTER INSERT ON examples BEGIN
INSERT INTO examples_fts(
rowid, title, summary, keywords, required_packages, related_concepts, related_tools,
content
)
VALUES (
new.id, new.title, new.summary, new.keywords, new.required_packages,
new.related_concepts, new.related_tools, new.content
);
END;
`);
const insertStatement = db.prepare(
'INSERT INTO examples(' +
'title, summary, keywords, required_packages, related_concepts, related_tools, experimental, content' +
') VALUES(?, ?, ?, ?, ?, ?, ?, ?);',
);
const frontmatterSchema = z.object({
title: z.string(),
summary: z.string(),
keywords: z.array(z.string()).optional(),
required_packages: z.array(z.string()).optional(),
related_concepts: z.array(z.string()).optional(),
related_tools: z.array(z.string()).optional(),
experimental: z.boolean().optional(),
});
db.exec('BEGIN TRANSACTION');
const entries = globSync
? globSync('**/*.md', { cwd: resolve(inPath), withFileTypes: true })
: readdirSync(resolve(inPath), { withFileTypes: true });
for (const entry of entries) {
if (!entry.isFile() || !entry.name.endsWith('.md')) {
continue;
}
const content = readFileSync(join(entry.parentPath, entry.name), 'utf-8');
const frontmatter = parseFrontmatter(content);
const validation = frontmatterSchema.safeParse(frontmatter);
if (!validation.success) {
console.error(`Validation failed for example file: ${entry.name}`);
console.error('Issues:', validation.error.issues);
throw new Error(`Invalid front matter in ${entry.name}`);
}
const {
title,
summary,
keywords,
required_packages,
related_concepts,
related_tools,
experimental,
} = validation.data;
insertStatement.run(
title,
summary,
JSON.stringify(keywords ?? []),
JSON.stringify(required_packages ?? []),
JSON.stringify(related_concepts ?? []),
JSON.stringify(related_tools ?? []),
experimental ? 1 : 0,
content,
);
}
db.exec('END TRANSACTION');
db.close();
}
if (require.main === module) {
const argv = process.argv.slice(2);
if (argv.length !== 2) {
console.error('Must include 2 arguments.');
process.exit(1);
}
const [inPath, outPath] = argv;
try {
generate(inPath, outPath);
} catch (error) {
console.error('An error happened:');
console.error(error);
process.exit(127);
}
}
exports.generate = generate;