From 75f986087a13337e812e49ce35248d22e756ad63 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Thu, 17 Apr 2025 10:05:48 +0800 Subject: [PATCH] chore(electron-builder): Simplify file renaming logic and remove space (#4919) * chore(electron-builder): Disable universal installer option in NSIS configuration * refactor(after-build): Change file handling to delete files with spaces and rename files in YAML data - Updated the function to delete files containing spaces instead of renaming them. - Enhanced YAML processing to rename files and their blockmaps, ensuring proper handling of setup and portable versions. - Adjusted the final YAML output to reflect the new file names. * refactor(after-build): Simplify file renaming logic and remove space handling script - Updated the after-build script to rename artifact files by replacing spaces with hyphens. - Removed the replace-spaces.js script as its functionality is now integrated into the after-build process. - Adjusted the build process in package.json to reflect the changes in file handling. * refactor(electron-builder): Update artifact build script reference and remove obsolete after-build script - Changed the artifactBuildCompleted script reference in electron-builder.yml to point to the new script. - Deleted the outdated after-build.js script, which is no longer needed for file handling. * delete js-yml --- .github/workflows/release.yml | 3 -- electron-builder.yml | 2 + package.json | 4 +- scripts/after-build.js | 72 ----------------------------- scripts/artifact-build-completed.js | 23 +++++++++ scripts/replace-spaces.js | 58 ----------------------- yarn.lock | 9 ---- 7 files changed, 26 insertions(+), 145 deletions(-) delete mode 100644 scripts/after-build.js create mode 100644 scripts/artifact-build-completed.js delete mode 100644 scripts/replace-spaces.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fe49f9ea..bb5f6796 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -95,9 +95,6 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} - - name: Replace spaces in filenames - run: node scripts/replace-spaces.js - - name: Release uses: ncipollo/release-action@v1 with: diff --git a/electron-builder.yml b/electron-builder.yml index 75a35da4..29ee7316 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -50,6 +50,7 @@ nsis: allowToChangeInstallationDirectory: true oneClick: false include: build/nsis-installer.nsh + buildUniversalInstaller: false portable: artifactName: ${productName}-${version}-${arch}-portable.${ext} mac: @@ -77,6 +78,7 @@ electronDownload: mirror: https://npmmirror.com/mirrors/electron/ afterPack: scripts/after-pack.js afterSign: scripts/notarize.js +artifactBuildCompleted: scripts/artifact-build-completed.js releaseInfo: releaseNotes: | 全新图标风格 diff --git a/package.json b/package.json index bd300c04..779a34b8 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "build": "npm run typecheck && electron-vite build", "build:check": "yarn test && yarn typecheck && yarn check:i18n", "build:unpack": "dotenv npm run build && electron-builder --dir", - "build:win": "dotenv npm run build && electron-builder --win --x64 --arm64 && node scripts/after-build.js", + "build:win": "dotenv npm run build && electron-builder --win --x64 --arm64", "build:win:x64": "dotenv npm run build && electron-builder --win --x64", "build:win:arm64": "dotenv npm run build && electron-builder --win --arm64", "build:mac": "dotenv electron-vite build && electron-builder --mac --arm64 --x64", @@ -86,7 +86,6 @@ "fetch-socks": "^1.3.2", "fs-extra": "^11.2.0", "got-scraping": "^4.1.1", - "js-yaml": "^4.1.0", "jsdom": "^26.0.0", "markdown-it": "^14.1.0", "officeparser": "^4.1.1", @@ -124,7 +123,6 @@ "@types/adm-zip": "^0", "@types/diff": "^7", "@types/fs-extra": "^11", - "@types/js-yaml": "^4", "@types/lodash": "^4.17.5", "@types/markdown-it": "^14", "@types/md5": "^2.3.5", diff --git a/scripts/after-build.js b/scripts/after-build.js deleted file mode 100644 index e8c1e824..00000000 --- a/scripts/after-build.js +++ /dev/null @@ -1,72 +0,0 @@ -const fs = require('fs') -const path = require('path') -const yaml = require('js-yaml') - -async function renameFilesWithSpaces() { - const distPath = path.join('dist') - const files = fs.readdirSync(distPath, { withFileTypes: true }) - - // Only process files in the root of dist directory, not subdirectories - files.forEach((file) => { - if (file.isFile() && file.name.includes(' ')) { - const oldPath = path.join(distPath, file.name) - const newName = file.name.replace(/ /g, '-') - const newPath = path.join(distPath, newName) - - fs.renameSync(oldPath, newPath) - console.log(`Renamed: ${file.name} -> ${newName}`) - } - }) -} - -async function afterBuild() { - console.log('[After build] hook started...') - - try { - // Read the latest.yml file - const latestYmlPath = path.join('dist', 'latest.yml') - const yamlContent = fs.readFileSync(latestYmlPath, 'utf8') - const data = yaml.load(yamlContent) - - // Remove the first element from files array - if (data.files && data.files.length > 1) { - const file = data.files.shift() - - // Remove Cherry Studio-1.2.3-setup.exe - fs.rmSync(path.join('dist', file.url)) - fs.rmSync(path.join('dist', file.url + '.blockmap')) - - // Remove Cherry Studio-1.2.3-portable.exe - fs.rmSync(path.join('dist', file.url.replace('-setup', '-portable'))) - - // Update path and sha512 with the new first element's data - if (data.files[0]) { - data.path = data.files[0].url - data.sha512 = data.files[0].sha512 - } - } - - // Write back the modified YAML with specific dump options - const newYamlContent = yaml.dump(data, { - lineWidth: -1, // Prevent line wrapping - quotingType: '"', // Use double quotes when needed - forceQuotes: false, // Only quote when necessary - noCompatMode: true, // Use new style options - styles: { - '!!str': 'plain' // Force plain style for strings - } - }) - - fs.writeFileSync(latestYmlPath, newYamlContent, 'utf8') - - // Rename files with spaces - await renameFilesWithSpaces() - - console.log('Successfully cleaned up latest.yml data') - } catch (error) { - console.error('Error processing latest.yml:', error) - throw error - } -} - -afterBuild() diff --git a/scripts/artifact-build-completed.js b/scripts/artifact-build-completed.js new file mode 100644 index 00000000..32f082f9 --- /dev/null +++ b/scripts/artifact-build-completed.js @@ -0,0 +1,23 @@ +const fs = require('fs') + +exports.default = function (buildResult) { + try { + console.log('[artifact build completed] rename artifact file...') + if (!buildResult.file.includes(' ')) { + return + } + + let oldFilePath = buildResult.file + if (oldFilePath.includes('-portable') && !oldFilePath.includes('-x64') && !oldFilePath.includes('-arm64')) { + console.log('[artifact build completed] delete portable file:', oldFilePath) + fs.unlinkSync(oldFilePath) + return + } + const newfilePath = oldFilePath.replace(/ /g, '-') + fs.renameSync(oldFilePath, newfilePath) + buildResult.file = newfilePath + console.log(`[artifact build completed] rename file ${oldFilePath} to ${newfilePath} `) + } catch (error) { + console.error('Error renaming file:', error) + } +} diff --git a/scripts/replace-spaces.js b/scripts/replace-spaces.js deleted file mode 100644 index 80009e0d..00000000 --- a/scripts/replace-spaces.js +++ /dev/null @@ -1,58 +0,0 @@ -// replaceSpaces.js - -const fs = require('fs') -const path = require('path') - -const directory = 'dist' - -// 处理文件名中的空格 -function replaceFileNames() { - fs.readdir(directory, (err, files) => { - if (err) throw err - - files.forEach((file) => { - const oldPath = path.join(directory, file) - const newPath = path.join(directory, file.replace(/ /g, '-')) - - fs.stat(oldPath, (err, stats) => { - if (err) throw err - - if (stats.isFile() && oldPath !== newPath) { - fs.rename(oldPath, newPath, (err) => { - if (err) throw err - console.log(`Renamed: ${oldPath} -> ${newPath}`) - }) - } - }) - }) - }) -} - -function replaceYmlContent() { - fs.readdir(directory, (err, files) => { - if (err) throw err - - files.forEach((file) => { - if (path.extname(file).toLowerCase() === '.yml') { - const filePath = path.join(directory, file) - - fs.readFile(filePath, 'utf8', (err, data) => { - if (err) throw err - - // 替换内容 - const newContent = data.replace(/Cherry Studio-/g, 'Cherry-Studio-') - - // 写回文件 - fs.writeFile(filePath, newContent, 'utf8', (err) => { - if (err) throw err - console.log(`Updated content in: ${filePath}`) - }) - }) - } - }) - }) -} - -// 执行两个操作 -replaceFileNames() -replaceYmlContent() diff --git a/yarn.lock b/yarn.lock index 2b4f2fd1..905a5b5b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3668,13 +3668,6 @@ __metadata: languageName: node linkType: hard -"@types/js-yaml@npm:^4": - version: 4.0.9 - resolution: "@types/js-yaml@npm:4.0.9" - checksum: 10c0/24de857aa8d61526bbfbbaa383aa538283ad17363fcd5bb5148e2c7f604547db36646440e739d78241ed008702a8920665d1add5618687b6743858fae00da211 - languageName: node - linkType: hard - "@types/json-schema@npm:^7.0.15": version: 7.0.15 resolution: "@types/json-schema@npm:7.0.15" @@ -4192,7 +4185,6 @@ __metadata: "@types/adm-zip": "npm:^0" "@types/diff": "npm:^7" "@types/fs-extra": "npm:^11" - "@types/js-yaml": "npm:^4" "@types/lodash": "npm:^4.17.5" "@types/markdown-it": "npm:^14" "@types/md5": "npm:^2.3.5" @@ -4244,7 +4236,6 @@ __metadata: html-to-image: "npm:^1.11.13" husky: "npm:^9.1.7" i18next: "npm:^23.11.5" - js-yaml: "npm:^4.1.0" jsdom: "npm:^26.0.0" lint-staged: "npm:^15.5.0" lodash: "npm:^4.17.21"