Skip to content

Commit 7c8f2ed

Browse files
committed
Convert contents config to TS
1 parent a0f80f1 commit 7c8f2ed

9 files changed

Lines changed: 65 additions & 62 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ static/assets
2020
static/collections
2121
static/contents
2222
src/lib/data/catalog.js
23-
src/lib/data/contents.js
2423
src/gen-assets
2524
vite.config.js.timestamp*

config/index.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,50 @@ export type Quiz = {
383383
}[];
384384
passScore?: number; //\pm
385385
};
386+
387+
export type LangContainer = { [lang: string]: string };
388+
389+
export type LinkMeta = {
390+
// intended to pass between functions so that there is one object passed
391+
linkType?: string;
392+
linkTarget?: string;
393+
linkLocation?: string;
394+
};
395+
396+
export type ContentItem = {
397+
id: number;
398+
heading?: boolean;
399+
features?: any;
400+
title: LangContainer;
401+
subtitle?: LangContainer;
402+
audioFilename?: LangContainer;
403+
imageFilename?: string;
404+
itemType?: string;
405+
contentItemContainer: boolean;
406+
linkType?: string;
407+
linkTarget?: string;
408+
linkLocation?: string;
409+
layoutMode?: string;
410+
layoutCollection?: string[];
411+
children?: ContentItem[];
412+
};
413+
414+
export type ContentScreen = {
415+
id: number;
416+
title?: {
417+
[lang: string]: string;
418+
};
419+
items?: {
420+
id: number;
421+
}[];
422+
};
423+
424+
export type ContentsData = {
425+
title?: {
426+
[lang: string]: string;
427+
};
428+
features?: any;
429+
items?: ContentItem[];
430+
nestedItems?: boolean;
431+
screens?: ContentScreen[];
432+
};

convert/convertContents.ts

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,11 @@
11
import { existsSync, readFileSync } from 'fs';
22
import path from 'path';
3-
import { ScriptureConfig } from '$config';
3+
import { ContentItem, ContentsData, LangContainer, LinkMeta, ScriptureConfig } from '$config';
44
import jsdom from 'jsdom';
55
import { ConfigTaskOutput, parseLangAttribute } from './convertConfig';
66
import { createHashedFile, createOutputDir, deleteOutputDir, joinUrlPath } from './fileUtils';
77
import { Task, TaskOutput } from './Task';
88

9-
export type LangContainer = { [lang: string]: string };
10-
11-
export type LinkMeta = {
12-
// intended to pass between functions so that there is one object passed
13-
linkType?: string;
14-
linkTarget?: string;
15-
linkLocation?: string;
16-
};
17-
18-
type ContentItem = {
19-
id: number;
20-
heading?: boolean;
21-
features?: any;
22-
title: LangContainer;
23-
subtitle?: LangContainer;
24-
audioFilename?: LangContainer;
25-
imageFilename?: string;
26-
itemType?: string;
27-
contentItemContainer: boolean;
28-
linkType?: string;
29-
linkTarget?: string;
30-
linkLocation?: string;
31-
layoutMode?: string;
32-
layoutCollection?: string[];
33-
children?: ContentItem[];
34-
};
35-
36-
type ContentScreen = {
37-
id: number;
38-
title?: {
39-
[lang: string]: string;
40-
};
41-
items?: {
42-
id: number;
43-
}[];
44-
};
45-
46-
export type ContentsData = {
47-
title?: {
48-
[lang: string]: string;
49-
};
50-
features?: any;
51-
items?: ContentItem[];
52-
nestedItems?: boolean;
53-
screens?: ContentScreen[];
54-
};
55-
56-
const data: ContentsData = {};
57-
589
export interface ContentsTaskOutput extends TaskOutput {
5910
taskName: 'ConvertContents';
6011
}
@@ -367,6 +318,8 @@ export function convertContents(
367318
deleteOutputDir(destDir);
368319
}
369320

