Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 当 BLOG_POST_DIRECTORY 为空或当前目录时,将会扫描系统根目录中的文件 #18

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __tests__/api/datasource/FileSystemDatasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import path from 'node:path'
import datasource from '@/api/datasource'

test('Test archive list', async () => {
const files = glob.globSync(path.join(process.env.BLOG_PATH, process.env.BLOG_HOME_POST_DIRECTORY, '/**/*.{md,mdx}'))
const files = glob.globSync('./**/*.{md,mdx}', { cwd: path.resolve(process.env.BLOG_PATH, process.env.BLOG_HOME_POST_DIRECTORY) })

expect(files.length).toBeGreaterThan(0)

expect((await datasource.getAllHomePosts()).map(v => v.visitPath))
.toStrictEqual(files.map(v => datasource.resolvePostWebPath(v)))
Expand Down
2 changes: 1 addition & 1 deletion scripts/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path"
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

const root = path.resolve('__test__/__blog__')
const root = path.resolve('__tests__/__blog__')

process.env.BLOG_PATH = root
process.env.BLOG_HOME_POST_DIRECTORY = '_post'
Expand Down
5 changes: 4 additions & 1 deletion src/api/datasource/FileSystemDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default class FileSystemDatasource implements Datasource {
private readonly homePaths: string[]

public constructor() {
if (!fs.existsSync(process.env.BLOG_PATH) || !fs.statSync(process.env.BLOG_PATH).isDirectory()) {
throw new Error(`The path '${process.env.BLOG_PATH}' must be a directory. Please check you BLOG_PATH configuration.`)
}
const config = {
homePostDirectory: process.env.BLOG_HOME_POST_DIRECTORY,
resourceDirectory: process.env.BLOG_RESOURCE_DIRECTORY,
Expand All @@ -51,7 +54,7 @@ export default class FileSystemDatasource implements Datasource {
let searchGlobs: string[] = []
pageRelativePath = Array.isArray(pageRelativePath) ? pageRelativePath : [pageRelativePath]

const append = recursion ? '/**/*.{md,mdx}' : '/*.{md,mdx}'
const append = recursion ? './**/*.{md,mdx}' : './*.{md,mdx}'
for (let root of pageRelativePath) {
searchGlobs.push(path.join(root, append).replaceAll('\\', '/'))
}
Expand Down
Loading