2017-07-03 16:16:05 +08:00
|
|
|
const fs = require('fs-extra');
|
2017-07-26 16:33:00 +08:00
|
|
|
const path = require('path');
|
2017-07-03 16:16:05 +08:00
|
|
|
const gulp = require('gulp');
|
2017-07-26 20:59:43 +08:00
|
|
|
const watch = require('gulp-watch');
|
2017-07-03 16:16:05 +08:00
|
|
|
const babel = require('gulp-babel');
|
2017-07-26 16:33:00 +08:00
|
|
|
const ora = require('ora');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const { spawn } = require('child_process');
|
2017-07-26 20:59:43 +08:00
|
|
|
let spinner = null;
|
2017-07-26 16:33:00 +08:00
|
|
|
const DIST = 'server_dist/';
|
|
|
|
const SRC = 'server/**/*.js';
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
function generateBabel(status) {
|
|
|
|
const babelProcess = babel({
|
|
|
|
presets: ['es2015', "stage-3"],
|
|
|
|
plugins: ['transform-runtime']
|
|
|
|
});
|
|
|
|
|
|
|
|
babelProcess.on('error', function (e) {
|
|
|
|
const restart = status ? status.count < 2 : true;
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
output('error', 'babel 编译失败!', restart);
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
status.count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return babelProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
function excuteCmd(cmd, args, opts) {
|
2017-07-26 20:59:43 +08:00
|
|
|
const NAME = cmd === 'ykit' ? chalk.cyan('[ykit]') : chalk.blue('[dev-server]');
|
|
|
|
let command = spawn(cmd, args, opts);
|
2017-07-26 16:33:00 +08:00
|
|
|
|
|
|
|
command.stdout.on('data', data => {
|
2017-07-26 20:59:43 +08:00
|
|
|
output('log', `${NAME} ${data.toString()}`, true);
|
2017-07-26 16:33:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
command.stderr.on('data', data => {
|
2017-07-26 20:59:43 +08:00
|
|
|
output('log', `${NAME} ${data.toString()}`, true);
|
2017-07-26 16:33:00 +08:00
|
|
|
});
|
|
|
|
|
2017-07-26 20:59:43 +08:00
|
|
|
return command;
|
2017-07-26 16:33:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function output(type, message, restart = false) {
|
|
|
|
spinner.stop();
|
|
|
|
|
|
|
|
if (type === 'success') {
|
|
|
|
message = '✔ ' + message;
|
|
|
|
console.log(chalk.green(message));
|
|
|
|
} else if (type === 'error') {
|
|
|
|
message = '✖ ' + message;
|
|
|
|
console.log(chalk.red(message));
|
|
|
|
} else {
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
if (restart) {
|
|
|
|
spinner.start();
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-26 20:59:43 +08:00
|
|
|
function waitingSpinner() {
|
|
|
|
spinner = ora({
|
|
|
|
text: '等待文件变更...',
|
|
|
|
spinner: 'circleQuarters',
|
|
|
|
color: 'cyan'
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
gulp.task('removeDist', [], function () {
|
|
|
|
return fs.removeSync(DIST)
|
2017-07-03 16:16:05 +08:00
|
|
|
});
|
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
gulp.task('initialBuild', ['removeDist'], () => {
|
2017-07-26 20:59:43 +08:00
|
|
|
spinner = ora('初始编译...').start();
|
2017-07-26 16:33:00 +08:00
|
|
|
|
|
|
|
return gulp.src(SRC)
|
|
|
|
.pipe(generateBabel())
|
|
|
|
.pipe(gulp.dest(DIST))
|
|
|
|
.on('end', () => {
|
|
|
|
output('success', '初始编译成功!');
|
2017-07-26 20:59:43 +08:00
|
|
|
waitingSpinner();
|
2017-07-26 16:33:00 +08:00
|
|
|
|
2017-07-26 20:59:43 +08:00
|
|
|
excuteCmd('node_modules/.bin/nodemon', ['-q', 'server_dist/app.js'], {
|
2017-07-26 16:33:00 +08:00
|
|
|
cwd: __dirname
|
|
|
|
});
|
|
|
|
|
|
|
|
excuteCmd('ykit', ['s', '-p', '4000'], {
|
|
|
|
cwd: path.resolve(__dirname, '../')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('default', ['initialBuild'], () => {
|
|
|
|
gulp.watch(SRC, (event) => {
|
2017-07-26 20:59:43 +08:00
|
|
|
let originFilePath = path.relative(path.join(__dirname, 'server'), event.path)
|
|
|
|
let distPath = path.resolve(DIST, path.join(originFilePath))
|
2017-07-26 16:33:00 +08:00
|
|
|
spinner.text = `正在编译 ${event.path}...`;
|
|
|
|
|
|
|
|
gulp.src(event.path).pipe(generateBabel())
|
2017-07-26 20:59:43 +08:00
|
|
|
.pipe(gulp.dest(path.parse(distPath).dir)).on('end', () => {
|
|
|
|
output('success', `成功编译 ${originFilePath}`);
|
|
|
|
output('success', '正在重启服务器...');
|
|
|
|
waitingSpinner();
|
2017-07-26 16:33:00 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-26 20:59:43 +08:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
gulp.task('build', () => {
|
|
|
|
let status = {
|
|
|
|
count: 0
|
|
|
|
};
|
|
|
|
let ykitOutput = '';
|
|
|
|
|
2017-07-26 20:59:43 +08:00
|
|
|
spinner = ora('请稍等...').start();
|
2017-07-26 16:33:00 +08:00
|
|
|
|
|
|
|
gulp.src(SRC)
|
|
|
|
.pipe(generateBabel(status))
|
|
|
|
.pipe(gulp.dest(DIST))
|
|
|
|
.on('error', (err) => {
|
|
|
|
status.count++;
|
|
|
|
output('error', err, status.count < 2);
|
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
status.count++;
|
|
|
|
output('success', '后端编译成功!', status.count < 2);
|
|
|
|
});
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
const ykitBuild = spawn('ykit', ['pack', '-m'], {
|
|
|
|
cwd: __dirname
|
2017-07-03 16:16:05 +08:00
|
|
|
});
|
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
ykitBuild.stderr.on('data', data => {
|
|
|
|
ykitOutput += data.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
ykitBuild.stdout.on('data', data => {
|
|
|
|
ykitOutput += data.toString();
|
|
|
|
});
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-26 16:33:00 +08:00
|
|
|
ykitBuild.on('close', code => {
|
|
|
|
if (code === 0) {
|
|
|
|
status.count++;
|
|
|
|
output('success', '前端编译成功!', status.count < 2);
|
|
|
|
} else {
|
|
|
|
status.count++;
|
|
|
|
output('error', '前端编译失败!', status.count < 2);
|
|
|
|
output('log', ykitOutput);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|