gulpfile.js
2.64 KB
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
// require("./build/gulp.css.js")
const { src, dest, series, watch } = require('gulp')
// const del = require('del')
const fs = require('fs')
const through2 = require('through2')
// // // 删除website/中的所有子目录
// async function clean(cb) {
// await del('./website')
// // cb()
// }
// 迭代 components 下的所有 md 文件,并合并到 website/components 下的同名文件
function generateMd(path) {
return src(path).pipe(
through2.obj(function(file, _, cb) {
// console.log('file: ', file);
let filename = file.path.split(/\\/).pop()
console.log(filename)
if (file.isBuffer()) {
try {
fs.readFile(`./website/components/${filename}`, (err, data) => {
// console.log('data: ', data);
if (data) {
// 存在合并
const originalData = data.toString().replace(/<!-- <content> -->[\s\S]*<!-- <\/content> -->/g, '')
if (err) throw err
let fileContent = file.contents.toString() + '\n\n' + originalData
fs.writeFile(`./website/components/${filename}`, fileContent, err => {
if (err) return console.error(err)
// console.log(`写入 ${filename} 成功`)
cb()
})
} else {
// 不存在创建
let filePath
if (filename === 'README.md') {
filePath = `./website/${filename}`
} else {
filePath = `./website/components/${filename}`
}
let fileContent = file.contents.toString()
fs.writeFile(filePath, fileContent, err => {
if (err) return console.error(err)
console.log(`写入 ${filename} 成功`)
cb()
})
}
})
} catch (exception) {
cb()
}
}
})
)
}
async function generateAllMd() {
await generateMd('./src/packages/**/*.md')
await generateMd('./src/bcPackages/**/*.md')
}
function copyFile() {
// return src(['./dist/*.js','./dist/*.css','./documents/index.html']).pipe(dest('./website/'))
return src(['./dist/*.js', './dist/*.css', './dist/fonts/*.*', './documents/**/*.*']).pipe(dest('./website/'))
}
function copyFonts() {
return src(['./dist/fonts/*.*']).pipe(dest('./website/fonts/'))
}
// require("./build/gulp.css.js")
//监听这些文件的变换,不需要cb(只监听*.md,变化刷新)
function watcher() {
watch('src/**/*.*', generateAllMd, copyFile, copyFonts)
watch('documents/**/*.*', generateAllMd, copyFile, copyFonts)
}
exports.default = series([generateAllMd, copyFile, copyFonts, watcher])