gulpfile.js 2.64 KB
// 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])