|
1 | 1 | const execa = require('execa') |
2 | 2 | const path = require('path') |
3 | 3 | const fs = require('fs-extra') |
4 | | -const execSync = require('child_process').execSync |
| 4 | +const net = require('net') |
| 5 | +const chalk = require('chalk') |
| 6 | + |
| 7 | + |
| 8 | +const { execSync } = require('child_process') |
5 | 9 | const { success } = require('../util/log') |
6 | 10 |
|
| 11 | + |
| 12 | +const errLog = (msg) => console.log(`${chalk.red('[错误]')}${msg}`) |
| 13 | +const successLog = (msg) => console.log(`${chalk.green('[成功]')}${msg}`) |
| 14 | +const baseLog = (msg) => console.log(`${chalk.cyanBright(msg)}`) |
| 15 | +const warnLog = (msg) => console.log(`${chalk.yellow('[警告]')}${msg}`) |
| 16 | +const changeLog = (msg) => console.log(`${chalk.cyan('[变更]')}${msg}`) |
| 17 | +const compileLog = (msg) => console.log(`${chalk.grey('[编译]')}${msg}`) |
| 18 | + |
| 19 | +async function portInUse(port) { |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + const server = net.createServer().listen(port) |
| 22 | + server.on('listening', function () { |
| 23 | + server.close() |
| 24 | + resolve(port) |
| 25 | + }) |
| 26 | + server.on('error', function (err) { |
| 27 | + if (err.code === 'EADDRINUSE') { |
| 28 | + port++ |
| 29 | + reject(err) |
| 30 | + } |
| 31 | + }) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +exports.tryUsePort = (port, _portAvailableCallback) => { |
| 36 | + portInUse(port).then((port) => { |
| 37 | + _portAvailableCallback(port) |
| 38 | + }).catch(() => { |
| 39 | + console.log(port + '被占用') |
| 40 | + port += 1 |
| 41 | + tryUsePort(port, _portAvailableCallback) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
7 | 45 | // 获取当前分支 |
8 | 46 | exports.getCurBranch = () => { |
9 | 47 | return execSync('git rev-parse --abbrev-ref HEAD').toString().trim() |
|
0 commit comments