1
1
import fs from 'fs/promises'
2
- import path from 'path'
3
2
4
3
import { Octokit } from 'octokit'
5
4
import { Buffer } from 'buffer/'
6
5
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'
16
7
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' ]
32
9
33
10
/**
34
11
* Create Octokit instance based on authentication
@@ -42,17 +19,23 @@ function createOctokitClient(githubToken?: string): Octokit {
42
19
}
43
20
44
21
/**
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
48
25
*/
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}
52
35
}
53
36
54
37
/**
55
- * Download both markdowns or images
38
+ * Download both markdowns and images
56
39
* @param octokit Octokit client
57
40
* @param owner Repository owner
58
41
* @param repo Repository name
@@ -76,8 +59,8 @@ async function downloadFile(
76
59
// Decode file content (GitHub API returns base64 encoded content)
77
60
const fileContent = Buffer . from ( data . content , 'base64' )
78
61
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 } )
81
64
82
65
if ( item . name . endsWith ( '.md' ) ) {
83
66
// For markdown files, convert to utf-8 string before writing
@@ -141,29 +124,15 @@ async function processRepoContents(
141
124
142
125
/**
143
126
* Pull articles and images from a GitHub repository
144
- * @param articlePath Destination path for articles
145
- * @param config Configuration options
146
127
*/
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 > {
154
129
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 )
157
133
158
- const octokit = createOctokitClient ( githubToken )
159
-
160
- const { owner, repo} = parseRepoDetails ( sourceUrl )
161
-
162
- const articlesDir = path . resolve ( process . cwd ( ) , articlePath )
163
134
await fs . mkdir ( articlesDir , { recursive : true } )
164
-
165
135
await processRepoContents ( octokit , owner , repo , '' , articlesDir )
166
-
167
136
console . log ( 'Article and image pull completed successfully' )
168
137
} catch ( error ) {
169
138
console . error ( 'Error pulling articles and images:' , error )
0 commit comments