Skip to content

Commit c30bac8

Browse files
committed
build:重构开源许可证 JSON 生成流程
- 新增 OSSLicensesJSONBuild.js 文件,用于生成开源项目许可证 JSON 信息 - 修改 package.json,更新 license-build 脚本为使用新工具 - 删除 licenses-mp.js 中的冗余空行
1 parent cb1a751 commit c30bac8

File tree

4 files changed

+87
-27
lines changed

4 files changed

+87
-27
lines changed

source/OSSLicensesJSONBuild.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// OSSLicensesJSONBuild.js
2+
3+
/*
4+
Copyright (c) 2024 ArcticFoxPro
5+
Qverbow Vigor is licensed under Mulan PubL v2.
6+
You can use this software according to the terms and conditions of the Mulan PubL v2.
7+
You may obtain a copy of Mulan PubL v2 at:
8+
http://license.coscl.org.cn/MulanPubL-2.0
9+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
10+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
11+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
12+
See the Mulan PubL v2 for more details.
13+
*/
14+
15+
/**
16+
* 此文件用于为微信小程序生成开源项目许可证 JSON 信息,并通过 JavaScript `module.exports` 语句导出为 JS 对象。
17+
*
18+
* 每次小程序发布前均需手动执行 `node OSSLicensesJSONBuild.js`
19+
*/
20+
21+
const { execSync } = require('child_process');
22+
const fs = require('fs');
23+
const path = require('path');
24+
25+
function runCommand(command) {
26+
try {
27+
return execSync(command, { encoding: 'utf8' });
28+
} catch (error) {
29+
console.error(`命令执行失败: ${command}`);
30+
process.exit(1);
31+
}
32+
}
33+
34+
/**
35+
* 生成并保存项目依赖的许可证信息
36+
*
37+
* 该函数通过执行外部命令获取项目依赖的许可证信息,并将其保存为JSON和JS文件
38+
* 执行完成后,会删除JSON文件,仅保留JS文件
39+
*
40+
* @param {string} outputFile - 输出文件的名称,不包含文件扩展名
41+
* @param {string} customPath - 指定依赖项的自定义路径
42+
* @param {string} [startPath=''] - 可选参数,指定从哪个路径开始查找依赖项,默认为当前目录
43+
*/
44+
function buildLicenses(outputFile, customPath, startPath = '') {
45+
const jsonFile = path.join(__dirname, `pages/utils/${outputFile}.json`);
46+
const jsFile = path.join(__dirname, `pages/utils/${outputFile}.js`);
47+
const command = `license-checker-rseidelsohn ${startPath} --customPath ${customPath} --json`;
48+
const output = runCommand(command);
49+
fs.writeFileSync(jsonFile, output, 'utf8');
50+
const jsonData = JSON.parse(output);
51+
const jsContent = `module.exports = ${JSON.stringify(jsonData, null, 2)};`;
52+
fs.writeFileSync(jsFile, jsContent, 'utf8');
53+
fs.unlinkSync(jsonFile);
54+
}
55+
56+
function main() {
57+
const customPath = path.join(__dirname, 'pages/utils/licenseFormat.json');
58+
buildLicenses('licenses-build', customPath);
59+
buildLicenses('licenses-mp', customPath, '--start ./pages/');
60+
}
61+
62+
main();

source/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
"scripts": {
88
"unocss": "unocss pages/**/*.wxml !pages/**/node_modules/** !pages/**/miniprogram_npm/** -c uno.config.js --watch -o unocss.wxss",
99
"unocss:build": "unocss pages/**/*.wxml !pages/**/node_modules/** !pages/**/miniprogram_npm/** -c uno.config.js -o unocss.wxss",
10-
"license-build": "powershell -Command \"license-checker-rseidelsohn --customPath .\\pages\\utils\\licenseFormat.json --json | jq '.' | Out-File -FilePath .\\pages\\utils\\licenses-build.json -Encoding utf8; echo 'module.exports = ' | Out-File -FilePath .\\pages\\utils\\licenses-build.js -Encoding utf8 -NoNewline; Get-Content .\\pages\\utils\\licenses-build.json | Out-File -FilePath .\\pages\\utils\\licenses-build.js -Encoding utf8 -Append; Remove-Item .\\pages\\utils\\licenses-build.json\"",
11-
"license-mp": "powershell -Command \"license-checker-rseidelsohn --start .\\pages\\ --customPath .\\pages\\utils\\licenseFormat.json --json | jq '.' | Out-File -FilePath .\\pages\\utils\\licenses-mp.json -Encoding utf8; echo 'module.exports = ' | Out-File -FilePath .\\pages\\utils\\licenses-mp.js -Encoding utf8 -NoNewline; Get-Content .\\pages\\utils\\licenses-mp.json | Out-File -FilePath .\\pages\\utils\\licenses-mp.js -Encoding utf8 -Append; Remove-Item .\\pages\\utils\\licenses-mp.json\""
12-
},
13-
"type": "module"
10+
"license-build": "node OSSLicensesJSONBuild.js"
11+
}
1412
}

0 commit comments

Comments
 (0)