Skip to content

Commit

Permalink
chore: reclassify source directories according to module purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
chengpeiquan committed Oct 23, 2024
1 parent c3e4f42 commit bf78cc0
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 55 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"type": "module",
"private": true,
"scripts": {
"dev": "pnpm build:meta-cache && next dev -p 4832",
"dev": "pnpm build:cache && next dev -p 4832",
"build": "run-s build:*",
"build:meta-cache": "tsx ./scripts/generate-meta-cache.ts",
"build:cache": "tsx ./scripts/generate-cache.ts",
"build:app": "next build",
"build:rss": "tsx ./scripts/generate-rss.ts",
"build:copy-assets": "tsx ./scripts/copy-assets.ts",
Expand Down Expand Up @@ -78,4 +78,4 @@
"eslint --fix"
]
}
}
}
16 changes: 14 additions & 2 deletions scripts/generate-meta-cache.ts → scripts/generate-cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { join } from 'node:path'
import { writeFile } from 'node:fs/promises'
import { fs } from '@bassist/node-utils'
import { getMetaCachePath } from '@/contents/io'
import { type ContentFolder } from '@/config/content-config'
import { type Locale, locales } from '@/config/locale-config'
import { writeFile } from 'node:fs/promises'
import { getPosts } from './shared'
import { cacheRootFolder, metaCacheRootFolder } from '@/config/cache-config'

const cacheRootPath = join(process.cwd(), 'src', cacheRootFolder)

const metaCacheRootPath = join(cacheRootPath, metaCacheRootFolder)

const getMetaCachePath = async (folder: ContentFolder, locale: Locale) => {
return {
rootPath: metaCacheRootPath,
filePath: join(metaCacheRootPath, `${folder}-${locale}.json`),
}
}

const listFolders: Exclude<ContentFolder, 'about'>[] = ['article', 'cookbook']

Expand Down
2 changes: 1 addition & 1 deletion scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import { type ContentFolder } from '@/config/content-config'
import { type Locale } from '@/config/locale-config'
import { getContents } from '@/contents/io'
import { getContents } from '@/engines/io'

const cwd = process.cwd()

Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/about/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type DetailsPageProps,
getDetails,
getDetailsMetadata,
} from '@/contents'
} from '@/engines/dispatcher'
import { MarkupRenderer } from '@/components/markup/renderer'

const folder: ContentFolder = 'about'
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/article/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type DetailsPageProps,
getDetails,
getDetailsMetadata,
} from '@/contents'
} from '@/engines/dispatcher'
import { MarkupRenderer } from '@/components/markup/renderer'
import { DesktopToc, MobileToc } from '@/components/markup/table-of-contents'
import { PublishedBooks } from '@/components/sidebar/published-books'
Expand Down
6 changes: 5 additions & 1 deletion src/app/[locale]/articles/[...args]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
articleCategories,
} from '@/config/content-config'
import { isMobileDevice } from '@/config/middleware-config'
import { type ListPageProps, getList, getListMetadata } from '@/contents'
import {
type ListPageProps,
getList,
getListMetadata,
} from '@/engines/dispatcher'
import { Empty } from '@/components/layouts/empty'
import { Pagination } from '@/components/layouts/pagination'
import { CategoryLinks } from '@/components/layouts/category-links'
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/cookbook/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type DetailsPageProps,
getDetails,
getDetailsMetadata,
} from '@/contents'
} from '@/engines/dispatcher'
import { MarkupRenderer } from '@/components/markup/renderer'
import { DesktopToc, MobileToc } from '@/components/markup/table-of-contents'
import { FriendlyLinks } from '@/components/sidebar/friendly-links'
Expand Down
6 changes: 5 additions & 1 deletion src/app/[locale]/cookbooks/[...args]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
cookbookCategories,
} from '@/config/content-config'
import { isMobileDevice } from '@/config/middleware-config'
import { type ListPageProps, getList, getListMetadata } from '@/contents'
import {
type ListPageProps,
getList,
getListMetadata,
} from '@/engines/dispatcher'
import { Empty } from '@/components/layouts/empty'
import { Pagination } from '@/components/layouts/pagination'
import { CategoryLinks } from '@/components/layouts/category-links'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions src/cache/meta-cache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use server'

