From 41f8a5dca182afe0e3e8e341af84ccc63bc8516a Mon Sep 17 00:00:00 2001 From: Kuingsmile <96409857+Kuingsmile@users.noreply.github.com> Date: Thu, 15 Jan 2026 22:15:53 +0800 Subject: [PATCH] :package: Chore(custom): add prepare scripts and update action --- .github/workflows/buid_arch.yml | 32 +++++----- package.json | 3 + scripts/prepare.js | 100 ++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 17 deletions(-) create mode 100644 scripts/prepare.js diff --git a/.github/workflows/buid_arch.yml b/.github/workflows/buid_arch.yml index 69e896c0..a4cdfcbb 100644 --- a/.github/workflows/buid_arch.yml +++ b/.github/workflows/buid_arch.yml @@ -152,6 +152,21 @@ jobs: yarn config set ignore-engines true rm -rf node_modules dist_electron && yarn install --frozen-lockfile yarn global add xvfb-maybe + if [[ "${{ matrix.format }}" == "zip" || "${{ matrix.format }}" == "7z" ]]; then + echo "Target format is ${{ matrix.format }}, downloading all resources..." + yarn run prepare + else + echo "Target format is other, downloading themes only..." + yarn run prepare:themes + fi + echo "Checking resources directory:" + ls -alh ./resources/theme || echo "Theme directory not found" + # check 7za.exe and theme dir + if [[ -f "./resources/7za.exe" ]]; then + echo "✅ 7za.exe exists" + else + echo "❌ 7za.exe does not exist" + fi - name: Generate release notes if: github.event.inputs.build_os == matrix.filter || github.event.inputs.build_os == 'All' @@ -183,27 +198,10 @@ jobs: fi echo "Publishing argument: $PUBLISH_ARG" if [[ "${{ matrix.os }}" == windows* ]]; then - if [[ "${{ matrix.format }}" == "nsis" ]]; then - rm -f ./resources/7za.exe - if [ -f ./resources/7za.exe ]; then - echo "Error: 7za.exe was not removed from resources." - exit 1 - fi - fi yarn run build:win ${{ matrix.format}} --${{ matrix.arch }} --publish $PUBLISH_ARG elif [[ "${{ matrix.os }}" == macos* ]]; then - rm -f ./resources/7za.exe - if [ -f ./resources/7za.exe ]; then - echo "Error: 7za.exe was not removed from resources." - exit 1 - fi yarn run build:mac ${{ matrix.format}} --${{ matrix.arch }} --publish $PUBLISH_ARG elif [[ "${{ matrix.os }}" == ubuntu* ]]; then - rm -f ./resources/7za.exe - if [ -f ./resources/7za.exe ]; then - echo "Error: 7za.exe was not removed from resources." - exit 1 - fi yarn run build:linux ${{ matrix.format}} --${{ matrix.arch }} --publish $PUBLISH_ARG else echo "Unsupported OS: ${{ matrix.os }}" diff --git a/package.json b/package.json index 4f02efba..e490ec84 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,9 @@ "postinstall": "electron-builder install-app-deps", "postuninstall": "electron-builder install-app-deps", "prebuild": "electron-vite build", + "prepare": "node ./scripts/prepare.js --all", + "prepare:7zaip": "node ./scripts/prepare.js --type=7zip", + "prepare:themes": "node ./scripts/prepare.js --type=themes", "preview": "electron-vite preview", "release": "electron-vite build && electron-builder", "sha256": "node ./scripts/gen-sha256.js", diff --git a/scripts/prepare.js b/scripts/prepare.js new file mode 100644 index 00000000..b69dc5a6 --- /dev/null +++ b/scripts/prepare.js @@ -0,0 +1,100 @@ +import { execSync } from 'node:child_process' +import fs from 'fs-extra' +import path from 'node:path' +import axios from 'axios' +import AdmZip from 'adm-zip' + +const cwd = process.cwd() +let arch = process.arch +const platform = process.platform + +const args = process.argv.slice(2) +const params = Object.fromEntries( + args.map(arg => { + const [key, value] = arg.replace(/^--/, '').split('=') + return [key, value || true] + }), +) + +const resolve7zip = () => + resolveResource({ + file: '7za.exe', + downloadURL: `https://github.com/develar/7zip-bin/raw/master/win/${arch}/7za.exe`, + }) + +async function fetchThemes() { + try { + const zipUrl = 'https://github.com/Kuingsmile/piclist-themeHub/releases/download/latest/themes.zip' + const zipData = await axios.get(zipUrl, { + responseType: 'arraybuffer', + headers: { 'Content-Type': 'application/octet-stream' }, + }) + const zip = new AdmZip(zipData.data) + zip.extractAllTo(path.join(cwd, 'resources', 'theme'), true) + console.log('[INFO]: themes downloaded and extracted') + return true + } catch (e) { + console.log('[WARN]: failed to download themes', e) + return false + } +} + +async function downloadFile(url, path) { + const response = await fetch(url, { + method: 'GET', + headers: { 'Content-Type': 'application/octet-stream' }, + }) + const buffer = await response.arrayBuffer() + fs.writeFileSync(path, new Uint8Array(buffer)) + + console.log(`[INFO]: download finished "${url}"`) +} + +async function resolveResource(binInfo) { + const { file, downloadURL, needExecutable = false } = binInfo + + const resDir = path.join(cwd, 'resources') + const targetPath = path.join(resDir, file) + + if (fs.existsSync(targetPath)) { + fs.removeSync(targetPath) + } + + fs.mkdirSync(resDir, { recursive: true }) + await downloadFile(downloadURL, targetPath) + + if (needExecutable && platform !== 'win32') { + execSync(`chmod 755 ${targetPath}`) + console.log(`[INFO]: ${file} chmod finished`) + } + + console.log(`[INFO]: ${file} finished`) +} + +async function main() { + const tasks = [] + + if (params.type === '7zip' || params.all) { + console.log('[INFO]: Resolving 7zip...') + tasks.push(resolve7zip()) + } + + if (params.type === 'themes' || params.all) { + console.log('[INFO]: Resolving themes...') + tasks.push(fetchThemes()) + } + + if (tasks.length === 0) { + console.log('请提供参数,例如: node script.js --type=7zip 或 --all') + return + } + + try { + await Promise.all(tasks) + console.log('Selected resources have been resolved.') + } catch (err) { + console.error('Error:', err) + } +} + +main()