Merge branch 'dev' of gitlab.corp.qunar.com:mfe/yapi into dev

This commit is contained in:
waliang.wang 2017-07-26 21:10:27 +08:00
commit 9ca83c71e1
2 changed files with 43 additions and 27 deletions

View File

@ -1,11 +1,12 @@
const fs = require('fs-extra');
const path = require('path');
const gulp = require('gulp');
const watch = require('gulp-watch');
const babel = require('gulp-babel');
const ora = require('ora');
const chalk = require('chalk');
const { spawn } = require('child_process');
let spinner = ora('请稍等...').start();
let spinner = null;
const DIST = 'server_dist/';
const SRC = 'server/**/*.js';
@ -30,21 +31,18 @@ function generateBabel(status) {
}
function excuteCmd(cmd, args, opts) {
const command = spawn(cmd, args, opts);
const NAME = cmd === 'ykit' ? chalk.cyan('[ykit]') : chalk.blue('[dev-server]');
let command = spawn(cmd, args, opts);
command.stdout.on('data', data => {
output('log', `${cmd}: ${data.toString()}`, true);
output('log', `${NAME} ${data.toString()}`, true);
});
command.stderr.on('data', data => {
output('log', `${cmd}: ${data.toString()}`, true);
output('log', `${NAME} ${data.toString()}`, true);
});
command.on('close', code => {
if (code !== 0) {
output('log', `${cmd}: ${data.toString()}`);
}
});
return command;
}
function output(type, message, restart = false) {
@ -64,25 +62,29 @@ function output(type, message, restart = false) {
}
}
function waitingSpinner() {
spinner = ora({
text: '等待文件变更...',
spinner: 'circleQuarters',
color: 'cyan'
}).start();
}
gulp.task('removeDist', [], function () {
return fs.removeSync(DIST)
});
gulp.task('initialBuild', ['removeDist'], () => {
spinner.text = '初始编译...';
spinner = ora('初始编译...').start();
return gulp.src(SRC)
.pipe(generateBabel())
.pipe(gulp.dest(DIST))
.on('end', () => {
output('success', '初始编译成功!');
spinner = ora({
text: '等待文件变更...',
spinner: 'pong',
color: 'green'
}).start();
waitingSpinner();
excuteCmd('node_modules/.bin/nodemon', ['-q', 'server_dist/app.js', 'dev'], {
excuteCmd('node_modules/.bin/nodemon', ['-q', 'server_dist/app.js'], {
cwd: __dirname
});
@ -94,28 +96,41 @@ gulp.task('initialBuild', ['removeDist'], () => {
gulp.task('default', ['initialBuild'], () => {
gulp.watch(SRC, (event) => {
let originFilePath = path.relative(path.join(__dirname, 'server'), event.path)
let distPath = path.resolve(DIST, path.join(originFilePath))
spinner.text = `正在编译 ${event.path}...`;
gulp.src(event.path).pipe(generateBabel())
.pipe(gulp.dest(DIST)).on('end', () => {
output('success', `成功编译 ${event.path}`);
spinner = ora({
text: 'waiting changes...',
spinner: 'pong',
color: 'green'
});
spinner.start();
.pipe(gulp.dest(path.parse(distPath).dir)).on('end', () => {
output('success', `成功编译 ${originFilePath}`);
output('success', '正在重启服务器...');
waitingSpinner();
});
});
});
gulp.task('buildNode', () => {
return gulp.src(SRC)
.pipe(generateBabel())
.pipe(gulp.dest(DIST));
});
gulp.task('watchNode', ['buildNode'], () => {
return watch(SRC, {
verbose: true,
ignoreInitial: false
})
.pipe(generateBabel())
.pipe(gulp.dest(DIST));
});
gulp.task('build', () => {
let status = {
count: 0
};
let ykitOutput = '';
spinner.text = '正在编译...';
spinner = ora('请稍等...').start();
gulp.src(SRC)
.pipe(generateBabel(status))

View File

@ -5,10 +5,11 @@
"main": "index.js",
"scripts": {
"build-server": "babel server -d server_dist",
"dev-server": "nodemon server_dist/app.js",
"dev-server": "nodemon server_dist/app.js -L",
"install-server" : "node server_dist/install.js",
"dev": "gulp --silent",
"build": "gulp build --silent"
"build": "gulp build --silent",
"only-watch": "gulp watchNode"
},
"repository": {
"type": "git",