From 5c42690586b915abee63c586d66929da75e54c4b Mon Sep 17 00:00:00 2001 From: Kuingsmile Date: Wed, 5 Mar 2025 13:19:03 +0800 Subject: [PATCH] :package: Chore(custom): update scripts --- scripts/check-dep.js | 4 +- scripts/config.js | 96 +++++++++++++++------------- scripts/gen-i18n-types.js | 3 +- scripts/gen-sha256.js | 130 ++++++++++++++++++++++++++++++-------- scripts/notarize.js | 9 +-- scripts/upload-beta.js | 2 +- 6 files changed, 163 insertions(+), 81 deletions(-) diff --git a/scripts/check-dep.js b/scripts/check-dep.js index a855d20..ba21d40 100644 --- a/scripts/check-dep.js +++ b/scripts/check-dep.js @@ -1,7 +1,7 @@ const ncu = require('npm-check-updates') const axios = require('axios') -async function getRepositoryInfo (packageName) { +async function getRepositoryInfo(packageName) { try { const { data } = await axios.get(`https://registry.npmjs.org/${packageName}`) const repository = data.repository @@ -17,7 +17,7 @@ async function getRepositoryInfo (packageName) { return null } -async function checkUpdates () { +async function checkUpdates() { const updated = await ncu.run({ packageFile: './package.json', upgrade: false diff --git a/scripts/config.js b/scripts/config.js index 8318363..358adcc 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -1,53 +1,63 @@ // different platform has different format // macos -const darwin = [{ - appNameWithPrefix: 'PicList-', - ext: '.dmg', - arch: '-arm64', - 'version-file': 'latest-mac.yml' -}, { - appNameWithPrefix: 'PicList-', - ext: '.dmg', - arch: '-x64', - 'version-file': 'latest-mac.yml' -}, { - appNameWithPrefix: 'PicList-', - ext: '.dmg', - arch: '-universal', - 'version-file': 'latest-mac.yml' -} +const darwin = [ + { + appNameWithPrefix: 'PicList-', + ext: '.dmg', + arch: '-arm64', + 'version-file': 'latest-mac.yml' + }, + { + appNameWithPrefix: 'PicList-', + ext: '.dmg', + arch: '-x64', + 'version-file': 'latest-mac.yml' + }, + { + appNameWithPrefix: 'PicList-', + ext: '.dmg', + arch: '-universal', + 'version-file': 'latest-mac.yml' + } ] -const linux = [{ - appNameWithPrefix: 'PicList-', - ext: '.AppImage', - arch: '', - 'version-file': 'latest-linux.yml' -}, { - appNameWithPrefix: 'piclist_', - ext: '.snap', - arch: '_amd64', - 'version-file': 'latest-linux.yml' -}] +const linux = [ + { + appNameWithPrefix: 'PicList-', + ext: '.AppImage', + arch: '', + 'version-file': 'latest-linux.yml' + }, + { + appNameWithPrefix: 'piclist_', + ext: '.snap', + arch: '_amd64', + 'version-file': 'latest-linux.yml' + } +] // windows -const win32 = [{ - appNameWithPrefix: 'PicList-Setup-', - ext: '.exe', - arch: '-ia32', - 'version-file': 'latest.yml' -}, { - appNameWithPrefix: 'PicList-Setup-', - ext: '.exe', - arch: '-x64', - 'version-file': 'latest.yml' -}, { - appNameWithPrefix: 'PicList-Setup-', - ext: '.exe', - arch: '', // 32 & 64 - 'version-file': 'latest.yml' -}] +const win32 = [ + { + appNameWithPrefix: 'PicList-Setup-', + ext: '.exe', + arch: '-ia32', + 'version-file': 'latest.yml' + }, + { + appNameWithPrefix: 'PicList-Setup-', + ext: '.exe', + arch: '-x64', + 'version-file': 'latest.yml' + }, + { + appNameWithPrefix: 'PicList-Setup-', + ext: '.exe', + arch: '', // 32 & 64 + 'version-file': 'latest.yml' + } +] module.exports = { darwin, diff --git a/scripts/gen-i18n-types.js b/scripts/gen-i18n-types.js index 3f15626..d0e29b1 100644 --- a/scripts/gen-i18n-types.js +++ b/scripts/gen-i18n-types.js @@ -12,8 +12,7 @@ const obj = yaml.load(langFile) const keys = Object.keys(obj) -const types = -`interface ILocales { +const types = `interface ILocales { ${keys.map(key => `${key}: string`).join('\n ')} } type ILocalesKey = keyof ILocales diff --git a/scripts/gen-sha256.js b/scripts/gen-sha256.js index 7ebaae6..3cb7cdb 100644 --- a/scripts/gen-sha256.js +++ b/scripts/gen-sha256.js @@ -4,55 +4,133 @@ const path = require('path') const axios = require('axios') const fs = require('fs-extra') const pkg = require('../package.json') -const version = pkg.version +const version = process.argv[2] || pkg.version -const x64URL = `https://release.piclist.cn/latest/PicList-${version}-x64.dmg` -const arm64URL = `https://release.piclist.cn/latest/PicList-${version}-arm64.dmg` -const x64Path = path.join(os.homedir(), 'Downloads', 'PicList-x64.dmg') -const arm64Path = path.join(os.homedir(), 'Downloads', 'PicList-arm64.dmg') +// Configuration +const BASE_URL = `https://github.com/Kuingsmile/PicList/releases/download/v${version}` +const DOWNLOAD_DIR = process.argv[3] || path.join(os.homedir(), 'Downloads') +// File information +const files = [ + { + name: 'PicList-x64.dmg', + url: `${BASE_URL}/PicList-${version}-x64.dmg` + }, + { + name: 'PicList-arm64.dmg', + url: `${BASE_URL}/PicList-${version}-arm64.dmg` + } +] + +/** + * Create progress bar string + */ +function getProgressBar(current, total, length = 20) { + const progress = Math.round((current / total) * length) + const percentage = Math.round((current / total) * 100) + const bar = '█'.repeat(progress) + '░'.repeat(length - progress) + return `[${bar}] ${percentage}% (${formatBytes(current)}/${formatBytes(total)})` +} + +/** + * Format bytes to human-readable format + */ +function formatBytes(bytes, decimals = 2) { + if (bytes === 0) return '0 Bytes' + const k = 1024 + const sizes = ['Bytes', 'KB', 'MB', 'GB'] + const i = Math.floor(Math.log(bytes) / Math.log(k)) + return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i] +} + +/** + * Download file and calculate SHA256 hash + */ +async function downloadAndHash(fileInfo) { + const { url, name } = fileInfo + const filePath = path.join(DOWNLOAD_DIR, name) + + console.log(`\nStarting download: ${name} from ${url}`) -const downloadAndHash = async (url, path) => { try { const response = await axios({ method: 'get', url, responseType: 'stream' }) - const writer = fs.createWriteStream(path) + + const writer = fs.createWriteStream(filePath) response.data.pipe(writer) const hash = crypto.createHash('sha256') const progressTotal = parseInt(response.headers['content-length'], 10) let progressCurrent = 0 - console.log('\n') - response.data.on('data', (chunk) => { + response.data.on('data', chunk => { hash.update(chunk) progressCurrent += chunk.length - const progressBar = Math.round(progressCurrent / progressTotal * 100) - process.stdout.write(`\r${progressBar}% ${path}`) + process.stdout.write(`\r${name}: ${getProgressBar(progressCurrent, progressTotal)}`) }) await new Promise((resolve, reject) => { - response.data.on('end', () => { - resolve() - }) - response.data.on('error', (err) => { - reject(err) - }) + writer.on('finish', resolve) + writer.on('error', reject) + response.data.on('error', reject) }) - console.log(`\n${path} SHA256: ${hash.digest('hex')}`) + const hashValue = hash.digest('hex') + console.log(`\n✅ ${name} SHA256: ${hashValue}`) - await fs.remove(path) - console.log(`Deleted ${path}`) + // Write hash to a file for reference + await fs.writeFile(`${filePath}.sha256`, hashValue) + console.log(`Hash saved to ${filePath}.sha256`) + + if (process.argv.includes('--keep-files')) { + console.log(`Keeping file: ${filePath}`) + } else { + await fs.remove(filePath) + console.log(`Deleted: ${filePath}`) + } + + return { name, hash: hashValue } } catch (err) { - console.error(`\nError downloading ${url}: ${err.message}`) + console.error(`\n❌ Error processing ${name}: ${err.message}`) + throw err } } -downloadAndHash(x64URL, x64Path).then(() => { - downloadAndHash(arm64URL, arm64Path) -}).catch(() => { - downloadAndHash(arm64URL, arm64Path) -}) +/** + * Main function + */ +async function main() { + console.log(`Generating SHA256 hashes for PicList v${version}`) + console.log(`Download directory: ${DOWNLOAD_DIR}`) + + try { + // Ensure download directory exists + await fs.ensureDir(DOWNLOAD_DIR) + + // Start all downloads concurrently + const results = await Promise.allSettled(files.map(downloadAndHash)) + + // Output summary + console.log('\n===== SUMMARY =====') + results.forEach((result, index) => { + const fileName = files[index].name + if (result.status === 'fulfilled') { + console.log(`✅ ${fileName}: ${result.value.hash}`) + } else { + console.log(`❌ ${fileName}: Failed - ${result.reason.message}`) + } + }) + + // Check if all downloads were successful + if (results.some(r => r.status === 'rejected')) { + process.exit(1) + } + } catch (err) { + console.error(`\nCritical error: ${err.message}`) + process.exit(1) + } +} + +main() diff --git a/scripts/notarize.js b/scripts/notarize.js index 3aebfb2..3435af2 100644 --- a/scripts/notarize.js +++ b/scripts/notarize.js @@ -3,14 +3,9 @@ require('dotenv').config() const { notarize } = require('@electron/notarize') -const { - ELECTRON_SKIP_NOTARIZATION, - XCODE_APP_LOADER_EMAIL, - XCODE_APP_LOADER_PASSWORD, - XCODE_TEAM_ID -} = process.env +const { ELECTRON_SKIP_NOTARIZATION, XCODE_APP_LOADER_EMAIL, XCODE_APP_LOADER_PASSWORD, XCODE_TEAM_ID } = process.env -async function main (context) { +async function main(context) { const { electronPlatformName, appOutDir } = context if ( diff --git a/scripts/upload-beta.js b/scripts/upload-beta.js index 86402d1..203fe3d 100644 --- a/scripts/upload-beta.js +++ b/scripts/upload-beta.js @@ -45,7 +45,7 @@ const uploadFile = async () => { } } }) - parallelUploads3.on('httpUploadProgress', (progress) => { + parallelUploads3.on('httpUploadProgress', progress => { const progressBar = Math.round((progress.loaded / progress.total) * 100) process.stdout.write(`\r${progressBar}% ${fileName}`) })