15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
 
const path = require('path');
const Utils = require('../lib/utils');
const is = require('is-type-of');
const chalk = require('chalk');
const crossSpawn = require('cross-spawn');
 
module.exports = {
 
  execProcess: {},
 
  /**
   * 启动前端、主进程服务
   */
  dev(options = {}) {
    const { config, serve } = options;
    const binCmd = 'dev';
    const binCfg = Utils.loadConfig(config);
    const binCmdConfig = binCfg[binCmd];
 
    let command = serve;
    if (!command) {
      command = Object.keys(binCmdConfig).join();
    }
 
    const opt = {
      binCmd,
      binCmdConfig,
      command,
    }
    this.multiExec(opt);
  },
 
  /**
   * 启动主进程服务
   */
  start(options = {}) {
    const { config } = options;
    const binCmd = 'start';
    const binCfg = Utils.loadConfig(config);
    const binCmdConfig = {
      start: binCfg[binCmd]
    };
 
    const opt = {
      binCmd,
      binCmdConfig,
      command: binCmd,
    }
    this.multiExec(opt);
  },
 
  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  },
 
  /**
   * 构建
   */
  build(options = {}) {
    const { config, cmds } = options;
    const binCmd = 'build';
    const binCfg = Utils.loadConfig(config);
    const binCmdConfig = binCfg[binCmd];
 
    if (!cmds || cmds == "") {
      // [todo]
      let tip = chalk.bgYellow('Warning') + ' Please modify the ' + chalk.blue('build') + ' config, See: ';
      tip += chalk.underline('https://www.kaka996.com/pages/c492f8/');
      console.log(tip);
      return
    }
 
    const opt = {
      binCmd,
      binCmdConfig,
      command: cmds,
    }
    this.multiExec(opt);
  },
 
  /**
   * 执行自定义命令
   */
  exec(options = {}) {
    let { config, command, cmds } = options;
    const binCmd = 'exec';
    const binCfg = Utils.loadConfig(config);
    const binCmdConfig = binCfg[binCmd];
 
    // if (typeof command !== "string" || !cmds) {
    //   console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.red(`Error: Please specify parameters for --cmds` ));
    //   return
    // }
 
    // 兼容
    if (typeof command === "string") {
      cmds = command;
    }
 
    const opt = {
      binCmd,
      binCmdConfig,
      command: cmds,
    }
    this.multiExec(opt);
  },
 
  /**
   * 支持多个命令
   */
  multiExec(opt = {}) {
    //console.log('multiExec opt:', opt)
    const { binCmd, binCmdConfig, command } = opt;
 
    let cmds;
    const cmdString = command.trim();
    if (cmdString.indexOf(',') !== -1) {
      cmds = cmdString.split(',');
    } else {
      cmds = [cmdString];
    }
 
    for (let i = 0; i < cmds.length; i++) {
      let cmd = cmds[i];
      let cfg = binCmdConfig[cmd];
 
      if (!cfg) {
        console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.red(`Error: [${binCmd} ${cmd}] config does not exist` ));
        continue;
      }
 
      // frontend 如果是 file:// 协议,则不启动
      if (cmd == 'frontend' && cfg.protocol == 'file://') {
        continue;
      }
 
      console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + "Run " + chalk.green(`[${binCmd} ${cmd}]` + " command"));
      console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('config:'), JSON.stringify(cfg));
 
      let execDir = path.join(process.cwd(), cfg.directory);
      let execArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
      let stdio = cfg.stdio ? cfg.stdio: 'inherit';
 
      const handler = cfg.sync ? crossSpawn.sync : crossSpawn;
 
      this.execProcess[cmd] = handler(
        cfg.cmd,
        execArgs,
        { stdio: stdio, cwd: execDir, maxBuffer: 1024 * 1024 * 1024 },
      );
      console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ` command is ${cfg.sync ? 'run completed' : 'running'}`);
 
      if(!cfg.sync) {
        this.execProcess[cmd].on('exit', () => {
          if (cmd == 'electron') {
            console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('Press "CTRL+C" to exit'));
            return
          }
          console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ' command is executed and exits');
        });
      }
    }
  },
 
}