Skip to content

Commit

Permalink
feat: Add esbuild for server middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 8, 2023
1 parent db42fc1 commit fff7b56
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 95 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"license": "GPL",
"scripts": {
"zhi-dev": "pnpm build --skip-cache && pnpm zhi-build",
"build": "nx run-many --target=build --exclude=zhi-docs",
"zhi-build": "nx zhi-build zhi-core",
"test": "nx run-many --target=test --exclude=zhi-docs",
Expand Down Expand Up @@ -35,7 +36,6 @@
"@nrwl/webpack": "15.9.2",
"@nrwl/workspace": "15.9.2",
"@nx-plus/docusaurus": "15.0.0-rc.0",
"@nx-plus/nuxt": "^14.1.0",
"@nxext/vue": "15.7.0-Beta.3",
"@nxlv/python": "^15.7.0",
"@nxtensions/astro": "3.5.0",
Expand Down Expand Up @@ -65,15 +65,18 @@
"cypress": "^12.2.0",
"docusaurus-plugin-typedoc": "^0.18.0",
"esbuild": "^0.17.5",
"esbuild-plugin-inline-import": "^1.0.1",
"eslint": "~8.15.0",
"eslint-config-prettier": "8.1.0",
"eslint-config-standard-with-typescript": "^34.0.1",
"eslint-plugin-cypress": "^2.10.3",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vue": "^9.10.0",
"fs-extra": "^11.1.1",
"jest": "^29.4.1",
"jest-environment-node": "^29.4.1",
"jsdom": "~21.1.1",
"minimist": "^1.2.8",
"nx": "15.9.2",
"prettier": "^2.8.7",
"stylelint": "^15.4.0",
Expand Down
5 changes: 1 addition & 4 deletions packages/zhi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,5 @@
"zhi-core",
"siyuan-note",
"theme"
],
"dependencies": {
"express": "^4.18.2"
}
]
}
7 changes: 7 additions & 0 deletions packages/zhi-core/script/zhi-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/sh
ZHI_THEME_DIR=/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/conf/appearance/themes/zhi

## copy dist
cp -r dist/packages/zhi/* $ZHI_THEME_DIR/
echo "zhi dist copy success"

## install dependency for zhi
cd $ZHI_THEME_DIR/ || exit
echo "installing zhi dependencies ..."
npm i express compression vite-plugin-ssr --registry=https://registry.npmmirror.com
echo "zhi dependency installed"
126 changes: 126 additions & 0 deletions packages/zhi-core/src/lib/core/browser-windows/WindowManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import ZhiUtil from "../util/ZhiUtil"

/**
* 窗口管理器
*
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
class WindowManager {
private readonly logger
private readonly common

constructor() {
this.logger = ZhiUtil.zhiLog("window-manager")
this.common = ZhiUtil.zhiCommon()
}

/**
* 打开新窗口
*
* 示例:
*
* ```
* ## development
* windowManager.openBrowserWindow("https://www.baidu.com", undefined, undefined, true, false)
* windowManager.openBrowserWindow("https://www.baidu.com", { "key1": "value1", "key2": "value2" }, undefined, true, false)
*
* ## production
* windowManager.openBrowserWindow("https://www.baidu.com")
* ```
*
* @param url - url
* @param params - 参数
* @param win - 父窗口
* @param isDev - 是否打开开发者工具
* @param modal - 是否模态
*/
public openBrowserWindow(url: string, params?: Record<string, string>, win?: any, isDev = false, modal = false) {
try {
if (this.common.strUtil.isEmptyString(url)) {
this.logger.error("Url cannot be empty")
return
}

if (!this.common.browserUtil.isElectron()) {
this.logger.info("BrowserWindow can ony be available in siyuan Electron environment")
return
}

if (params) {
Object.keys(params).forEach((key: string) => {
const value = params[key]
url = this.common.browserUtil.setUrlParameter(url, key, value)
})
}

this.logger.info(this.common.strUtil.f("Opening a new BrowserWindow from url => {0}", url))

const mainWin = win ?? this.common.siyuanUtil.siyuanWindow()
const { app, BrowserWindow, getCurrentWindow } = mainWin.require("@electron/remote")
const remote = mainWin.require("@electron/remote").require("@electron/remote/main")
const mainWindow = getCurrentWindow()
const newWindow = new BrowserWindow({
parent: mainWindow,
width: 900,
height: 750,
resizable: true,
modal: modal,
icon: this.common.siyuanUtil.joinPath(
this.common.siyuanUtil.siyuanWindow().siyuan.config.system.appDir,
"stage",
"icon-large.png"
),
titleBarOverlay: {
color: "#cccccca5",
symbolColor: "black",
},
webPreferences: {
nativeWindowOpen: true,
nodeIntegration: true,
webviewTag: true,
webSecurity: false,
contextIsolation: false,
},
})

newWindow.webContents.userAgent = `SiYuan/${app.getVersion()} https://b3log.org/siyuan Electron`
// 允许
remote.enable(newWindow.webContents)
if (isDev) {
newWindow.webContents.openDevTools()
}
newWindow.loadURL(url)
} catch (e) {
this.logger.error("Open browser window failed", e)
}
}
}

