Npm.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\command;
  14. use crmeb\services\FileService;
  15. use crmeb\utils\Terminal;
  16. use think\console\Command;
  17. use think\console\input\Argument;
  18. use think\console\input\Option;
  19. /**
  20. * Class Npm
  21. * @author 等风来
  22. * @email 136327134@qq.com
  23. * @date 2023/4/13
  24. * @package crmeb\command
  25. */
  26. class Npm extends Command
  27. {
  28. /**
  29. * @author 等风来
  30. * @email 136327134@qq.com
  31. * @date 2023/4/13
  32. */
  33. protected function configure()
  34. {
  35. $this->setName('npm')
  36. ->addOption('path', 'dp', Option::VALUE_OPTIONAL, '默认路径')
  37. ->addOption('build', 'bu', Option::VALUE_OPTIONAL, '打包存放路径')
  38. ->setDescription('NPM打包工具');
  39. }
  40. /**
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/4/13
  44. */
  45. public function handle()
  46. {
  47. $path = $this->input->getOption('path');
  48. $build = $this->input->getOption('build');
  49. if (!$build) {
  50. $build = public_path() . 'admin';
  51. }
  52. $terminal = new Terminal();
  53. $terminal->setOutput($this->output);
  54. $adminPath = $path ?: $terminal->adminTemplatePath();
  55. $adminPath = dirname($adminPath);
  56. if (is_dir($adminPath . DS . 'dist')) {
  57. $question = $this->output->confirm($this->input, '检测到已经生成打包文件是否重新打包?', false);
  58. if (!$question) {
  59. $this->output->info('已退出打包程序');
  60. return;
  61. }
  62. }
  63. $dir = $adminPath . DS . 'node_modules';
  64. if (!is_dir($dir)) {
  65. $terminal->run('npm-install');
  66. }
  67. $terminal->run('npm-build');
  68. if (!is_dir($adminPath . DS . 'dist')) {
  69. $this->output->error('打包失败');
  70. return;
  71. }
  72. $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
  73. $this->output->info('执行成功');
  74. }
  75. }