perf: 优化开发执行脚本

This commit is contained in:
kuaifan 2023-03-04 06:37:03 +08:00
parent 112b70dcad
commit b1f5cae663
7 changed files with 57 additions and 111 deletions

View File

@ -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

View File

@ -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']);
}

15
bin/auto Executable file
View File

@ -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

89
bin/run
View File

@ -1,89 +0,0 @@
#!/usr/bin/env php
<?php
/**
* Class runLoader
*/
class runLoader
{
public function randString($length = 16)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$passwordstr = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$passwordstr .= $chars[mt_rand(0, $max)];
}
return $passwordstr;
}
function getEnv(string $key)
{
if (empty($key) || !is_string($key)) {
return '';
}
$envPath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '.env';
if (!file_exists($envPath)) {
return false;
}
$envContent = file_get_contents($envPath);
preg_match_all("/^" . $key . "\s*=\s*(.*?)$/m", $envContent, $matchs);
return $matchs[1] ?: '';
}
function modifyEnv(array $data)
{
if (empty($data) || !is_array($data)) {
return false;
}
$envPath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '.env';
if (!file_exists($envPath)) {
return false;
}
$envContent = file_get_contents($envPath);
foreach ($data as $key => $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);
}

27
cmd
View File

@ -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 "<script>window.location.href=window.location.href.replace(/:\d+/, ':' + $(env_get APP_PORT))</script>" > ./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

View File

@ -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

18
vite.config.js vendored
View File

@ -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,