-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
112 lines (95 loc) · 2.2 KB
/
gulpfile.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
const { spawn } = require('child_process');
const path = require('path');
const chalk = require('chalk');
const del = require('del');
const gulp = require('gulp');
const ts = require('gulp-typescript');
const sourcemaps = require('gulp-sourcemaps');
const runSequence = require('run-sequence');
const tsconfig = require('./tsconfig');
const DIST_DIR = tsconfig.compilerOptions.outDir || path.resolve('./dist');
/**
* 编译 ts 文件
*/
gulp.task('compile', () => {
return gulp
.src('src/**/*.ts')
.pipe(sourcemaps.init())
.pipe(
ts(Object.assign(tsconfig.compilerOptions, {
outDir: DIST_DIR,
}))
)
.pipe(sourcemaps.write())
.pipe(gulp.dest(DIST_DIR));
});
/**
* 删除 dist 目录
*/
gulp.task('del', (cb) => {
return del([DIST_DIR], cb);
});
/**
* 复制 src 文件下的非 .ts 后缀文件到 dist 目录
*/
gulp.task('copy', () => {
return gulp
.src(['src/**/*', '!src/**/*.ts', '!src/@types'])
.pipe(gulp.dest(DIST_DIR));
});
let node;
/**
* 启动 node 进程
*/
gulp.task('server', () => {
if (node) node.kill();
node = spawn('node', [path.resolve('./dist/maius.js')], { stdio: 'inherit' });
node.on('close', (code) => {
console.log(' ');
switch (code) {
case 8 || 12:
console.log(chalk.red('Error detected, attempting reboot...'));
setTimeout(runLiveServer, 500);
break;
case 1:
console.log(chalk.red('The port alreay in use'));
break;
default:
break;
}
if (!code) {
console.log(chalk.green('The Application Had Relaunced.'));
}
console.log(' ');
});
});
/**
* 复合任务:构建项目
*
* 1. 删除 dist 目录
* 2. 构建 TS 源码到 dist 目录下
* 3. 将 src 目录下非 ts 后缀文件复制到 dist 目录下
*/
gulp.task('dist', (cb) => {
runSequence(
'del',
['compile', 'copy'],
cb
);
});
/**
* 复合任务:重新启动并编译项目
*/
gulp.task('server.restart', (cb) => {
runSequence(
'dist',
'server',
cb,
);
})
/**
* 监听 src 目录下的文件改动,重新编译项目,并重启 Node 进程
*/
gulp.task('watch', ['server.restart'], () => {
return gulp.watch('src/**/*', ['server.restart']);
});