|
| 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(); |
0 commit comments