Skip to content

Commit f1d1841

Browse files
committed
UpdateDb: Commmand line arguments for path prefix and keeping the build number
1 parent 1e823e7 commit f1d1841

6 files changed

Lines changed: 80 additions & 53 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@mdx-js/mdx": "^3.1.0",
1717
"@mdx-js/react": "^3.1.0",
1818
"antd": "^5.26.3",
19+
"commander": "^14.0.0",
1920
"gray-matter": "^4.0.3",
2021
"markdown-it": "^12.3.2",
2122
"next": "15.3.2",

scripts/createDb.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function rebuildDatabase() {
1515
conn.exec(`
1616
CREATE TABLE builds (
1717
id INTEGER PRIMARY KEY AUTOINCREMENT,
18-
trigger TEXT NOT NULL,
18+
path TEXT NOT NULL,
1919
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2020
output TEXT NOT NULL DEFAULT ''
2121
);

scripts/updateDb.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,45 @@ import { getMdFile } from "../utils/helpers";
44
import { getPaths } from "../utils/getPaths";
55
import { open } from "sqlite";
66
import {updatePaths} from "@/utils/updatePaths";
7+
import { program } from "commander";
78

89
const DB_PATH = path.join(process.cwd(), "db");
910
const DB_FILE = path.join(DB_PATH, "notes.sqlite");
1011

11-
async function updateDb(trigger: string) {
12+
async function updateDb(pathPrefix: string, update: false) {
1213
const db = await open({
1314
filename: path.join(DB_FILE),
1415
driver: sqlite3.Database,
1516
});
16-
const { id: buildId } = await db.get(`INSERT INTO builds (trigger)
17-
VALUES (?)
18-
RETURNING id;`, [trigger]);
1917

20-
const paths: [string[], boolean][] = getPaths([]).map((path) => [path, !!getMdFile(path)]);
18+
let buildId, prefix;
19+
if (update) {
20+
buildId = (await db.get(`SELECT MAX(id) as buildId FROM builds;`)).buildId;
21+
prefix = (await db.get(`SELECT path FROM builds WHERE id = ?;`, [buildId.buildId])).path;
22+
}
23+
else {
24+
buildId = (await db.get(`INSERT INTO builds (path) VALUES (?) RETURNING id;`, [pathPrefix])).id;
25+
prefix = pathPrefix;
26+
}
27+
28+
const paths: [string[], boolean][] = getPaths(prefix ? prefix.split("/") : []).map((path) => [path, !!getMdFile(path)]);
2129
const bookPaths = paths.filter(([, isBook]) => isBook).map(([path]) => path);
2230
const collectionPaths = paths.filter(([, isBook]) => !isBook).map(([path]) => path);
23-
await updatePaths(bookPaths, collectionPaths, db, buildId);
31+
await updatePaths(bookPaths, collectionPaths, db, buildId, prefix);
2432
}
2533

26-
const trigger = process.argv[2] || "manual";
34+
program
35+
.option("-p, --path <path>", "Path to the directory to update", "")
36+
.option("-u, --update", "Update without increasing the build number", false)
37+
38+
program.parseOptions(process.argv.slice(2));
39+
const { path: pathPrefix, update } = program.opts();
40+
if (pathPrefix && update) {
41+
console.error("Both --path and --update options are provided. Please provide only one.");
42+
process.exit(1);
43+
}
2744

28-
updateDb(trigger).catch((err) => {
29-
console.error("Error marking a new build:", err);
45+
updateDb(pathPrefix, update).catch((err) => {
46+
console.error("Error making a new build:", err);
3047
process.exit(1);
3148
});

utils/getCollectionProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getMdFile,
77
isDirectory,
88
parseMd,
9-
readPublicDir, serializedContent,
9+
readPublicDir,
1010
} from "./helpers";
1111
import {
1212
BookFrontmatter,

utils/updatePaths.ts

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const extractQuizzes = async (
4141
slug: string
4242
): Promise<QuestionDef[]> => {
4343
const compiledMdx = await compile(
44-
// At some point I useed mdxSource.replace(/[^\x00-\x7F]/g, "") to fix some problem.
44+
// At some point I used mdxSource.replace(/[^\x00-\x7F]/g, "") to fix some problem.
4545
// Later it turned out it makes options non-unique (e.g. in `options={["Č", "Š", "Ž"]}`).
4646
// I removed it and it still works. I'm keeping the comment, just for the case.
4747
mdxSource,
@@ -234,7 +234,8 @@ export const extractQuizzes = async (
234234
const checkBooks = async (
235235
books: RawBookProps[],
236236
allBookSlugs: Set<string>,
237-
db: Database
237+
db: Database,
238+
updatePath: string
238239
): Promise<
239240
Record<string, { frontmatter: ChapterFrontmatter; questions: QuestionDef[] }>
240241
> => {
@@ -250,7 +251,9 @@ const checkBooks = async (
250251
FROM books
251252
JOIN books_chapters ON books.id = books_chapters.bookId
252253
JOIN chapters ON books_chapters.chapterId = chapters.id
253-
JOIN questions ON chapters.id = questions.chapterId`
254+
JOIN questions ON chapters.id = questions.chapterId
255+
WHERE books.path LIKE ?`,
256+
[`${updatePath}/%`]
254257
);
255258
booksWithQuestions
256259
.filter(({ path }) => !allBookSlugs.has(path))
@@ -412,7 +415,7 @@ const checkCollections = async (
412415
logError(
413416
collection.slug,
414417
`The following books are missing in the collection:\n ${missingBooks
415-
.map((b) => ` ${b}`)
418+
.map((b) => ` ${b.slug}`)
416419
.join("\n")}`
417420
);
418421
}
@@ -425,7 +428,7 @@ const checkCollections = async (
425428
logError(
426429
collection.slug,
427430
`The following collections are missing in the collection:\n ${missingCollections
428-
.map((c) => ` ${c}`)
431+
.map((c) => ` ${c.slug}`)
429432
.join("\n")}`
430433
);
431434
}
@@ -546,54 +549,54 @@ const insertCollections = async (
546549
for (const {
547550
slug: collectionSlug,
548551
frontmatter: { title, subTitle, date, description, public: isPublic, language, coverImg, recursiveContent },
549-
books,
550-
collections: subCollections,
551552
} of collections) {
552553
// Do not change this to "DELETE + INSERT" because it will delete rows that use this collections's id as foreign key.
553-
const { id: collectionId } = await db.get(
554+
await db.get(
554555
`
555-
INSERT INTO collections (
556-
lastBuildId,
557-
path, title, subtitle, description, date,
558-
public, language, coverImg, recursiveContent)
559-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
560-
ON CONFLICT(path) DO UPDATE SET title = excluded.title,
561-
lastBuildId = excluded.lastBuildId,
562-
subtitle = excluded.subtitle,
563-
description = excluded.description,
564-
date = excluded.date,
565-
public = excluded.public,
566-
language = excluded.language,
567-
coverImg = excluded.coverImg,
568-
recursiveContent = excluded.recursiveContent
569-
RETURNING id
570-
`,
556+
INSERT INTO collections (lastBuildId,
557+
path, title, subtitle, description, date,
558+
public, language, coverImg, recursiveContent)
559+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
560+
ON CONFLICT(path) DO UPDATE SET title = excluded.title,
561+
lastBuildId = excluded.lastBuildId,
562+
subtitle = excluded.subtitle,
563+
description = excluded.description,
564+
date = excluded.date,
565+
public = excluded.public,
566+
language = excluded.language,
567+
coverImg = excluded.coverImg,
568+
recursiveContent = excluded.recursiveContent
569+
RETURNING id
570+
`,
571571
[buildId, collectionSlug, title, subTitle, description, date, isPublic, language, coverImg, recursiveContent]
572572
);
573+
}
573574

