Skip to content

Commit

Permalink
perf: ⚡️ version 版本自动化
Browse files Browse the repository at this point in the history
Closes: #156
  • Loading branch information
meetqy committed Jun 2, 2023
1 parent f4d2b8c commit 8d1f959
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
5 changes: 5 additions & 0 deletions apps/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ app.on("ready", () => {
process.env["ASSETS_PORT"] = _assets_port;

if (app.isPackaged) {
// app config production
// dev 在 watchDesktop.ts 中指定
process.env["APP_VERSION"] = app.getVersion();
process.env["APP_NAME"] = app.getName();

// Create sqlite database file
createSqlite(join(process.resourcesPath, "./packages/db/prisma/db.sqlite"));

Expand Down
1 change: 1 addition & 0 deletions apps/electron/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@acme/electron",
"productName": "Rao Pics",
"version": "0.1.0",
"private": true,
"main": "main/dist/index.cjs",
Expand Down
2 changes: 2 additions & 0 deletions apps/electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ process.once("loaded", () => {
ip: process.env["IP"],
web_port: process.env["WEB_PORT"],
assets_port: process.env["ASSETS_PORT"],
name: process.env["APP_NAME"],
version: process.env["APP_VERSION"],
});
// If you expose something here, you get window.something in the React app
// type it in types/exposedInMainWorld.d.ts to add it to the window type
Expand Down
1 change: 0 additions & 1 deletion apps/electron/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rao Pics</title>
</head>
<body>
<div id="root"></div>
Expand Down
6 changes: 3 additions & 3 deletions apps/electron/renderer/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Home() {
const utils = trpc.useContext();
const isInit = useRef<boolean>(false);
const library = trpc.library.get.useQuery();
const { ip, web_port, assets_port } = window.electronENV;
const { ip, web_port, assets_port, name, version } = window.electronENV;
const config = trpc.config.update.useMutation();

useEffect(() => {
Expand Down Expand Up @@ -274,8 +274,8 @@ function Home() {
</figure>
<div className="card-body items-center text-center">
<h2 className="card-title uppercase !mb-0">
rao.pics
<button className="btn btn-sm btn-link hover:no-underline no-underline p-0 text-secondary normal-case relative -top-2 -left-1">v0.5.0-beta.2</button>
{name}
<button className="btn btn-sm btn-link hover:no-underline no-underline p-0 text-secondary normal-case relative -top-2 -left-1">v{version}</button>
</h2>
<p className="text-base-content/90 ">~~暂未添加文件夹,请点击下面按钮~~</p>
<div className="card-actions mt-2">
Expand Down
13 changes: 8 additions & 5 deletions apps/electron/scripts/compile.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import builder from "electron-builder";

import { config } from "./config.js";

const { name, version } = config;
const extraResources: builder.FileSet[] = [];

export const Config: builder.Configuration = {
productName: "Rao Pics",
export const AppConfig: builder.Configuration = {
productName: name,
copyright: `Copyright © 2022-${new Date().getFullYear()} meetqy`,
mac: {
category: "public.app-category.photography",
icon: "buildResources/icon.icns",
darkModeSupport: true,
target: {
target: "dmg",
arch: ["x64", "arm64"],
// arch: ["x64", "arm64"],
},
extraResources: extraResources,
},
extraMetadata: { version: "0.5.0-beta.2" },
extraMetadata: { version },
directories: {
output: "dist",
buildResources: "buildResources",
Expand Down Expand Up @@ -52,7 +55,7 @@ export const Config: builder.Configuration = {

builder
.build({
config: Config,
config: AppConfig,
})
.then((result) => {
console.log(JSON.stringify(result));
Expand Down
16 changes: 16 additions & 0 deletions apps/electron/scripts/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from "fs";

const Pkg = JSON.parse(fs.readFileSync("../../package.json", "utf-8")) as Record<string, unknown>;

const toPascalCase = (str: string) =>
str
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");

const name = toPascalCase(Pkg.name as string);

export const config = {
name,
version: Pkg.version as string,
};
6 changes: 6 additions & 0 deletions apps/electron/scripts/watchDesktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { spawn, type ChildProcess } from "child_process";
import electronPath from "electron";
import { build, type ViteDevServer } from "vite";

import { config } from "./config.js";
import { listeningWebServer } from "./watchWeb.js";

const { name, version } = config;

process.env["APP_NAME"] = name;
process.env["APP_VERSION"] = version;

// process.env.MODE is used in various vite config files
const mode = (process.env.MODE = process.env.MODE ?? "development");

Expand Down
2 changes: 2 additions & 0 deletions apps/electron/types/exposedInMainWorld.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ interface Window {
ip: string;
web_port: string;
assets_port: string;
name: string;
version: string;
};
}

0 comments on commit 8d1f959

Please sign in to comment.