Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions electron/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -rf ../node_modules
14 changes: 8 additions & 6 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"start": "electron-forge start",
"dev": "EMBEDSERVER=1 electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"prebuild": "sh prebuild.sh",
"afterbuild": "sh clean.sh",
"make": "npm run prebuild && electron-forge make && npm run afterbuild",
"make-x86": "electron-forge make --arch=x64 --platform=darwin",
"publish": "electron-forge publish",
"lint": "eslint --ext .ts,.tsx ."
Expand All @@ -25,10 +27,10 @@
"packagerConfig": {
"extraResource": [
"../server/xwin-server-macos",
"../server/prisma",
"../server/node_modules/prisma/build",
"../server/node_modules/prisma/package.json",
"../server/node_modules/@prisma/engines/migration-engine-darwin-arm64"
"../server/prisma/schema.prisma",
"node_modules/prisma",
"node_modules/@prisma/engines/migration-engine-darwin-arm64",
"../node_modules"
],
"extendInfo": {
"LSUIElement": true
Expand Down Expand Up @@ -111,4 +113,4 @@
"react-highlight-words": "^0.18.0",
"react-select": "^5.4.0"
}
}
}
3 changes: 3 additions & 0 deletions electron/prebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mkdir -p ../node_modules/@prisma/engines
cp -r node_modules/@prisma/engines/dist ../node_modules/@prisma/engines/
cp node_modules/@prisma/engines/package.json ../node_modules/@prisma/engines/
46 changes: 36 additions & 10 deletions electron/src/DBManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path');

const isDebug = process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
const dbFileName = "dev.db";
// app.getPath() ~/git/xwin/electron in dev mode. production: resources/app/ xx some wepack fodler
const appDataPath = app.getPath("userData"); // e.g. '/Users/grimmer/Library/Application Support/Xwin'
const dbUsedVersionTxtPath = `${appDataPath}/dbUsedVersion.txt`
export const sqlitePathInProd = `${appDataPath}/${dbFileName}`
Expand Down Expand Up @@ -46,22 +47,19 @@ async function prisma(...args:any[]) {
// electron 的 exec 之前試是認不得 code path, 所以要用 /usr/local/bin/code (之前試是打包版會,沒打包版呢? 沒試)
// 但現在打包版 code path 又正常了 ????

// TODO: 找看看有無方法避免產生
/** prisma migrate 也會自動產生 typescript file in @prisma/client, 所以 prisma/client 也要安裝 */
const env = { ...process.env, PATH: `${process.env.PATH}:/usr/local/bin` };

// NOTE:
// 原本的 server process 可以跑 (exec/spawn 時會沒有預設 node path時)? 是因為它已經是 vercel 了

// https://stackoverflow.com/a/58291475/7354486
// https://stackoverflow.com/a/58291475/7354486 electron 執行 node.js script 缺 node.js 問題
// solution candidates
// 1. 看是要用 fork (上面 link 提到的可延用 node, 因為 spwan 會沒有預設 node process/path), <- 現在用這方法
// 2. 或是用vercel/pkg 打包 prisma cli (但要解決 pkg bundle/index.js 缺 package.json 問題)
// 2. 或是用 vercel/pkg 打包 prisma cli
// (但要解決 pkg bundle/index.js 缺 package.json 問題 <- 跟下面需要 node_modules/@prisma/engines/package.json
// 是類似問題,皆需 package.json,下面問題靠 prebuild.sh 解決)
// p.s. fork behavior (use node by default): node "/usr/local/bin/code /Users/grimmer/git",
// so pass a node.js script path is correct way
console.log("fork1");
const child = fork(command, args, { env}); // cwd: TOOLS_DIR,
console.log("fork2");
}