export default WindowManager
60 changes: 60 additions & 0 deletions packages/zhi-core/src/lib/core/browser-windows/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import DependencyItem from "../../models/DependencyItem"
import ZhiUtil from "../util/ZhiUtil"
import WindowManager from "./WindowManager"

/**
* 这里统一挂载一个方法,可以打开 Electron 的 BrowserWindow
*/
class ZhiBrowserWindow {
private readonly logger
private readonly common

private windowManager

constructor() {
this.logger = ZhiUtil.zhiLog("zhi-browser-window")
this.common = ZhiUtil.zhiCommon()

this.windowManager = new WindowManager()
}

/**
* 挂载 BrowserWindow
*
* @author terwer
* @since 1.0.0
*/
public async initBrowserWindow(): Promise<DependencyItem[]> {
this.logger.info("Init windowManager")
this.common.siyuanUtil.siyuanWindow().windowManager = this.windowManager
this.logger.info("WindowManager inited", this.common.siyuanUtil.siyuanWindow().windowManager)
return Promise.resolve([])
}
}

export default ZhiBrowserWindow
102 changes: 102 additions & 0 deletions packages/zhi-core/src/lib/core/cmd/customCmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

// import child_process from "child_process";
//
// const { exec } = require('child_process');
//
// function runCommand(command) {
// return new Promise((resolve, reject) => {
// exec(command, (error, stdout, stderr) => {
// if (error) {
// reject(error);
// } else {
// resolve(stdout.trim());
// }
// });
// });
// }
//
// // 调用示例:执行 ls 命令,并输出其结果
// runCommand('/Users/terwer/Downloads/n/node_modules/.bin/next -v').then((result) => {
// console.log(result);
// }).catch((error) => {
// console.error(error);
// });
//
// class CustomCmd {
// init(){
// const { spawn } = require('child_process');
// const path = require('path');
//
// async function executeCommand(command, args = [], options = {}) {
// return new Promise((resolve, reject) => {
// const childProcess = spawn(command, args, options);
//
// let stdout = '';
// let stderr = '';
//
// childProcess.stdout.on('data', (data) => {
// stdout += data.toString();
// });
//
// childProcess.stderr.on('data', (data) => {
// stderr += data.toString();
// });
//
// childProcess.on('close', (code) => {
// if (code === 0) {
// resolve(stdout);
// } else {
// reject(new Error(stderr));
// }
// });
//
// childProcess.on('error', (error) => {
// reject(error);
// });
// });
// }
//
// const baseDir = "/Users/terwer/Downloads/n"
// const pathToBinary = path.join(baseDir, "node_modules", ".bin", "next")
// const args = ['-v'];
// const options = {
// env: {
// ELECTRON_RUN_AS_NODE: 1,
// },
// };
//
// executeCommand(pathToBinary, args, options)
// .then((stdout) => {
// console.log(`Command output: ${stdout}`);
// })
// .catch((error) => {
// console.error(`Command failed: ${error.message}`);
// });
// }
// }


25 changes: 25 additions & 0 deletions packages/zhi-core/src/lib/core/cmd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

Loading

0 comments on commit fff7b56

Please sign in to comment.