Explorar o código

build server into desktop app

Jeffrey Morgan hai 1 ano
pai
achega
d3709f85b5

+ 0 - 4
.gitignore

@@ -1,6 +1,2 @@
 .DS_Store
 .vscode
-*.spec
-*/build
-*/dist
-client/resources/server

+ 1 - 0
client/forge.config.ts

@@ -13,6 +13,7 @@ const config: ForgeConfig = {
   packagerConfig: {
     asar: true,
     icon: './images/icon',
+    extraResource: ['../server/dist/server'],
   },
   rebuildConfig: {},
   makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],

+ 0 - 24
client/permissions-plugin.ts

@@ -1,24 +0,0 @@
-import chmodr from 'chmodr'
-import * as path from 'path'
-
-interface PluginOptions {
-  resourcePath: string
-}
-
-class PermissionsPlugin {
-  options: PluginOptions
-
-  constructor(options: PluginOptions) {
-    this.options = options
-  }
-
-  apply(compiler: any) {
-    compiler.hooks.afterEmit.tap('PermissionsPlugin', () => {
-      chmodr(path.join(this.options.resourcePath), 0o755, err => {
-        // this fails on the first call to suppress the error
-      })
-    })
-  }
-}
-
-export default PermissionsPlugin

+ 1 - 1
client/src/app.tsx

@@ -1,6 +1,6 @@
 import { useState } from 'react'
 
-const API_URL = 'http://127.0.0.1:5000'
+const API_URL = 'http://127.0.0.1:5001'
 
 type Message = {
   sender: string

+ 17 - 9
client/src/index.ts

@@ -21,24 +21,32 @@ const createWindow = (): void => {
     minWidth: 400,
     minHeight: 300,
     titleBarStyle: 'hiddenInset',
-    // trafficLightPosition: { x: 20, y: 18 },
-    // vibrancy: 'titlebar',
     transparent: true,
   })
 
+  // and load the index.html of the app.
+  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
+}
+
+// if the app is packaged then run the server
+if (app.isPackaged) {
+  const resources = process.resourcesPath
+  console.log(resources)
+
   // Start the executable
-  let pyExecutable = path.join(__dirname, '../renderer/resources/server')
-  console.log(`Starting ${pyExecutable}`)
-  let pyProcess = spawn(pyExecutable)
-  pyProcess.stdout.on('data', data => {
+  const exec = path.join(resources, 'server')
+  console.log(`Starting ${exec}`)
+  const proc = spawn(exec)
+  proc.stdout.on('data', data => {
     console.log(`server: ${data}`)
   })
-  pyProcess.stderr.on('data', data => {
+  proc.stderr.on('data', data => {
     console.error(`server: ${data}`)
   })
 
-  // and load the index.html of the app.
-  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
+  process.on('exit', () => {
+    proc.kill()
+  })
 }
 
 // This method will be called when Electron has finished

+ 0 - 9
client/webpack.plugins.ts

@@ -1,19 +1,10 @@
 import type IForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
-import * as path from 'path'
-import PermissionsPlugin from './permissions-plugin'
 
 // eslint-disable-next-line @typescript-eslint/no-var-requires
 const ForkTsCheckerWebpackPlugin: typeof IForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
-const CopyWebpackPlugin = require('copy-webpack-plugin')
 
 export const plugins = [
   new ForkTsCheckerWebpackPlugin({
     logger: 'webpack-infrastructure',
   }),
-  new CopyWebpackPlugin({
-    patterns: [{ from: 'resources', to: 'resources' }],
-  }),
-  new PermissionsPlugin({
-    resourcePath: '.webpack/renderer/resources/server',
-  }),
 ]

+ 1 - 0
models/.gitignore

@@ -0,0 +1 @@
+*.bin

+ 5 - 0
server/.gitignore

@@ -0,0 +1,5 @@
+.env
+.venv
+*.spec
+build
+dist

+ 1 - 3
server/build.py

@@ -1,6 +1,5 @@
 import site
 import os
-import shutil
 from PyInstaller.__main__ import run as pyi_run
 
 # the llama_cpp directory is not included if not explicitly added
@@ -13,10 +12,9 @@ args = [
     site_packages_dir,
     "--add-data",
     f"{llama_cpp_dir}{os.pathsep}llama_cpp",
-    "--onefile",
+    "--onefile"
 ]
 
 # generate the .spec file and run PyInstaller
 pyi_run(args)
 
-shutil.copy2("dist/server", "../client/resources/server")

+ 1 - 1
server/server.py

@@ -76,4 +76,4 @@ def generate():
 
 
 if __name__ == "__main__":
-    app.run(debug=True, threaded=True, port=5000)
+    app.run(debug=True, threaded=True, port=5001)