FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeWords/scripts/deploy-oss.js at master · zyronon/TypeWords · GitHub
zyronon
/
TypeWords
Public
Notifications
You must be signed in to change notification settings
Fork
1.2k
Star
10.3k
Code
Issues
44
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeWords
/
scripts
/
deploy-oss.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
141 lines (124 loc) · 4.01 KB
Breadcrumbs
TypeWords
/
scripts
/
deploy-oss.js
Copy path
File metadata and controls
141 lines (124 loc) · 4.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import
OSS
from
'ali-oss'
import
fs
from
'fs'
import
path
from
'path'
import
Core
from
'@alicloud/pop-core'
const
{
OSS_REGION
,
OSS_KEY_ID
,
OSS_KEY_SECRET
,
OSS_BUCKET
,
}
=
process
.
env
if
(
!
OSS_REGION
||
!
OSS_KEY_ID
||
!
OSS_KEY_SECRET
||
!
OSS_BUCKET
)
{
console
.
error
(
'❌ 缺少必要的环境变量,请检查 GitHub Secrets 配置'
)
process
.
exit
(
1
)
}
const
client
=
new
OSS
(
{
region
:
OSS_REGION
,
accessKeyId
:
OSS_KEY_ID
,
accessKeySecret
:
OSS_KEY_SECRET
,
bucket
:
OSS_BUCKET
}
)
const
cdnClient
=
new
Core
(
{
accessKeyId
:
OSS_KEY_ID
,
accessKeySecret
:
OSS_KEY_SECRET
,
endpoint
:
'https://cdn.aliyuncs.com'
,
apiVersion
:
'2018-05-10'
}
)
// 遍历 dist 目录,统计文件
function
getAllFiles
(
dir
,
fileList
=
[
]
)
{
const
files
=
fs
.
readdirSync
(
dir
)
for
(
const
file
of
files
)
{
const
filePath
=
path
.
join
(
dir
,
file
)
const
stat
=
fs
.
statSync
(
filePath
)
if
(
stat
.
isDirectory
(
)
)
{
getAllFiles
(
filePath
,
fileList
)
}
else
{
fileList
.
push
(
filePath
)
}
}
return
fileList
}
// 上传文件,显示进度,可跳过指定目录
/**
* 上传文件并清理远端多余文件
*
@param
files 本地文件完整路径列表
*
@param
localBase 本地基准路径
*
@param
ignoreDirs 相对 localBase 的目录名数组,上传时跳过,删除远端时保留
*/
async
function
uploadFilesWithClean
(
files
,
localBase
=
'./dist'
,
ignoreDirs
=
[
]
)
{
// 1️⃣ 过滤掉忽略的目录
const
filteredFiles
=
files
.
filter
(
file
=>
{
const
relativePath
=
path
.
relative
(
localBase
,
file
)
const
topDir
=
relativePath
.
split
(
path
.
sep
)
[
0
]
return
!
ignoreDirs
.
includes
(
topDir
)
}
)
// 2️⃣ 获取远端已有文件列表
console
.
log
(
'📄 获取远端文件列表...'
)
let
remoteFiles
=
[
]
let
marker
=
''
do
{
const
result
=
await
client
.
list
(
{
prefix
:
''
,
'max-keys'
:
1000
,
marker
,
}
)
if
(
result
.
objects
)
{
remoteFiles
.
push
(
...
result
.
objects
.
map
(
f
=>
f
.
name
)
)
}
marker
=
result
.
nextMarker
||
''
}
while
(
marker
)
// 3️⃣ 上传文件
const
total
=
filteredFiles
.
length
let
count
=
0
const
uploadedFiles
=
[
]
for
(
const
file
of
filteredFiles
)
{
const
relativePath
=
path
.
relative
(
localBase
,
file
)
const
remotePath
=
relativePath
.
split
(
path
.
sep
)
.
join
(
'/'
)
// POSIX 路径
await
client
.
put
(
remotePath
,
file
)
uploadedFiles
.
push
(
remotePath
)
count
++
const
percent
=
(
(
count
/
total
)
*
100
)
.
toFixed
(
1
)
process
.
stdout
.
write
(
`\r📤 上传进度:
${
count
}
/
${
total
}
(
${
percent
}
%)
${
remotePath
}
`
)
}
console
.
log
(
'\n✅ 文件上传完成'
)
// 4️⃣ 删除远端多余文件(远端存在但本地未上传),同时保留 ignoreDirs
const
toDelete
=
remoteFiles
.
filter
(
f
=>
{
const
topDir
=
f
.
split
(
'/'
)
[
0
]
return
!
uploadedFiles
.
includes
(
f
)
&&
!
ignoreDirs
.
includes
(
topDir
)
}
)
if
(
toDelete
.
length
)
{
console
.
log
(
'🗑 删除远端多余文件:'
,
toDelete
)
// 分批删除,防止数量过多
const
batchSize
=
1000
for
(
let
i
=
0
;
i
<
toDelete
.
length
;
i
+=
batchSize
)
{
const
batch
=
toDelete
.
slice
(
i
,
i
+
batchSize
)
await
client
.
deleteMulti
(
batch
)
}
console
.
log
(
'✅ 多余文件删除完成'
)
}
else
{
console
.
log
(
'ℹ️ 无需删除远端文件'
)
}
}
// 刷新 CDN
async
function
refreshCDN
(
domain
)
{
console
.
log
(
`🔄 刷新
${
domain
}
CDN 缓存...`
)
const
params
=
{
ObjectPath
:
`https://
${
domain
}
/`
,
ObjectType
:
'Directory'
}
const
requestOption
=
{
method
:
'POST'
}
const
result
=
await
cdnClient
.
request
(
'RefreshObjectCaches'
,
params
,
requestOption
)
console
.
log
(
'✅ CDN 刷新完成:'
,
result
)
}
async
function
main
(
)
{
const
files
=
getAllFiles
(
'./dist'
)
console
.
log
(
`📁 共找到
${
files
.
length
}
个文件,开始上传...`
)
await
uploadFilesWithClean
(
files
,
'./dist'
,
[
'dicts'
,
'sound'
,
'libs'
,
'imgs'
]
)
// await uploadFilesWithClean(files, './dist', ['sound','libs','imgs'])
await
refreshCDN
(
'2study.top'
)
await
refreshCDN
(
'typewords.cc'
)
}
main
(
)
.
catch
(
err
=>
{
console
.
error
(
'❌ 部署失败:'
,
err
)
process
.
exit
(
1
)
}
)
Back
|
FazBrowse Home
|
New Git URL