Skip to content

Commit ee236a7

Browse files
committed
build(oss-licenses): 重构构建过程并优化 GitHub Actions 工作流
- 移除了 normalizeLineEndings 和 writeFileCrossPlatform 函数,简化了文件写入逻辑 - 更新了 runCommand 函数,移除了对 Windows 平台的特殊处理 - 优化了 OSSLicensesDist.js 的构建流程 - 更新了 GitHub Actions 工作流,使用 Ubuntu环境替代 Windows - 添加了 unix2dos 步骤,确保文件格式兼容 Windows -简化了文件比较逻辑,使用 git diff 替代自定义脚本
1 parent bcf729b commit ee236a7

File tree

2 files changed

+27
-48
lines changed

2 files changed

+27
-48
lines changed

.github/workflows/update-oss-licenses-dist.yml

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
update-oss-licenses-dist:
10-
runs-on: windows-latest
10+
runs-on: ubuntu-latest
1111
permissions:
1212
contents: write
1313
pull-requests: write
@@ -27,38 +27,35 @@ jobs:
2727
- name: Install dependencies
2828
run: yarn install
2929

30-
- name: Create backup of OSSLicensesDist.js
30+
- name: Install unix2dos
31+
run: sudo apt-get update && sudo apt-get install unix2dos
32+
33+
- name: Backup OSSLicensesDist.js
3134
run: |
32-
if (Test-Path pages\utils\OSSLicensesDist.js) {
33-
Copy-Item pages\utils\OSSLicensesDist.js pages\utils\OSSLicensesDist.js.bak
34-
} else {
35-
New-Item -ItemType File -Path pages\utils\OSSLicensesDist.js.bak -Force
36-
}
37-
shell: pwsh
35+
if [ -f pages/utils/OSSLicensesDist.js ]; then
36+
cp pages/utils/OSSLicensesDist.js pages/utils/OSSLicensesDist.js.bak
37+
else
38+
touch pages/utils/OSSLicensesDist.js.bak
39+
fi
3840
3941
- name: Build OSSLicensesDist.js
4042
run: yarn run build-oss-licenses-dist
4143

44+
- name: unix2dos OSSLicensesDist.js
45+
run: unix2dos pages/utils/OSSLicensesDist.js
46+
4247
- name: Compare files
4348
id: compare-files
4449
run: |
45-
$file1 = Get-Content pages\utils\OSSLicensesDist.js
46-
$file2 = Get-Content pages\utils\OSSLicensesDist.js.bak
47-
if ($null -eq $file1 -or $null -eq $file2) {
48-
echo "files_changed=true" >> $env:GITHUB_ENV
49-
} else {
50-
if (Compare-Object -ReferenceObject $file1 -DifferenceObject $file2) {
51-
echo "files_changed=true" >> $env:GITHUB_ENV
52-
} else {
53-
echo "files_changed=false" >> $env:GITHUB_ENV
54-
}
55-
}
56-
shell: pwsh
50+
if ! git diff --no-index --ignore-space-at-eol pages/utils/OSSLicensesDist.js pages/utils/OSSLicensesDist.js.bak; then
51+
echo "files_changed=true" >> $GITHUB_ENV
52+
else
53+
echo "files_changed=false" >> $GITHUB_ENV
54+
fi
5755
58-
- name: Delete backup file
59-
run: |
60-
Remove-Item pages\utils\OSSLicensesDist.js.bak -Force
61-
shell: pwsh
56+
- name: Delete OSSLicensesDist.js.bak
57+
if: env.files_changed == 'true'
58+
run: rm pages/utils/OSSLicensesDist.js.bak
6259

6360
- name: Create Pull Request
6461
if: env.files_changed == 'true'
@@ -68,4 +65,4 @@ jobs:
6865
body: '自动化更新 OSSLicensesDist.js'
6966
branch: 'update-licenses'
7067
commit-message: 'Update OSSLicensesDist.js'
71-
labels: 'auto-generated'
68+
labels: 'auto-generated'

source/OSSLicensesBuilder.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,13 @@ const path = require('path');
2424
const uglifyJS = require('uglify-js');
2525
const JSON5 = require('json5');
2626

27-
function normalizeLineEndings(content) {
28-
return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
29-
}
30-
31-
function writeFileCrossPlatform(filePath, content) {
32-
const normalized = normalizeLineEndings(content);
33-
const cleanContent = normalized.charCodeAt(0) === 0xFEFF ? normalized.slice(1) : normalized;
34-
fs.writeFileSync(filePath, cleanContent, {
35-
encoding: 'utf8', flag: 'w', mode: 0o644
36-
});
37-
}
38-
3927
function runCommand(command, args) {
40-
const isWindows = process.platform === 'win32';
41-
const sanitizedArgs = args.map(arg => {
42-
return isWindows ? arg.replace(/\//g, '\\') : arg;
43-
});
4428
try {
45-
const result = shell.exec(`${command} ${sanitizedArgs.join(' ')}`, {
29+
const result = shell.exec(`${command} ${args.join(' ')}`, {
4630
silent: true, env: {...process.env, LC_ALL: 'C.UTF-8'}
4731
});
4832
if (result.code !== 0) throw new Error(result.stderr || `Command failed with code ${result.code}`);
49-
return normalizeLineEndings(result.stdout);
33+
return result.stdout;
5034
} catch (error) {
5135
console.error(`[构建错误] ${error.message}`);
5236
process.exit(1);
@@ -73,7 +57,7 @@ function buildLicenses(outputFile, customFormat, customPath, startPath = '') {
7357
const output = runCommand('license-checker-rseidelsohn', [startPath, '--customPath', customFormat, '--json']);
7458
const jsonData = JSON.parse(output);
7559
if (Object.keys(jsonData).length === 0) throw new Error('生成的许可证数据为空');
76-
writeFileCrossPlatform(jsonFile, output);
60+
fs.writeFileSync(jsonFile, output, {encoding: 'utf8', flag: 'w', mode: 0o644});
7761
const jsContent = `module.exports = ${JSON.stringify(jsonData, null, 2)};`;
7862
const minifiedResult = uglifyJS.minify(jsContent, {
7963
mangle: {toplevel: true}, compress: {
@@ -83,11 +67,9 @@ function buildLicenses(outputFile, customFormat, customPath, startPath = '') {
8367
}
8468
});
8569
if (minifiedResult.error) throw minifiedResult.error;
86-
writeFileCrossPlatform(jsFile, minifiedResult.code);
70+
fs.writeFileSync(jsFile, minifiedResult.code, {encoding: 'utf8', flag: 'w', mode: 0o644});
8771
} finally {
88-
if (fs.existsSync(jsonFile)) {
89-
shell.rm('-f', jsonFile);
90-
}
72+
if (fs.existsSync(jsonFile)) shell.rm('-f', jsonFile);
9173
}
9274
}
9375

0 commit comments

Comments
 (0)