Skip to content

Commit bf08c6c

Browse files
committed
refactor params and move couple utils from pullarticles to pathustils
1 parent beebef5 commit bf08c6c

File tree

3 files changed

+66
-62
lines changed

3 files changed

+66
-62
lines changed

src/generate.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,16 @@ interface SiteGenerationConfig {
6969
* Generate static site with configurable options
7070
* @param config Site generation configuration
7171
*/
72-
async function generateSite(config: SiteGenerationConfig = {}): void {
73-
const {
74-
articlesPath = process.env.ARTICLES,
75-
publicPath = process.env.PUBLIC,
76-
source = process.env.SOURCE
77-
} = config
78-
72+
async function generateSite(): void {
73+
const articlesPath = process.env.ARTICLES
74+
const publicPath = process.env.PUBLIC
75+
const source = process.env.SOURCE
7976
const indexes: Indexes = []
8077
const pages: PageEntry[] = []
8178

8279
try {
8380
if (source !== 'local') {
84-
await pullArticles(articlesPath)
81+
await pullArticles()
8582
}
8683

8784
const processConfig: ArticleProcessingConfig = {
@@ -97,7 +94,7 @@ async function generateSite(config: SiteGenerationConfig = {}): void {
9794
await generateSitemap(publicPath, pages)
9895

9996
if (source !== 'local') {
100-
await cleanupArticlesDirectory(articlesPath)
97+
await cleanupArticlesDirectory()
10198
}
10299

103100
console.log('Static site generation completed successfully.')

src/pathutils.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {join} from 'path'
1+
import {join, extname, dirname, resolve} from 'path'
22

33
import type {SupportedLanguage, Index} from './types'
44

@@ -68,3 +68,41 @@ export function isLanguageDirectory(path: string): boolean {
6868
const candidate = parts[parts.length - 1]
6969
return validLanguages.includes(candidate as SupportedLanguage)
7070
}
71+
72+
/**
73+
* Check if a file is an image based on its extension
74+
* @param filename Filename to check
75+
* @returns Boolean indicating if file is an image
76+
*/
77+
export function isImage(filename: string): boolean {
78+
const IMAGE_EXTENSIONS: string[] = [
79+
'.png',
80+
'.jpg',
81+
'.jpeg',
82+
'.gif',
83+
'.svg',
84+
'.webp'
85+
]
86+
const ext = extname(filename).toLowerCase()
87+
return IMAGE_EXTENSIONS.includes(ext)
88+
}
89+
90+
/**
91+
* Get the directory name of a path
92+
* @param filePath Path to extract directory from
93+
* @returns Directory part of the path
94+
*/
95+
export function getDirname(filePath: string): string {
96+
return dirname(filePath)
97+
}
98+
99+
/**
100+
* Resolve a path from current working directory
101+
* @param paths Path segments to resolve
102+
* @returns Resolved absolute path
103+
*/
104+
export function resolvePath(...paths: string[]): string {
105+
return resolve(...paths)
106+
}
107+
108+
export { join }

src/pullarticles.ts

+21-52
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
import fs from 'fs/promises'
2-
import path from 'path'
32

43
import {Octokit} from 'octokit'
54
import {Buffer} from 'buffer/'
65

7-
const IGNORE_LIST: string[] = ['README.md', '.git', '.gitignore']
8-
const IMAGE_EXTENSIONS: string[] = [
9-
'.png',
10-
'.jpg',
11-
'.jpeg',
12-
'.gif',
13-
'.svg',
14-
'.webp'
15-
]
6+
import {join, isImage, getDirname, resolvePath} from './pathutils'
167

17-
/**
18-
* Parse GitHub repository details from URL
19-
* @param sourceUrl GitHub repository URL
20-
* @returns Object with owner and repo
21-
*/
22-
function parseRepoDetails(sourceUrl: string): {owner: string; repo: string} {
23-
const repoUrl = new URL(sourceUrl)
24-
const [owner, repo] = repoUrl.pathname.split('/').filter(Boolean)
25-
26-
if (!owner || !repo) {
27-
throw new Error('Invalid repository URL')
28-
}
29-
30-
return {owner, repo}
31-
}
8+
const IGNORE_LIST: string[] = ['README.md', '.git', '.gitignore', 'LICENSE']
329

3310
/**
3411
* Create Octokit instance based on authentication
@@ -42,17 +19,23 @@ function createOctokitClient(githubToken?: string): Octokit {
4219
}
4320

4421
/**
45-
* Check if a file is an image based on its extension
46-
* @param filename Filename to check
47-
* @returns Boolean indicating if file is an image
22+
* Parse GitHub repository details from URL
23+
* @param sourceUrl GitHub repository URL
24+
* @returns Object with owner and repo
4825
*/
49-
function isImage(filename: string): boolean {
50-
const ext = path.extname(filename).toLowerCase()
51-
return IMAGE_EXTENSIONS.includes(ext)
26+
function parseRepoDetails(sourceUrl: string): {owner: string; repo: string} {
27+
const repoUrl = new URL(sourceUrl)
28+
const [owner, repo] = repoUrl.pathname.split('/').filter(Boolean)
29+
30+
if (!owner || !repo) {
31+
throw new Error('Invalid repository URL')
32+
}
33+
34+
return {owner, repo}
5235
}
5336

5437
/**
55-
* Download both markdowns or images
38+
* Download both markdowns and images
5639
* @param octokit Octokit client
5740
* @param owner Repository owner
5841
* @param repo Repository name
@@ -76,8 +59,8 @@ async function downloadFile(
7659
// Decode file content (GitHub API returns base64 encoded content)
7760
const fileContent = Buffer.from(data.content, 'base64')
7861

79-
const localFilePath = path.join(articlesDir, item.path)
80-
await fs.mkdir(path.dirname(localFilePath), {recursive: true})
62+
const localFilePath = join(articlesDir, item.path)
63+
await fs.mkdir(getDirname(localFilePath), {recursive: true})
8164

8265
if (item.name.endsWith('.md')) {
8366
// For markdown files, convert to utf-8 string before writing
@@ -141,29 +124,15 @@ async function processRepoContents(
141124

142125
/**
143126
* Pull articles and images from a GitHub repository
144-
* @param articlePath Destination path for articles
145-
* @param config Configuration options
146127
*/
147-
async function pullArticles(
148-
articlePath: string,
149-
config?: {
150-
githubToken?: string
151-
sourceUrl?: string
152-
}
153-
): Promise<void> {
128+
async function pullArticles(): Promise<void> {
154129
try {
155-
const githubToken = config?.githubToken || process.env.GITHUB_TOKEN
156-
const sourceUrl = config?.sourceUrl || process.env.SOURCE
130+
const octokit = createOctokitClient(process.env.GITHUB_TOKEN)
131+
const {owner, repo} = parseRepoDetails(process.env.SOURCE)
132+
const articlesDir = resolvePath(process.cwd(), process.env.ARTICLE)
157133

158-
const octokit = createOctokitClient(githubToken)
159-
160-
const {owner, repo} = parseRepoDetails(sourceUrl)
161-
162-
const articlesDir = path.resolve(process.cwd(), articlePath)
163134
await fs.mkdir(articlesDir, {recursive: true})
164-
165135
await processRepoContents(octokit, owner, repo, '', articlesDir)
166-
167136
console.log('Article and image pull completed successfully')
168137
} catch (error) {
169138
console.error('Error pulling articles and images:', error)

0 commit comments

Comments
 (0)