Skip to content

Commit

Permalink
feat: 增加服务子进程
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Dec 15, 2023
1 parent 1985f79 commit 33b4dc4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 22 deletions.
4 changes: 3 additions & 1 deletion packages/desktop/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# port 端口号
VITE_API_URL = http://localhost:9190/
VITE_WEB_URL = http://localhost:9191/
VITE_WEB_URL = http://localhost:9191/

PORT=9190
4 changes: 4 additions & 0 deletions packages/desktop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @pear-rec/desktop

## 1.3.5

feat: 增加服务子进程

## 1.3.4

feat: 重置设置、显示快捷键
Expand Down
8 changes: 3 additions & 5 deletions packages/desktop/electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
appId: 'com.electron.pear-rec',
productName: 'pear-rec',
copyright: 'Copyright © 2023 ${author}',
asar: false,
// asar: true,
// extraResources: ['node_modules/sql.js', 'node_modules/typeorm'],
// asarUnpack: ['node_modules/sql.js', 'node_modules/typeorm'],
// asar: false,
asar: true,
directories: {
output: 'release/${version}',
},
files: ['dist-electron', 'dist'],
files: ['dist-electron', 'dist', '.env'],
mac: {
icon: 'build/mac/icon.icns',
artifactName: '${productName}-Mac-${version}-Installer.${ext}',
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/electron/main/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const VITE_API_URL = import.meta.env.VITE_API_URL;
// export const WEB_URL = 'http://127.0.0.1:9191/';
// export const VITE_API_URL = 'http://127.0.0.1:9190/';
export const preload = path.join(__dirname, '../preload/index.js');
export const serverPath = path.join(__dirname, '../server/mian.js');
export const serverPath = path.join(__dirname, '../server/main.js');
export const DIST_ELECTRON = path.join(__dirname, '../');
export const DIST = path.join(DIST_ELECTRON, '../dist');

Expand Down
12 changes: 8 additions & 4 deletions packages/desktop/electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { app, BrowserWindow, shell, ipcMain } from 'electron';
import { app, BrowserWindow, shell, utilityProcess } from 'electron';
import { release } from 'node:os';
import { spawn } from 'node:child_process';
import * as mainWin from '../win/mainWin';
import { initTray } from './tray';
import { update } from './update';
Expand All @@ -10,8 +9,13 @@ import { initConfig, getConfig } from '@pear-rec/server/src/config';
import { url, serverPath } from './contract';
import './ipcMain';

const appName = app.getPath('exe');
const serverAppProcess = url || spawn(appName, [serverPath]);
const serverAppProcess = utilityProcess.fork(serverPath);
serverAppProcess.stdout?.on('data', (data) => {
console.log(`serverAppProcess output: ${data}`);
});
serverAppProcess.stderr?.on('data', (data) => {
console.error(`serverAppProcess err: ${data}`);
});

initConfig();

Expand Down
9 changes: 4 additions & 5 deletions packages/desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pear-rec/desktop",
"version": "1.3.3",
"version": "1.3.4",
"main": "dist-electron/main/index.js",
"description": "pear-rec",
"author": "027xiguapi",
Expand All @@ -18,12 +18,11 @@
"preview": "vite preview",
"pree2e": "vite build --mode=test",
"e2e": "playwright test",
"release": "electron-builder -p always"
"release": "electron-builder -p always",
"server": "node dist-electron/server/main"
},
"dependencies": {
"electron-updater": "^6.1.1",
"sql.js": "^1.8.0",
"typeorm": "^0.3.17"
"electron-updater": "^6.1.1"
},
"devDependencies": {
"@pear-rec/server": "workspace:^",
Expand Down
14 changes: 11 additions & 3 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@pear-rec/server",
"version": "1.3.0",
"scripts": {
"build": "nest build",
"build": "nest build --webpack --webpackPath=./webpack.config.js",
"webpack": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand All @@ -16,11 +17,16 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/serve-static": "^4.0.0",
"@nestjs/typeorm": "^10.0.1",
"dayjs": "^1.11.10",
"http-proxy-middleware": "^2.0.6",
"https-proxy-agent": "^7.0.2",
"jsonfile": "^6.1.0",
"multer": "^1.4.5-lts.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"sql.js": "^1.9.0",
"typeorm": "^0.3.17",
"uuid": "^9.0.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand All @@ -32,9 +38,11 @@
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"copy-webpack-plugin": "^11.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
Expand All @@ -43,4 +51,4 @@
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
}
}
}
4 changes: 2 additions & 2 deletions packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ async function bootstrap() {
const configService = app.get(ConfigService);
const PORT = configService.get('PORT');

await app.listen(PORT);
await app.listen("9190");

console.log(`Server application is up and running on port ${PORT}`);
console.log(`Server application is up and running on port ${9190}`);
}
bootstrap();
1 change: 0 additions & 1 deletion packages/server/src/util/upload.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class UploadMiddleware implements MulterOptionsFactory {
return {
storage: diskStorage({
destination: async function (req, file, cb) {
console.log(1234);
const { type, userId } = req.body;
const config = getConfig();
const user = config.user;
Expand Down
67 changes: 67 additions & 0 deletions packages/server/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const webpack = require('webpack');
// fork-ts-checker-webpack-plugin需要单独安装
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
entry: './src/main',
target: 'node',
// 置为空即可忽略webpack-node-externals插件
externals: {},
module: {
noParse: /sql.js/,
rules: [
{
test: /\.ts?$/,
use: {
loader: 'ts-loader',
options: { transpileOnly: true },
},
exclude: /node_modules/,
},
],
},
// 打包后的文件名称以及位置
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.js', '.ts', '.json'],
},
plugins: [
// 需要进行忽略的插件
new webpack.IgnorePlugin({
checkResource(resource) {
const lazyImports = [
'@fastify/static',
'@nestjs/microservices',
'@nestjs/microservices/microservices-module',
'@nestjs/websockets/socket-module',
'cache-manager',
'class-validator',
'class-transformer'
];
if (!lazyImports.includes(resource)) {
return false;
}
try {
require.resolve(resource, {
paths: [process.cwd()],
});
} catch (err) {
return true;
}
return false;
},
}),
new ForkTsCheckerWebpackPlugin(),
new CopyPlugin({
patterns: [{
from: './node_modules/sql.js/dist/sql-wasm.wasm'
}]
}),
],
};

0 comments on commit 33b4dc4

Please sign in to comment.