forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuteCommand.js
145 lines (126 loc) · 3.83 KB
/
executeCommand.js
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
const chalk = require('chalk')
const EventEmitter = require('events')
const execa = require('execa')
const readline = require('readline')
const debug = require('debug')('vue-cli:install')
class InstallProgress extends EventEmitter {
constructor () {
super()
this._progress = -1
}
get progress () {
return this._progress
}
set progress (value) {
this._progress = value
this.emit('progress', value)
}
get enabled () {
return this._progress !== -1
}
set enabled (value) {
this.progress = value ? 0 : -1
}
log (value) {
this.emit('log', value)
}
}
function toStartOfLine (stream) {
if (!chalk.supportsColor) {
stream.write('\r')
return
}
readline.cursorTo(stream, 0)
}
function renderProgressBar (curr, total) {
const ratio = Math.min(Math.max(curr / total, 0), 1)
const bar = ` ${curr}/${total}`
const availableSpace = Math.max(0, process.stderr.columns - bar.length - 3)
const width = Math.min(total, availableSpace)
const completeLength = Math.round(width * ratio)
const complete = `#`.repeat(completeLength)
const incomplete = `-`.repeat(width - completeLength)
toStartOfLine(process.stderr)
process.stderr.write(`[${complete}${incomplete}]${bar}`)
}
const progress = exports.progress = new InstallProgress()
exports.executeCommand = function executeCommand (command, args, cwd) {
debug(`command: `, command)
debug(`args: `, args)
return new Promise((resolve, reject) => {
const apiMode = process.env.VUE_CLI_API_MODE
progress.enabled = false
if (apiMode) {
if (command === 'npm') {
// TODO when this is supported
} else if (command === 'yarn') {
args.push('--json')
}
}
const child = execa(command, args, {
cwd,
stdio: ['inherit', apiMode ? 'pipe' : 'inherit', !apiMode && command === 'yarn' ? 'pipe' : 'inherit']
})
if (apiMode) {
let progressTotal = 0
let progressTime = Date.now()
child.stdout.on('data', buffer => {
let str = buffer.toString().trim()
if (str && command === 'yarn' && str.indexOf('"type":') !== -1) {
const newLineIndex = str.lastIndexOf('\n')
if (newLineIndex !== -1) {
str = str.substr(newLineIndex)
}
try {
const data = JSON.parse(str)
if (data.type === 'step') {
progress.enabled = false
progress.log(data.data.message)
} else if (data.type === 'progressStart') {
progressTotal = data.data.total
} else if (data.type === 'progressTick') {
const time = Date.now()
if (time - progressTime > 20) {
progressTime = time
progress.progress = data.data.current / progressTotal
}
} else {
progress.enabled = false
}
} catch (e) {
console.error(e)
console.log(str)
}
} else {
process.stdout.write(buffer)
}
})
} else {
// filter out unwanted yarn output
if (command === 'yarn') {
child.stderr.on('data', buf => {
const str = buf.toString()
if (/warning/.test(str)) {
return
}
// progress bar
const progressBarMatch = str.match(/\[.*\] (\d+)\/(\d+)/)
if (progressBarMatch) {
// since yarn is in a child process, it's unable to get the width of
// the terminal. reimplement the progress bar ourselves!
renderProgressBar(progressBarMatch[1], progressBarMatch[2])
return
}
process.stderr.write(buf)
})
}
}
child.on('close', code => {
if (code !== 0) {
reject(`command failed: ${command} ${args.join(' ')}`)
return
}
resolve()
})
})
}