Skip to content

Commit c9d7120

Browse files
committed
fix: 修复 ora 不支持 node 0.12
1 parent 4b3a960 commit c9d7120

File tree

6 files changed

+255
-10
lines changed

6 files changed

+255
-10
lines changed

lib/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require('colors');
44

5-
global.spinner = require('ora')();
5+
global.spinner = require('./utils/ora')();
66
global.fs = require('fs');
77
global.sysPath = require('path');
88
global.async = require('async');

lib/plugins/progressBarPlugin.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ module.exports = new ProgressBarPlugin();
66

77
function ProgressBarPlugin() {
88
return new webpack.ProgressPlugin(function (percent, msg) {
9-
spinner.start();
9+
if (percent === 0) {
10+
spinner.start();
11+
} else if (msg === 'emit') {
12+
spinner.stop();
13+
}
1014

1115
if (msg) {
1216
spinner.text = '[Bundler] ' + msg;
13-
if(msg === 'emit') {
14-
setTimeout(() => {
15-
spinner.stop().clear();
16-
}, 100)
17-
}
1817
}
1918
});
20-
}
19+
}

lib/utils/ora.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
'use strict';
2+
3+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4+
5+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
var chalk = require('chalk');
10+
var extend = require('extend');
11+
var cliCursor = require('cli-cursor');
12+
var cliSpinners = require('cli-spinners');
13+
var logSymbols = require('log-symbols');
14+
15+
var Ora = function () {
16+
function Ora(options) {
17+
_classCallCheck(this, Ora);
18+
19+
if (typeof options === 'string') {
20+
options = {
21+
text: options
22+
};
23+
}
24+
25+
this.options = extend(true, {
26+
text: '',
27+
color: 'cyan',
28+
stream: process.stderr
29+
}, options);
30+
31+
var sp = this.options.spinner;
32+
this.spinner = (typeof sp === 'undefined' ? 'undefined' : _typeof(sp)) === 'object' ? sp : process.platform === 'win32' ? cliSpinners.line : cliSpinners[sp] || cliSpinners.dots; // eslint-disable-line no-nested-ternary
33+
34+
if (this.spinner.frames === undefined) {
35+
throw new Error('Spinner must define `frames`');
36+
}
37+
38+
this.text = this.options.text;
39+
this.color = this.options.color;
40+
this.interval = this.options.interval || this.spinner.interval || 100;
41+
this.stream = this.options.stream;
42+
this.id = null;
43+
this.frameIndex = 0;
44+
this.enabled = this.options.enabled || this.stream && this.stream.isTTY && !process.env.CI;
45+
}
46+
47+
_createClass(Ora, [{
48+
key: 'frame',
49+
value: function frame() {
50+
var frames = this.spinner.frames;
51+
var frame = frames[this.frameIndex];
52+
53+
if (this.color) {
54+
frame = chalk[this.color](frame);
55+
}
56+
57+
this.frameIndex = ++this.frameIndex % frames.length;
58+
59+
return frame + ' ' + this.text;
60+
}
61+
}, {
62+
key: 'clear',
63+
value: function clear() {
64+
if (!this.enabled) {
65+
return this;
66+
}
67+
68+
this.stream.clearLine();
69+
this.stream.cursorTo(0);
70+
71+
return this;
72+
}
73+
}, {
74+
key: 'render',
75+
value: function render() {
76+
this.clear();
77+
this.stream.write(this.frame());
78+
79+
return this;
80+
}
81+
}, {
82+
key: 'start',
83+
value: function start() {
84+
if (!this.enabled || this.id) {
85+
return this;
86+
}
87+
88+
cliCursor.hide();
89+
this.render();
90+
this.id = setInterval(this.render.bind(this), this.interval);
91+
92+
return this;
93+
}
94+
}, {
95+
key: 'stop',
96+
value: function stop() {
97+
if (!this.enabled) {
98+
return this;
99+
}
100+
101+
clearInterval(this.id);
102+
this.id = null;
103+
this.frameIndex = 0;
104+
this.clear();
105+
cliCursor.show();
106+
107+
return this;
108+
}
109+
}, {
110+
key: 'succeed',
111+
value: function succeed() {
112+
return this.stopAndPersist(logSymbols.success);
113+
}
114+
}, {
115+
key: 'fail',
116+
value: function fail() {
117+
return this.stopAndPersist(logSymbols.error);
118+
}
119+
}, {
120+
key: 'stopAndPersist',
121+
value: function stopAndPersist(symbol) {
122+
this.stop();
123+
this.stream.write((symbol || ' ') + ' ' + this.text + '\n');
124+
125+
return this;
126+
}
127+
}]);
128+
129+
return Ora;
130+
}();
131+
132+
module.exports = function (opts) {
133+
return new Ora(opts);
134+
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"license": "MIT",
1818
"dependencies": {
1919
"async": "^1.5.2",
20+
"chalk": "^1.1.3",
21+
"cli-cursor": "^1.0.2",
22+
"cli-spinners": "^0.3.0",
2023
"colors": "^1.1.2",
2124
"connect": "^3.4.1",
2225
"css-loader": "^0.23.1",
@@ -31,10 +34,10 @@
3134
"json5": "^0.5.0",
3235
"left-pad": "^1.1.3",
3336
"loader-utils": "^0.2.15",
37+
"log-symbols": "^1.0.2",
3438
"mkdirp": "^0.5.1",
3539
"moment": "^2.14.1",
3640
"optimist": "^0.6.1",
37-
"ora": "^0.3.0",
3841
"replacestream": "^4.0.0",
3942
"request": "^2.75.0",
4043
"require-uncached": "^1.0.2",

src/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require('colors');
44

5-
global.spinner = require('ora')();
5+
global.spinner = require('./utils/ora')();
66
global.fs = require('fs');
77
global.sysPath = require('path');
88
global.async = require('async');

src/utils/ora.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'use strict';
2+
const chalk = require('chalk');
3+
const extend = require('extend');
4+
const cliCursor = require('cli-cursor');
5+
const cliSpinners = require('cli-spinners');
6+
const logSymbols = require('log-symbols');
7+
8+
class Ora {
9+
constructor(options) {
10+
if (typeof options === 'string') {
11+
options = {
12+
text: options
13+
};
14+
}
15+
16+
this.options = extend(true, {
17+
text: '',
18+
color: 'cyan',
19+
stream: process.stderr
20+
}, options);
21+
22+
const sp = this.options.spinner;
23+
this.spinner = typeof sp === 'object'
24+
? sp
25+
: (process.platform === 'win32'
26+
? cliSpinners.line
27+
: (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary
28+
29+
if (this.spinner.frames === undefined) {
30+
throw new Error('Spinner must define `frames`');
31+
}
32+
33+
this.text = this.options.text;
34+
this.color = this.options.color;
35+
this.interval = this.options.interval || this.spinner.interval || 100;
36+
this.stream = this.options.stream;
37+
this.id = null;
38+
this.frameIndex = 0;
39+
this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI);
40+
}
41+
frame() {
42+
const frames = this.spinner.frames;
43+
let frame = frames[this.frameIndex];
44+
45+
if (this.color) {
46+
frame = chalk[this.color](frame);
47+
}
48+
49+
this.frameIndex = ++this.frameIndex % frames.length;
50+
51+
return frame + ' ' + this.text;
52+
}
53+
clear() {
54+
if (!this.enabled) {
55+
return this;
56+
}
57+
58+
this.stream.clearLine();
59+
this.stream.cursorTo(0);
60+
61+
return this;
62+
}
63+
render() {
64+
this.clear();
65+
this.stream.write(this.frame());
66+
67+
return this;
68+
}
69+
start() {
70+
if (!this.enabled || this.id) {
71+
return this;
72+
}
73+
74+
cliCursor.hide();
75+
this.render();
76+
this.id = setInterval(this.render.bind(this), this.interval);
77+
78+
return this;
79+
}
80+
stop() {
81+
if (!this.enabled) {
82+
return this;
83+
}
84+
85+
clearInterval(this.id);
86+
this.id = null;
87+
this.frameIndex = 0;
88+
this.clear();
89+
cliCursor.show();
90+
91+
return this;
92+
}
93+
succeed() {
94+
return this.stopAndPersist(logSymbols.success);
95+
}
96+
fail() {
97+
return this.stopAndPersist(logSymbols.error);
98+
}
99+
stopAndPersist(symbol) {
100+
this.stop();
101+
this.stream.write(`${symbol || ' '} ${this.text}\n`);
102+
103+
return this;
104+
}
105+
}
106+
107+
module.exports = function(opts) {
108+
return new Ora(opts);
109+
};

0 commit comments

Comments
 (0)