Browse Source

主题编译命令优化

jqh 5 years ago
parent
commit
fcc54001a5
2 changed files with 48 additions and 15 deletions
  1. 24 15
      src/Console/ThemeCommand.php
  2. 24 0
      src/Support/Helper.php

+ 24 - 15
src/Console/ThemeCommand.php

@@ -2,9 +2,10 @@
 
 
 namespace Dcat\Admin\Console;
 namespace Dcat\Admin\Console;
 
 
+use Dcat\Admin\Support\Helper;
 use Illuminate\Console\Command;
 use Illuminate\Console\Command;
 use Illuminate\Support\Str;
 use Illuminate\Support\Str;
-use TitasGailius\Terminal\Terminal;
+use Symfony\Component\Process\Process;
 
 
 class ThemeCommand extends Command
 class ThemeCommand extends Command
 {
 {
@@ -52,16 +53,13 @@ class ThemeCommand extends Command
      */
      */
     public function handle()
     public function handle()
     {
     {
-        if (! class_exists(Terminal::class)) {
-            throw new \RuntimeException('Please install "titasgailius/terminal" first!');
-        }
-
         $this->packagePath = realpath(__DIR__.'/../..');
         $this->packagePath = realpath(__DIR__.'/../..');
         $this->files = $this->laravel['files'];
         $this->files = $this->laravel['files'];
 
 
         $name = $this->argument('name');
         $name = $this->argument('name');
 
 
         if ($name === static::ALL) {
         if ($name === static::ALL) {
+            // 编译所有内置主题色
             return $this->compileAllDefaultThemes();
             return $this->compileAllDefaultThemes();
         }
         }
 
 
@@ -77,11 +75,7 @@ class ThemeCommand extends Command
             $this->info("[$name][$color] npm run production...");
             $this->info("[$name][$color] npm run production...");
 
 
             // 编译
             // 编译
-            $response = Terminal::builder()
-                ->timeout(900)
-                ->run("cd {$this->packagePath} && npm run prod");
-
-            $this->info($response->output());
+            $this->runProcess("cd {$this->packagePath} && npm run prod", 1800);
 
 
             // 重置文件
             // 重置文件
             $this->resetFiles();
             $this->resetFiles();
@@ -211,11 +205,7 @@ class ThemeCommand extends Command
 
 
         $this->info('npm install...');
         $this->info('npm install...');
 
 
-        $response = Terminal::builder()
-            ->timeout(1800)
-            ->run("cd {$this->packagePath} && npm install");
-
-        $this->line($response->output());
+        $this->runProcess("cd {$this->packagePath} && npm install");
     }
     }
 
 
     /**
     /**
@@ -263,4 +253,23 @@ class ThemeCommand extends Command
 
 
         return $color;
         return $color;
     }
     }
+
+    /**
+     * 执行命令.
+     *
+     * @param string $command
+     * @param int    $timeout
+     */
+    protected function runProcess($command, $timeout = 1800)
+    {
+        $process = Helper::process($command, $timeout);
+
+        $process->run(function ($type, $data) {
+            if ($type === Process::ERR) {
+                $this->warn($data);
+            } else {
+                $this->info($data);
+            }
+        });
+    }
 }
 }

+ 24 - 0
src/Support/Helper.php

@@ -12,6 +12,7 @@ use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Artisan;
 use Illuminate\Support\Facades\Artisan;
 use Illuminate\Support\Facades\File;
 use Illuminate\Support\Facades\File;
 use Illuminate\Support\Str;
 use Illuminate\Support\Str;
+use Symfony\Component\Process\Process;
 
 
 class Helper
 class Helper
 {
 {
@@ -596,4 +597,27 @@ class Helper
     {
     {
         return (string) (session()->get('admin.prev.url') ? url(session()->get('admin.prev.url')) : url()->previous());
         return (string) (session()->get('admin.prev.url') ? url(session()->get('admin.prev.url')) : url()->previous());
     }
     }
+
+    /**
+     * @param mixed $command
+     * @param int   $timeout
+     * @param null  $input
+     * @param null  $cwd
+     *
+     * @return Process
+     */
+    public static function process($command, $timeout = 100, $input = null, $cwd = null)
+    {
+        $parameters = [
+            $command,
+            $cwd,
+            [],
+            $input,
+            $timeout
+        ];
+
+        return is_string($command)
+            ? Process::fromShellCommandline(...$parameters)
+            : new Process(...$parameters);
+    }
 }
 }