import { type ListFolder } from '@/config/content-config'
import { type Locale } from '@/config/locale-config'
import { type MetaCacheItem, type MetaCacheMapKey } from '@/config/cache-config'
import article_en from './article-en.json'
import article_zh from './article-zh.json'
import cookbook_zh from './cookbook-zh.json'

const metaCacheMap = new Map<MetaCacheMapKey, MetaCacheItem[]>([
['article_en', article_en as MetaCacheItem[]],
['article_zh', article_zh as MetaCacheItem[]],
['cookbook_zh', cookbook_zh as MetaCacheItem[]],
])

export const getMetaCache = async (
folder: ListFolder,
locale: Locale,
): Promise<MetaCacheItem[]> => {
const cache = metaCacheMap.get(`${folder}_${locale}`)
return cache || []
}
2 changes: 1 addition & 1 deletion src/components/shared/time-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface TimeDisplayProps {
*
* @description
* The `date` return value from `parseDate` method,
* in `@/contents/index.ts`
* in `@/engines/parser/index.ts`
*
*
*/
Expand Down
10 changes: 10 additions & 0 deletions src/config/cache-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type ContentItem, type ListFolder } from './content-config'
import { type Locale } from './locale-config'

export const cacheRootFolder = 'cache'

export const metaCacheRootFolder = 'meta-cache'

export type MetaCacheMapKey = `${ListFolder}_${Locale}`

export type MetaCacheItem = Pick<ContentItem, 'slug' | 'metadata'>
6 changes: 2 additions & 4 deletions src/config/content-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ export type ContentMetadata = z.infer<typeof contentMetadataSchema>

export type ContentItem = z.infer<typeof contentItemSchema>

export type MetaCacheItem = Pick<ContentItem, 'slug' | 'metadata'>

export const isValidContentItem = (v: unknown): v is ContentItem => !!v

export type ListFolder = Exclude<ContentFolder, 'about'>

export interface GetListResponse<T extends ContentItem | MetaCacheItem> {
export interface GetListResponse<T> {
items: T[]
page: number
pageSize: number
Expand All @@ -96,7 +94,7 @@ export interface GetListResponse<T extends ContentItem | MetaCacheItem> {
isEmpty: boolean
}

export const getPagination = <T extends ContentItem | MetaCacheItem>(
export const getPagination = <T>(
contents: T[],
page: number,
pageSize: number,
Expand Down
13 changes: 0 additions & 13 deletions src/contents/meta-cache/index.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/contents/index.ts → src/engines/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import { cache } from 'react'
import { type Metadata } from 'next'
import { getTranslations } from 'next-intl/server'
import { type LocalePageParams } from '@/config/locale-config'
import { sharedMetadata } from '@/config/site-config'
import {
type ContentFolder,
type GetListResponse,
type ListFolder,
type MetaCacheItem,
articleCategories,
cookbookCategories,
getPagination,
pageSizeConfig,
} from '@/config/content-config'
import { getContent, getMetaCache } from './io'
import { getTranslations } from 'next-intl/server'
import { type MetaCacheItem } from '@/config/cache-config'
import { getMetaCache } from '@/cache/meta-cache'
import { getContent } from './io'

interface ListPageParams extends LocalePageParams {
args: string[] // [page] | [slug, page]
Expand Down
23 changes: 0 additions & 23 deletions src/contents/io.ts → src/engines/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
type ContentFolder,
type ContentItem,
type GetListResponse,
type ListFolder,
type MetaCacheItem,
contentFolders,
contentItemSchema,
contentRootFolder,
Expand All @@ -18,30 +16,9 @@ import {
isValidContentItem,
} from '@/config/content-config'
import { type ParseOptions, parse } from './parser'
import { metaCacheMap } from './meta-cache'

const contentRootPath = join(process.cwd(), 'src', contentRootFolder)

const metaCacheRootPath = join(contentRootPath, 'meta-cache')

export const getMetaCachePath = async (
folder: ContentFolder,
locale: Locale,
) => {
return {
rootPath: metaCacheRootPath,
filePath: join(metaCacheRootPath, `${folder}-${locale}.json`),
}
}

export const getMetaCache = async (
folder: ListFolder,
locale: Locale,
): Promise<MetaCacheItem[]> => {
const cache = metaCacheMap.get(`${folder}_${locale}`)
return cache || []
}

const getMarkdownFiles = (dir: string) => {
try {
return readdirSync(dir)
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit bf78cc0

Please sign in to comment.