From b1f5cae663d53e978cca538e6ea5cc8ad6fc8272 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 4 Mar 2023 06:37:03 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.docker | 2 +- app/Http/Controllers/IndexController.php | 9 ++- bin/auto | 15 ++++ bin/run | 89 ------------------------ cmd | 27 ++++--- docker/php/php.conf | 8 +-- vite.config.js | 18 +++++ 7 files changed, 57 insertions(+), 111 deletions(-) create mode 100755 bin/auto delete mode 100755 bin/run diff --git a/.env.docker b/.env.docker index 1ce4ffe06..af3a54dea 100644 --- a/.env.docker +++ b/.env.docker @@ -1,7 +1,7 @@ APP_NAME=DooTask APP_ENV=local APP_KEY= -APP_DEBUG=true +APP_DEBUG=false APP_SCHEME=auto APP_URL=http://localhost diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 23993ddcd..a4c2ad6e5 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -52,11 +52,14 @@ class IndexController extends InvokeController */ public function main() { - if (config('app.debug')) { + $hotFile = public_path('hot'); + $manifestFile = public_path('manifest.json'); + if (file_exists($hotFile)) { + $array = Base::json2array(file_get_contents($hotFile)); $style = null; - $script = preg_replace("/^(\/\/(.*?))(:\d+)?\//i", "$1:" . env("APP_DEV_PORT") . "/", asset_main("resources/assets/js/app.js")); + $script = preg_replace("/^(\/\/(.*?))(:\d+)?\//i", "$1:" . $array['APP_DEV_PORT'] . "/", asset_main("resources/assets/js/app.js")); } else { - $array = Base::json2array(file_get_contents(public_path('manifest.json'))); + $array = Base::json2array(file_get_contents($manifestFile)); $style = asset_main($array['resources/assets/js/app.js']['css'][0]); $script = asset_main($array['resources/assets/js/app.js']['file']); } diff --git a/bin/auto b/bin/auto new file mode 100755 index 000000000..e0d3ddbc4 --- /dev/null +++ b/bin/auto @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +if [ ! -f ".env" ]; then + echo -e "配置文件不存在!" + exit 1 +fi + +debug=`cat .env | grep "^APP_DEBUG=" | awk -F '=' '{print $2}'` +if [ "$debug" = "true" ]; then + echo "[MODE] development" + ./bin/inotify ./app +else + echo "[MODE] production" + php bin/laravels start -i +fi diff --git a/bin/run b/bin/run deleted file mode 100755 index 47b05addd..000000000 --- a/bin/run +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env php - $val) { - $envContent = preg_replace("/^" . $key . "\s*=\s*(.*?)$/m", $key . "=" . $val, $envContent); - } - file_put_contents($envPath, $envContent); - return true; - } - - function modifyMode($type) - { - $filePath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '/docker/php/php.conf'; - if (!file_exists($filePath)) { - return false; - } - $envContent = file_get_contents($filePath); - $envContent = str_replace("#command=php bin/laravels start -i", "command=php bin/laravels start -i", $envContent); - $envContent = str_replace("#command=./bin/inotify ./app", "command=./bin/inotify ./app", $envContent); - if ($type == "dev") { - $envContent = str_replace("command=php bin/laravels start -i", "#command=php bin/laravels start -i", $envContent); - $this->modifyEnv([ - 'APP_DEBUG' => 'true' - ]); - } else { - $envContent = str_replace("command=./bin/inotify ./app", "#command=./bin/inotify ./app", $envContent); - $this->modifyEnv([ - 'APP_DEBUG' => 'false' - ]); - } - file_put_contents($filePath, $envContent); - return true; - } -} - -$array = getopt('', ['port:', 'mode:']); -$loader = new runLoader(); - -if (isset($array['mode'])) { - $loader->modifyMode($array['mode']); -} - -$data = []; -if (isset($array['port'])) { - $data['APP_PORT'] = $array['port']; -} -if ($data) { - $loader->modifyEnv($data); -} diff --git a/cmd b/cmd index 9e0f6f082..311b50083 100755 --- a/cmd +++ b/cmd @@ -55,6 +55,17 @@ restart_php() { fi } +switch_debug() { + local debug="false" + if [[ "$1" == "true" ]] || [[ "$1" == "dev" ]] || [[ "$1" == "open" ]]; then + debug="true" + fi + if [[ "$(env_get APP_DEBUG)" != "$debug" ]]; then + env_set APP_DEBUG "$debug" + restart_php + fi +} + check_docker() { docker --version &> /dev/null if [ $? -ne 0 ]; then @@ -99,8 +110,7 @@ run_compile() { echo "" > ./index.html env_set APP_DEV_PORT $(rand 20001 30000) fi - run_exec php "php bin/run --mode=$type" - restart_php + switch_debug "$type" # if [ "$type" = "prod" ]; then rm -rf "./public/js/build" @@ -130,8 +140,7 @@ run_electron() { fi # if [ "$argv" == "dev" ]; then - run_exec php "php bin/run --mode=$argv" - restart_php + switch_debug "$argv" else mkdir -p ./electron/public cp ./electron/index.html ./electron/public/index.html @@ -288,7 +297,7 @@ if [ $# -gt 0 ]; then exit 1 fi [[ -z "$(env_get APP_KEY)" ]] && run_exec php "php artisan key:generate" - run_exec php "php bin/run --mode=prod" + switch_debug "false" # 检查数据库 remaining=20 while [ ! -f "${cur_path}/docker/mysql/data/$(env_get DB_DATABASE)/db.opt" ]; do @@ -384,12 +393,8 @@ if [ $# -gt 0 ]; then docker run -it --rm -v ${cur_path}:/home/node/apidoc kuaifan/apidoc -i app/Http/Controllers/Api -o public/docs elif [[ "$1" == "debug" ]]; then shift 1 - if [[ "$@" == "close" ]]; then - env_set APP_DEBUG "false" - else - env_set APP_DEBUG "true" - fi - restart_php + switch_debug "$@" + echo "success" elif [[ "$1" == "https" ]]; then shift 1 if [[ "$@" == "auto" ]]; then diff --git a/docker/php/php.conf b/docker/php/php.conf index 14a4861a6..ebeccaaea 100644 --- a/docker/php/php.conf +++ b/docker/php/php.conf @@ -1,12 +1,6 @@ [program:php] directory=/var/www - -# 生产环境 -command=php bin/laravels start -i - -# 开发环境 -#command=./bin/inotify ./app - +command=./bin/auto numprocs=1 autostart=true autorestart=true diff --git a/vite.config.js b/vite.config.js index d43aeb8d2..cd14d4648 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,4 +1,5 @@ import {resolve} from "path"; +import {writeFileSync, existsSync, unlinkSync} from "fs"; import {spawnSync} from "child_process"; import {defineConfig, loadEnv} from 'vite' import {createVuePlugin} from 'vite-plugin-vue2'; @@ -20,6 +21,23 @@ export default defineConfig(({command, mode}) => { const host = "0.0.0.0" const port = parseInt(env['APP_DEV_PORT']) + if (command === 'serve') { + const hotFile = resolve(__dirname, 'public/hot') + const hotClean = (exit) => { + if (existsSync(hotFile)) { + unlinkSync(hotFile); + } + if (exit) { + process.exit() + } + } + hotClean(false) + writeFileSync(hotFile, JSON.stringify(env)); + process.on('exit', () => hotClean(true)); + process.on('SIGINT', () => hotClean(true)); + process.on('SIGHUP', () => hotClean(true)); + } + return { base: basePath, publicDir: publicPath,