/** NOTE: only use for packaged app */
Expand All @@ -74,6 +72,10 @@ export class DBManager {
static prismaPath = ""
static migrateExePath = ""

static fmtExePath = ""
static queryExePath = ""
static introspectionExePath =""

static initPath() {

if (DBManager.serverFolderPath) {
Expand All @@ -95,7 +97,6 @@ export class DBManager {
db_url = path.resolve(`${process.cwd()}/../server/prisma/dev.db`);

// db_url = `${__dirname}/../server/prisma/dev.db`; // works but not good. dirname is some webpack main file location
// app.getPath() ~/git/xwin/electron in dev mode

// for testing
// DBManager.migrateExePath = `${DBManager.serverFolderPath}/migration-engine-darwin-arm64`;
Expand All @@ -106,12 +107,25 @@ export class DBManager {
// after resolve, no final /
DBManager.serverFolderPath = path.resolve(`${app.getAppPath()}/../`) //`${__dirname}/../../../`;
// DBManager.serverFolderPath = "/Users/grimmer/git/xwin/electron/out/XWin-darwin-arm64/XWin.app/Contents/Resources"
DBManager.schemaPath = `${DBManager.serverFolderPath}/prisma/schema.prisma`;
DBManager.schemaPath = `${DBManager.serverFolderPath}/schema.prisma`;
db_url = sqlitePathInProd;
DBManager.prismaPath = `${DBManager.serverFolderPath}/build/index.js`

/** mainly it requires two files
* 1. node_modules/prisma/build/index.js
* 2. node_modules/prisma/package.json
* with these structure ./index.js & ../package.json
* */
DBManager.prismaPath = `${DBManager.serverFolderPath}/prisma/build/index.js`

// DBManager.migrateExePath = `${DBManager.serverFolderPath}/node_modules/engines/dist/index.js`;
// DBManager.migrateExePath = `${DBManager.serverFolderPath}/node_modules/@prisma/engines/migration-engine-darwin-arm64`;
DBManager.migrateExePath = `${DBManager.serverFolderPath}/migration-engine-darwin-arm64`;

/** not copy and not really use them */
DBManager.introspectionExePath =`${DBManager.serverFolderPath}/@prisma/engines/introspection-engine-darwin-arm64`;
DBManager.fmtExePath = `${DBManager.serverFolderPath}/@prisma/engines/prisma-fmt-darwin-arm64`;
DBManager.queryExePath = `${DBManager.serverFolderPath}/@prisma/engines/libquery_engine-darwin-arm64.dylib.node`;

}
DBManager.databaseFilePath = db_url
}
Expand All @@ -125,7 +139,19 @@ export class DBManager {
// const PRISMA_MIGRATION_ENGINE_BINARY = `${common}migration-engine-darwin-arm64`;

if (DBManager.migrateExePath) {
/** For migration, it also requires
* 1. node_modules/@prisma/engines/dist !!!!!
* 2. node_modules/@prisma/engines/package.json
* node_modules (<- folder structure required) should not be in some folder deeper than resources/
* but if it is outside /resources, not suitable for packaging
*/

process.env.PRISMA_MIGRATION_ENGINE_BINARY = DBManager.migrateExePath;
process.env.PRISMA_INTROSPECTION_ENGINE_BINARY = "" // DBManager.introspectionExePath;
process.env.PRISMA_FMT_BINARY = "" // DBManager.fmtExePath;
process.env.PRISMA_QUERY_ENGINE_BINARY = "" // DBManager.queryExePath;
process.env.PRISMA_QUERY_ENGINE_LIBRARY = "" // DBManager.queryExePath;
// process.env.PRISMA_CLIENT_ENGINE_TYPE = "binary"
}

try {
Expand Down
4 changes: 2 additions & 2 deletions electron/src/TrayGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { nativeImage, BrowserWindow, Tray, Menu } from "electron";
import { app} from "electron";
import { DBManager, sqlitePathInProd} from "./DBManager";

const prismaPath = require.resolve('prisma')
// const prismaPath = require.resolve('prisma')

// ref:
// https://blog.logrocket.com/building-a-menu-bar-application-with-electron-and-react/
Expand Down Expand Up @@ -57,7 +57,7 @@ export class TrayGenerator {
const appPath = app.getAppPath();

const error = DBManager.migrateError;
let info= `db:${DBManager.databaseFilePath};schema:${DBManager.schemaPath};server:${ DBManager.serverFolderPath};prismaPath:${prismaPath};appPath:${appPath};info.${error}`;
let info= `db:${DBManager.databaseFilePath};schema:${DBManager.schemaPath};server:${ DBManager.serverFolderPath};prismaPath:${DBManager.prismaPath};appPath:${appPath};info.${error}`;

// DBManager.databaseURL: string = "";
// DBManager.schemaPath = ""
Expand Down