11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
33
4- // 项目根目录
5- const scriptingDir = path . resolve ( __dirname , "../" ) ;
6- const readmePath = path . resolve ( scriptingDir , "README.md" ) ;
4+ // 获取项目根目录(GitHub Actions 中可用 GITHUB_WORKSPACE)
5+ const projectRoot = process . env . GITHUB_WORKSPACE || path . resolve ( __dirname , "../" ) ;
6+ const scriptingDir = projectRoot ;
7+ const readmePath = path . join ( projectRoot , "README.md" ) ;
78
89let collectedLinks = [ ] ;
910
1011// 遍历目录,收集 .scripting 文件
1112function walkDir ( dir , callback ) {
1213 fs . readdirSync ( dir ) . forEach ( ( file ) => {
1314 const filepath = path . join ( dir , file ) ;
14- if ( fs . statSync ( filepath ) . isFile ( ) && filepath . endsWith ( ".scripting" ) ) {
15+ const stat = fs . statSync ( filepath ) ;
16+ if ( stat . isFile ( ) && filepath . endsWith ( ".scripting" ) ) {
1517 callback ( filepath ) ;
18+ } else if ( stat . isDirectory ( ) ) {
19+ walkDir ( filepath , callback ) ; // 递归子目录
1620 }
1721 } ) ;
1822}
1923
2024// 生成 scripting.fun 链接
2125function generateScriptLink ( filename ) {
2226 const baseUrl = "https://github.com/ScriptingApp/Community-Scripts/raw/refs/heads/main/" ;
23- const fullUrl = baseUrl + encodeURIComponent ( path . basename ( filename ) ) ;
27+ const relativePath = path . relative ( scriptingDir , filename ) . replace ( / \\ / g, "/" ) ; // 保留目录结构
28+ const fullUrl = baseUrl + encodeURIComponent ( relativePath ) ;
2429 const name = path . basename ( filename , ".scripting" ) ; // 去掉后缀
2530 const link = `https://scripting.fun/import_scripts?urls=${ encodeURIComponent ( `[\"${ fullUrl } \"]` ) } ` ;
2631 return { name, link } ;
@@ -35,8 +40,10 @@ walkDir(scriptingDir, (file) => {
3540let readmeContent = fs . existsSync ( readmePath ) ? fs . readFileSync ( readmePath , "utf-8" ) : "" ;
3641const startTag = "<!-- SCRIPTS_LINKS_START -->" ;
3742const endTag = "<!-- SCRIPTS_LINKS_END -->" ;
43+
44+ // Markdown 列表 + 时间戳保证内容每次都变
3845const linksMarkdown = collectedLinks . map ( ( { name, link } ) => `- [${ name } ](${ link } )` ) . join ( "\n" ) ;
39- const replacement = `${ startTag } \n${ linksMarkdown } \n${ endTag } ` ;
46+ const replacement = `${ startTag } \n${ linksMarkdown } \n<!-- updated at ${ new Date ( ) . toISOString ( ) } -->\n ${ endTag } ` ;
4047
4148// 替换或追加
4249if ( readmeContent . includes ( startTag ) ) {
@@ -45,5 +52,6 @@ if (readmeContent.includes(startTag)) {
4552 readmeContent += `\n\n${ replacement } \n` ;
4653}
4754
55+ // 写入 README.md,GitHub Actions workflow 会提交
4856fs . writeFileSync ( readmePath , readmeContent , "utf-8" ) ;
4957console . log ( "README.md updated with latest links." ) ;
0 commit comments