574-
for (const { slug: bookSlug } of books) {
575+
for (const { slug: collectionSlug, collections: subCollections, books } of collections) {
576+
const collectionId = (await db.get(`SELECT id FROM collections WHERE path = ?`, [collectionSlug])).id;
577+
for (const { slug } of books) {
575578
await db.run(
576579
`
577-
INSERT INTO collections_books (collectionId, bookId, lastBuildId)
578-
SELECT ?, id, ?
579-
FROM books
580-
WHERE path = ?
581-
ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
582-
`,
583-
[collectionId, buildId, bookSlug]
580+
INSERT INTO collections_books (collectionId, bookId, lastBuildId)
581+
SELECT ?, id, ?
582+
FROM books
583+
WHERE path = ?
584+
ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
585+
`,
586+
[collectionId, buildId, slug]
584587
);
585588
}
586589

587-
for (const { slug: subCollectionSlug } of subCollections) {
590+
for (const { slug } of subCollections) {
588591
await db.run(
589592
`
590-
INSERT INTO collections_collections (collectionId, subCollectionId, lastBuildId)
591-
SELECT ?, id, ?
592-
FROM collections
593-
WHERE path = ?
594-
ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
595-
`,
596-
[collectionId, buildId, subCollectionSlug]
593+
INSERT INTO collections_collections (collectionId, subCollectionId, lastBuildId)
594+
SELECT ?, id, ?
595+
FROM collections
596+
WHERE path = ?
597+
ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
598+
`,
599+
[collectionId, buildId, slug]
597600
);
598601
}
599602
}
@@ -603,7 +606,8 @@ export const updatePaths = async (
603606
bookSlugs: string[][],
604607
collectionSlugs: string[][],
605608
db: Database,
606-
buildId: number
609+
buildId: number,
610+
pathPrefix: string
607611
) => {
608612
const rawBooks = await Promise.all(bookSlugs.map(getRawBook));
609613
const serializedBooks = await Promise.all(bookSlugs.map(getBookProps));
@@ -613,7 +617,7 @@ export const updatePaths = async (
613617
);
614618
const allCollectionSlugs = new Set(collections.map(({ slug }) => slug));
615619

616-
const chapters = await checkBooks(rawBooks, allBookSlugs, db);
620+
const chapters = await checkBooks(rawBooks, allBookSlugs, db, pathPrefix);
617621
await checkQuestions(chapters, db);
618622
await checkCollections(collections, allCollectionSlugs, allBookSlugs);
619623
if (hasError()) {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,11 @@ comma-separated-tokens@^2.0.0:
18021802
resolved "https://registry.npmjs.org/comma-separated-tokens/-/comma-separated-tokens-2.0.3.tgz"
18031803
integrity sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==
18041804

1805+
commander@^14.0.0:
1806+
version "14.0.0"
1807+
resolved "https://registry.yarnpkg.com/commander/-/commander-14.0.0.tgz#f244fc74a92343514e56229f16ef5c5e22ced5e9"
1808+
integrity sha512-2uM9rYjPvyq39NwLRqaiLtWHyDC1FvryJDa2ATTVims5YAS4PupsEQsDvP14FqhFr0P49CYDugi59xaxJlTXRA==
1809+
18051810
commander@^8.3.0:
18061811
version "8.3.0"
18071812
resolved "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz"

0 commit comments

Comments
 (0)