From 6e667216886fc4227b3ec8f8e1fe532067ebaf4f Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Mon, 14 Apr 2025 14:40:17 +0800 Subject: [PATCH] feat: add after-build script for renaming files and updating latest.yml - Introduced a new script to rename files with spaces in the 'dist' directory. - Updated 'latest.yml' to remove the first file entry and adjust paths accordingly. - Enhanced build process for Windows to include the new script execution. - Added js-yaml dependency for YAML file manipulation. --- dev-app-update.yml | 10 +++--- electron-builder.yml | 13 +++++--- package.json | 4 ++- scripts/after-build.js | 72 ++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 9 ++++++ 5 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 scripts/after-build.js diff --git a/dev-app-update.yml b/dev-app-update.yml index 6c9cb28c9..cb30636af 100644 --- a/dev-app-update.yml +++ b/dev-app-update.yml @@ -1,8 +1,8 @@ # provider: generic # url: http://127.0.0.1:8080 # updaterCacheDirName: cherry-studio-updater -provider: github -repo: cherry-studio -owner: kangfenmao -# provider: generic -# url: https://cherrystudio.ocool.online +# provider: github +# repo: cherry-studio +# owner: kangfenmao +provider: generic +url: https://releases.cherry-ai.com diff --git a/electron-builder.yml b/electron-builder.yml index b0ba9ca51..b36f4f809 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -35,7 +35,13 @@ win: artifactName: ${productName}-${version}-${arch}-setup.${ext} target: - target: nsis + arch: + - x64 + - arm64 - target: portable + arch: + - x64 + - arm64 nsis: artifactName: ${productName}-${version}-${arch}-setup.${ext} shortcutName: ${productName} @@ -74,11 +80,8 @@ linux: maintainer: electronjs.org category: Utility publish: - # provider: generic - # url: https://cherrystudio.ocool.online - provider: github - repo: cherry-studio - owner: CherryHQ + provider: generic + url: https://releases.cherry-ai.com electronDownload: mirror: https://npmmirror.com/mirrors/electron/ afterPack: scripts/after-pack.js diff --git a/package.json b/package.json index 8c8facddf..3bb284a80 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", + "build:win": "dotenv npm run build && electron-builder --win && node scripts/after-build.js", "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", @@ -86,6 +86,7 @@ "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", @@ -122,6 +123,7 @@ "@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 new file mode 100644 index 000000000..033a0aea1 --- /dev/null +++ b/scripts/after-build.js @@ -0,0 +1,72 @@ +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 { + // First rename files with spaces + await renameFilesWithSpaces() + + // 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') + + console.log('Successfully cleaned up latest.yml data') + } catch (error) { + console.error('Error processing latest.yml:', error) + throw error + } +} + +afterBuild() diff --git a/yarn.lock b/yarn.lock index 8b28d187d..b06e3a2f2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3432,6 +3432,13 @@ __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" @@ -3927,6 +3934,7 @@ __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" @@ -3976,6 +3984,7 @@ __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"