Skip to content

Commit

Permalink
Merge pull request #143 from umbrella22/opt_dep
Browse files Browse the repository at this point in the history
Opt dep
  • Loading branch information
umbrella22 authored Nov 29, 2024
2 parents 6ca8f1c + f8fe9ba commit ec77029
Show file tree
Hide file tree
Showing 12 changed files with 7,995 additions and 6,852 deletions.
1 change: 1 addition & 0 deletions .electron-vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import rollupOptions from "./rollup.config";
import { errorLog, doneLog } from "./log";
import { getArgv } from "./utils";

const { clean = false, target = "client" } = getArgv();
const mainOpt = rollupOptions(process.env.NODE_ENV, "main");
const preloadOpt = rollupOptions(process.env.NODE_ENV, "preload");

Expand Down
123 changes: 73 additions & 50 deletions .electron-vite/hot-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,126 @@
* power by biuuu
*/

import chalk from 'chalk'
import { join } from 'path'
import { ensureDir, emptyDir, copy, outputJSON, remove, stat, readFile } from 'fs-extra'
import { createHmac } from 'crypto'
import { platform } from 'os'
import AdmZip from 'adm-zip'
import packageFile from '../package.json'
import buildConfig from '../build.json'
import config from '../config'
import { okayLog, errorLog, doneLog } from './log'
import chalk from "chalk";
import { join } from "path";
import {
ensureDir,
emptyDir,
copy,
outputJSON,
remove,
stat,
readFile,
} from "fs-extra";
import { BinaryLike, createHmac } from "crypto";
import AdmZip from "adm-zip";
import packageFile from "../package.json";
import buildConfig from "../build.json";
import config from "../config";
import { okayLog, errorLog, doneLog } from "./log";

const buildPath = join(".", "dist", "electron");

const platformName = platform().includes('win32') ? 'win' : platform().includes('darwin') ? 'mac' : 'linux'
const buildPath = join('.', 'build', `${platformName === 'mac' ? 'mac' : platformName + '-unpacked'}`)

const hash = (data, type = 'sha256') => {
const hmac = createHmac(type, 'Sky')
hmac.update(data)
return hmac.digest('hex')
}
const hash = (data: BinaryLike, type = "sha256") => {
const hmac = createHmac(type, "Sky");
hmac.update(data);
return hmac.digest("hex");
};

const createZip = (filePath: string, dest: string) => {
const zip = AdmZip()
zip.addLocalFolder(filePath, "", null)
zip.toBuffer()
zip.writeZip(dest, null)
}
const zip = new AdmZip();
zip.addLocalFolder(filePath, "");
zip.toBuffer();
zip.writeZip(dest);
};

const start = async () => {
console.log(chalk.green.bold(`Start packing \n`))
console.log(chalk.green.bold(`Start packing \n`));

if (buildConfig.asar) {
errorLog(`${chalk.red("Please make sure the build.asar option in the Package.json file is set to false")}\n`)
errorLog(
`${chalk.red(
"Please make sure the build.asar option in the Package.json file is set to false"
)}\n`
);
return;
}

if (config.build.hotPublishConfigName === '') {
errorLog(`${chalk.red("HotPublishConfigName is not set, which will cause the update to fail, please set it in the config/index.js \n") + chalk.red.bold(`\n Packing failed \n`)}`)
process.exit(1)
if (config.build.hotPublishConfigName === "") {
errorLog(
`${
chalk.red(
"HotPublishConfigName is not set, which will cause the update to fail, please set it in the config/index.js \n"
) + chalk.red.bold(`\n Packing failed \n`)
}`
);
process.exit(1);
}

stat(join(buildPath, 'resources', 'app'), async (err, stats) => {
stat(join(buildPath, "main"), async (err, stats) => {
if (err) {
errorLog(`${chalk.red("No resource files were found, please execute this command after the build command")}\n`)
errorLog(
`${chalk.red(
"No resource files were found, please execute this command after the build command"
)}\n`
);
return;
}

try {
console.log(chalk.green.bold(`Check the resource files \n`))
const packResourcesPath = join('.', 'build', 'resources', 'dist');
const packPackagePath = join('.', 'build', 'resources');
const resourcesPath = join('.', 'dist');
const appPath = join('.', 'build', 'resources');
console.log(chalk.green.bold(`Check the resource files \n`));
const packResourcesPath = join(".", "build", "resources", "dist");
const packPackagePath = join(".", "build", "resources");
const resourcesPath = join(".", "dist");
const appPath = join(".", "build", "resources");
const name = "app.zip";
const outputPath = join('.', 'build', 'update');
const outputPath = join(".", "build", "update");
const zipPath = join(outputPath, name);


await ensureDir(packResourcesPath);
await emptyDir(packResourcesPath);
await copy(resourcesPath, packResourcesPath);
okayLog(chalk.cyan.bold(`File copy complete \n`))
okayLog(chalk.cyan.bold(`File copy complete \n`));
await outputJSON(join(packPackagePath, "package.json"), {
name: packageFile.name,
productName: buildConfig.productName,
version: packageFile.version,
description: packageFile.description,
main: packageFile.main,
author: packageFile.author,
dependencies: packageFile.dependencies
dependencies: packageFile.dependencies,
});
okayLog(chalk.cyan.bold(`Rewrite package file complete \n`))
okayLog(chalk.cyan.bold(`Rewrite package file complete \n`));
await ensureDir(outputPath);
await emptyDir(outputPath);
createZip(appPath, zipPath);
const buffer = await readFile(zipPath);
const sha256 = hash(buffer);
const hashName = sha256.slice(7, 12);
await copy(zipPath, join(outputPath, `${hashName}.zip`));
await outputJSON(join(outputPath, `${config.build.hotPublishConfigName}.json`),
await outputJSON(
join(outputPath, `${config.build.hotPublishConfigName}.json`),
{
version: packageFile.version,
name: `${hashName}.zip`,
hash: sha256
hash: sha256,
}
);
okayLog(chalk.cyan.bold(`Zip file complete, Start cleaning up redundant files \n`))
okayLog(
chalk.cyan.bold(
`Zip file complete, Start cleaning up redundant files \n`
)
);
await remove(zipPath);
await remove(appPath)
okayLog(chalk.cyan.bold(`Cleaning up redundant files completed \n`))
doneLog('The resource file is packaged!\n')
await remove(appPath);
okayLog(chalk.cyan.bold(`Cleaning up redundant files completed \n`));
doneLog("The resource file is packaged!\n");
console.log("File location: " + chalk.green(outputPath) + "\n");
} catch (error) {
errorLog(`${chalk.red(error.message || error)}\n`)
process.exit(1)
errorLog(`${chalk.red(error.message || error)}\n`);
process.exit(1);
}
});
}
};

start()
start();
1 change: 1 addition & 0 deletions .electron-vite/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineConfig({
"@store": join(root, "/store/modules"),
},
},

base: "./",
build: {
outDir:
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"npm.packageManager": "npm",
"eslint.validate": [
"javascript",
"javascriptreact",
Expand Down
Loading

0 comments on commit ec77029

Please sign in to comment.