-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
120 lines (108 loc) · 3.01 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
113
114
115
116
117
118
119
120
/*
* @Author: dyr
* @Description: gulp 构建
* @Date: 2019-09-17 17:23:04
* @LastEditors: dyr
* @LastEditTime: 2019-11-17 13:38:25
*/
/* eslint-disable no-console */
const gulp = require('gulp');
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const {
clean,
copyWxml,
copyJson,
compileJs,
compileCss,
copyAssets,
copyAresBaiduWithArtifacts,
cleanJscpd,
copyToJsCpd,
formatWxssToCss,
updateVersion,
creatTag,
} = require('./build/tasks');
const cwd = process.cwd();
function compile(filePath) {
if (filePath.includes('artifacts') || filePath.includes('ares-baidu')) {
return copyAresBaiduWithArtifacts(filePath);
}
console.info(chalk.green(`编译完成:${path.basename(filePath)}`));
if (filePath.endsWith('.ts') || filePath.endsWith('.js')) {
compileJs(filePath);
} else if (
filePath.endsWith('.less') ||
filePath.endsWith('.css') ||
filePath.endsWith('.sass') ||
filePath.endsWith('.wxss')
) {
compileCss(filePath);
} else if (filePath.endsWith('.wxml') || filePath.endsWith('.swan')) {
copyWxml(filePath);
} else if (filePath.endsWith('.json')) {
copyJson(filePath);
} else {
copyAssets(filePath);
}
}
/* watch */
function watch() {
console.log(chalk.blue(`正在监听文件...`));
const watcher = gulp.watch('src/**/**');
watcher.on('change', function(filePath) {
compile(filePath);
});
watcher.on('add', function(filePath) {
compile(filePath);
});
watcher.on('unlink', function(filePath) {
let distFile = filePath.replace(/^src\b/, 'dist');
let absolutePath = '';
if (distFile.endsWith('.ts')) {
distFile = distFile.replace(/.ts$/, '.js');
} else if (distFile.endsWith('.less')) {
distFile = distFile.replace(/.less$/, '.wxss');
}
absolutePath = path.join(cwd, distFile);
if (fs.existsSync(absolutePath)) {
fs.unlinkSync(absolutePath);
console.log(chalk.yellow(`删除文件:${path.basename(distFile)}`));
}
});
}
/* tasks */
const buildTasks = gulp.parallel([compileJs, compileCss, copyJson, copyWxml, copyAssets, copyAresBaiduWithArtifacts]);
/* 开发环境 监听文件 */
if (process.env.NODE_ENV === 'development') {
gulp.task('default', gulp.series([clean, gulp.parallel([watch, buildTasks])]));
} else {
gulp.task(
'default',
gulp.series([clean].concat(buildTasks), done => {
console.log(chalk.blue(`build成功...`));
done();
}),
);
}
gulp.task('watch', watch);
// 统计代码重复率tasks
const buildJsCpdTasks = [gulp.parallel([copyToJsCpd, formatWxssToCss])];
gulp.task(
'jscpd',
gulp.series([cleanJscpd].concat(buildJsCpdTasks), done => {
console.log(chalk.blue(`jscpd文件生成成功,开始统计代码重复率中....`));
done();
}),
);
gulp.task('clean-cpd', gulp.series(cleanJscpd));
// 更新版本号
gulp.task(
'bumpversion',
gulp.series(updateVersion, async () => {
await console.log(chalk.blue(`版本号已更新`));
}),
);
// 创建tag
gulp.task('tag', creatTag);