321+
const data: ContentsData = {};
322+
370323
const contentsFile = path.join(dataDir, 'contents.xml');
371324
if (!existsSync(contentsFile)) {
372325
return data;
@@ -602,8 +555,12 @@ export class ConvertContents extends Task {
602555
data,
603556
files: [
604557
{
605-
path: 'src/lib/data/contents.js',
606-
content: `export default ${JSON.stringify(data, null, 2)}`
558+
path: 'src/gen-assets/contents.ts',
559+
content: [
560+
`import type { ContentsData } from '$config';`,
561+
`export const contents = ${JSON.stringify(data, null, 2)} as Readonly<ContentsData>;`,
562+
`export default contents;\n`
563+
].join('\n')
607564
}
608565
]
609566
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"convert": "ts-node convert/index.ts",
1818
"convert:watch": "ts-node convert/index.ts --watch",
1919
"sandbox": "ts-node data-sandbox/index.ts",
20-
"clean": "rimraf .svelte-kit build src/gen-assets src/lib/data/catalog.js src/lib/data/contents.js static/illustrations static/icons static/collections static/contents static/manifest*.json && echo 🔔 Reminder: The project cannot be built until the conversion scripts are run again.",
20+
"clean": "rimraf .svelte-kit build src/gen-assets src/lib/data/catalog.js static/illustrations static/icons static/collections static/contents static/manifest*.json && echo 🔔 Reminder: The project cannot be built until the conversion scripts are run again.",
2121
"clean:data": "rimraf --glob data/*",
2222
"clean:all": "npm run clean && npm run clean:data",
2323
"test": "vitest",

src/lib/components/BottomNavigationBar.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<script>
55
import { base } from '$app/paths';
66
import config from '$assets/config';
7-
import contents from '$lib/data/contents';
7+
import contents from '$assets/contents';
88
import { language, languageDefault, refs, s, theme } from '$lib/data/stores';
99
import { gotoRoute } from '$lib/navigate';
1010

src/lib/components/Sidebar.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ The sidebar/drawer.
55
<script>
66
import { resolve } from '$app/paths';
77
import config from '$assets/config';
8+
import contents from '$assets/contents';
89
import nav_drawer_image from '$assets/images/nav_drawer.png';
910
import nav_drawer_2x from '$assets/images/nav_drawer@2x.png';
10-
import contents from '$lib/data/contents';
1111
import {
1212
direction,
1313
fontChoices,

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
import { page } from '$app/stores';
33
import config from '$assets/config';
4-
import contents from '$lib/data/contents';
4+
import contents from '$assets/contents';
55
import { audioActive, isDAB, isFirstLaunch } from '$lib/data/stores';
66
import { gotoRoute, navigateToTextReference } from '$lib/navigate';
77
import { onMount } from 'svelte';

src/routes/contents/[id]/+page.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import contents from '$lib/data/contents';
1+
import contents from '$assets/contents';
22

33
export async function load({ params }) {
44
const id = Number(params.id);
5-
const menu = contents.screens.find((x) => x.id === id);
5+
const menu = contents.screens?.find((x) => x.id === id);
66
const nestedItems = contents.nestedItems;
77
const features = contents.features;
88
const title = contents.title;
99

1010
const items = [];
11-
for (const item of menu.items) {
12-
const found = contents.items.find((x) => x.id === item.id);
11+
for (const item of menu?.items ?? []) {
12+
const found = contents.items?.find((x) => x.id === item.id);
1313
if (found && Object.keys(found).length !== 0) {
1414
items.push(found);
1515
} else {

src/routes/text/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { page } from '$app/state';
33
import config from '$assets/config';
4+
import contents from '$assets/contents';
45
import AudioBar from '$lib/components/AudioBar.svelte';
56
import BookSelector from '$lib/components/BookSelector.svelte';
67
import BookTabs from '$lib/components/BookTabs.svelte';
@@ -13,7 +14,6 @@
1314
import { showTextAppearance } from '$lib/components/TextAppearanceSelector.svelte';
1415
import TextSelectionToolbar from '$lib/components/TextSelectionToolbar.svelte';
1516
import { playStop, seekToVerse, updateAudioPlayer } from '$lib/data/audio';
16-
import contents from '$lib/data/contents';
1717
import {
1818
analytics,
1919
audioActive,

0 commit comments

Comments
 (0)