Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ZTools-plugins/scripts/detect-changes.js at main · ZToolsCenter/ZTools-plugins · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ZToolsCenter
ZTools-plugins
Repository navigation
Code
Issues
4
(4)
Pull requests
42
(42)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
ZTools-plugins
/
scripts
/
detect-changes.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
177 lines (147 loc) · 5.31 KB
main
Breadcrumbs
ZTools-plugins
/
scripts
/
detect-changes.js
Copy path
Top
File metadata and controls
Code
Blame
177 lines (147 loc) · 5.31 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
import
{
execSync
}
from
'child_process'
;
import
{
readdirSync
,
statSync
,
existsSync
,
readFileSync
,
writeFileSync
,
mkdirSync
}
from
'fs'
;
import
{
join
}
from
'path'
;
const
PLUGINS_DIR
=
'plugins'
;
// 解析命令行参数
const
args
=
process
.
argv
.
slice
(
2
)
;
const
BUILD_ALL
=
args
.
includes
(
'--all'
)
||
process
.
env
.
BUILD_ALL
===
'true'
;
const
BUILD_PLUGINS
=
parsePluginList
(
process
.
env
.
BUILD_PLUGINS
||
''
)
;
function
parsePluginList
(
value
)
{
return
[
...
new
Set
(
value
.
split
(
/
[
\s
,
]
+
/
)
.
map
(
name
=>
name
.
trim
(
)
)
.
filter
(
Boolean
)
)
]
;
}
/**
* 获取所有插件目录
*/
function
getAllPlugins
(
)
{
return
readdirSync
(
PLUGINS_DIR
)
.
filter
(
name
=>
{
const
pluginPath
=
join
(
PLUGINS_DIR
,
name
)
;
return
statSync
(
pluginPath
)
.
isDirectory
(
)
;
}
)
;
}
/**
* 校验手动指定的插件目录
*/
function
validatePluginNames
(
pluginNames
)
{
const
invalidPlugins
=
pluginNames
.
filter
(
name
=>
{
const
pluginPath
=
join
(
PLUGINS_DIR
,
name
)
;
return
!
existsSync
(
pluginPath
)
||
!
statSync
(
pluginPath
)
.
isDirectory
(
)
;
}
)
;
if
(
invalidPlugins
.
length
>
0
)
{
console
.
error
(
`指定的插件目录不存在:
${
invalidPlugins
.
join
(
', '
)
}
`
)
;
process
.
exit
(
1
)
;
}
}
/**
* 检测变动的插件
*/
function
detectChangedPlugins
(
)
{
// 手动指定插件时优先构建指定插件
if
(
BUILD_PLUGINS
.
length
>
0
)
{
validatePluginNames
(
BUILD_PLUGINS
)
;
console
.
log
(
`检测到 BUILD_PLUGINS,将构建指定插件:
${
BUILD_PLUGINS
.
join
(
', '
)
}
`
)
;
return
{
changedPlugins
:
BUILD_PLUGINS
,
deletedPlugins
:
[
]
}
;
}
// 如果指定了--all参数,构建所有插件
if
(
BUILD_ALL
)
{
console
.
log
(
'检测到 --all 参数,将构建所有插件'
)
;
return
{
changedPlugins
:
getAllPlugins
(
)
,
deletedPlugins
:
[
]
}
;
}
try
{
// 获取上一次提交的hash
const
lastCommit
=
execSync
(
'git rev-parse HEAD~1'
,
{
encoding
:
'utf-8'
}
)
.
trim
(
)
;
const
currentCommit
=
execSync
(
'git rev-parse HEAD'
,
{
encoding
:
'utf-8'
}
)
.
trim
(
)
;
console
.
log
(
`检测从
${
lastCommit
}
到
${
currentCommit
}
的变动...`
)
;
// 获取变动的文件
const
changedFiles
=
execSync
(
`git diff --name-only
${
lastCommit
}
${
currentCommit
}
`
,
{
encoding
:
'utf-8'
}
)
.
trim
(
)
.
split
(
'\n'
)
.
filter
(
Boolean
)
;
console
.
log
(
'变动的文件:'
,
changedFiles
)
;
// 提取变动的插件
const
changedPlugins
=
new
Set
(
)
;
changedFiles
.
forEach
(
file
=>
{
if
(
file
.
startsWith
(
'plugins/'
)
)
{
const
parts
=
file
.
split
(
'/'
)
;
if
(
parts
.
length
>=
2
)
{
changedPlugins
.
add
(
parts
[
1
]
)
;
}
}
}
)
;
const
existingPlugins
=
Array
.
from
(
changedPlugins
)
.
filter
(
name
=>
{
const
pluginPath
=
join
(
PLUGINS_DIR
,
name
)
;
return
existsSync
(
pluginPath
)
&&
statSync
(
pluginPath
)
.
isDirectory
(
)
;
}
)
;
const
deletedPlugins
=
Array
.
from
(
changedPlugins
)
.
filter
(
name
=>
!
existingPlugins
.
includes
(
name
)
)
;
if
(
deletedPlugins
.
length
>
0
)
{
console
.
log
(
`忽略已删除的插件目录:
${
deletedPlugins
.
join
(
', '
)
}
`
)
;
}
return
{
changedPlugins
:
existingPlugins
,
deletedPlugins
}
;
}
catch
(
error
)
{
console
.
log
(
'无法检测git变动,可能是首次提交,构建所有插件'
)
;
return
{
changedPlugins
:
getAllPlugins
(
)
,
deletedPlugins
:
[
]
}
;
}
}
/**
* 生成release版本号
*/
function
generateReleaseVersion
(
)
{
const
now
=
new
Date
(
)
;
const
year
=
now
.
getFullYear
(
)
;
const
month
=
String
(
now
.
getMonth
(
)
+
1
)
.
padStart
(
2
,
'0'
)
;
const
day
=
String
(
now
.
getDate
(
)
)
.
padStart
(
2
,
'0'
)
;
const
hour
=
String
(
now
.
getHours
(
)
)
.
padStart
(
2
,
'0'
)
;
const
minute
=
String
(
now
.
getMinutes
(
)
)
.
padStart
(
2
,
'0'
)
;
return
`
${
year
}
.
${
month
}
.
${
day
}
.
${
hour
}
${
minute
}
`
;
}
// 主逻辑
const
{
changedPlugins
,
deletedPlugins
}
=
detectChangedPlugins
(
)
;
if
(
changedPlugins
.
length
===
0
&&
deletedPlugins
.
length
===
0
)
{
console
.
log
(
'没有检测到插件变动'
)
;
// 写入GitHub Actions输出
if
(
process
.
env
.
GITHUB_OUTPUT
)
{
writeFileSync
(
process
.
env
.
GITHUB_OUTPUT
,
'has_changes=false\n'
,
{
flag
:
'a'
}
)
;
}
process
.
exit
(
0
)
;
}
console
.
log
(
`检测到
${
changedPlugins
.
length
}
个插件变动:`
,
changedPlugins
)
;
if
(
deletedPlugins
.
length
>
0
)
{
console
.
log
(
`检测到
${
deletedPlugins
.
length
}
个插件删除:`
,
deletedPlugins
)
;
}
// 生成release版本号
const
releaseVersion
=
generateReleaseVersion
(
)
;
// 输出到GitHub Actions(只输出插件名称列表)
if
(
process
.
env
.
GITHUB_OUTPUT
)
{
const
outputContent
=
[
'has_changes=true'
,
`release_version=
${
releaseVersion
}
`
,
`changed_plugins=
${
changedPlugins
.
join
(
','
)
}
`
,
`deleted_plugins=
${
deletedPlugins
.
join
(
','
)
}
`
]
.
join
(
'\n'
)
+
'\n'
;
writeFileSync
(
process
.
env
.
GITHUB_OUTPUT
,
outputContent
,
{
flag
:
'a'
}
)
;
}
console
.
log
(
'构建信息:'
)
;
console
.
log
(
` - 有变动: true`
)
;
console
.
log
(
` - Release版本:
${
releaseVersion
}
`
)
;
console
.
log
(
` - 变动插件:
${
changedPlugins
.
join
(
', '
)
}
`
)
;
// 保存到文件供后续步骤使用
const
output
=
{
hasChanges
:
true
,
releaseVersion
,
changedPlugins
:
changedPlugins
,
buildAll
:
BUILD_ALL
,
requestedPlugins
:
BUILD_PLUGINS
,
deletedPlugins
}
;
// 确保release目录存在
const
releaseDir
=
'release'
;
if
(
!
existsSync
(
releaseDir
)
)
{
mkdirSync
(
releaseDir
,
{
recursive
:
true
}
)
;
}
writeFileSync
(
'release/build-info.json'
,
JSON
.
stringify
(
output
,
null
,
2
)
)
;
console
.
log
(
'构建信息已保存到 release/build-info.json'
)
;
You can’t perform that action at this time.