index.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const path = require('path')
fxy060608's avatar
fxy060608 已提交
2
const fs = require('fs-extra')
fxy060608's avatar
fxy060608 已提交
3

fxy060608's avatar
fxy060608 已提交
4 5
const validate = require('./validate')

fxy060608's avatar
fxy060608 已提交
6 7
const patchVant = require('./vant')

fxy060608's avatar
fxy060608 已提交
8 9 10 11
const migraters = {
  'mp-weixin': require('./mp-weixin')
}

fxy060608's avatar
fxy060608 已提交
12 13
module.exports = function migrate(input, out, options = {}) {
  options.platform = options.platform || 'mp-weixin'
fxy060608's avatar
fxy060608 已提交
14 15 16 17
  const migrater = migraters[options.platform]
  if (!migrater) {
    return console.error(`错误: 目前支持 Object.keys(migraters).join(',') 转换`)
  }
fxy060608's avatar
fxy060608 已提交
18 19
  input = path.resolve(input)
  out = path.resolve(out || input)
fxy060608's avatar
fxy060608 已提交
20 21
  if (!validate(input, out, options)) {
    return
fxy060608's avatar
fxy060608 已提交
22
  }
fxy060608's avatar
fxy060608 已提交
23 24
  const [files, assets] = migrater.transform(input, out, options)
  files.forEach(file => {
fxy060608's avatar
fxy060608 已提交
25
    options.silent !== true && console.log(`write: ${file.path}`)
fxy060608's avatar
fxy060608 已提交
26 27 28
    fs.outputFileSync(file.path, file.content)
  })
  const styleExtname = options.extname.style
fxy060608's avatar
fxy060608 已提交
29 30

  const needCopy = input !== out
fxy060608's avatar
fxy060608 已提交
31
  assets.forEach(asset => {
fxy060608's avatar
fxy060608 已提交
32 33 34
    if (typeof asset === 'string') {
      const src = path.resolve(input, asset)
      const dest = path.resolve(out, asset.replace(styleExtname, '.css'))
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41
      if (
        needCopy || (
          asset.indexOf(styleExtname) !== -1 &&
          styleExtname !== '.css'
        )
      ) {
        options.silent !== true && console.log(`copy: ${dest}`)
42
        src !== dest && fs.copySync(src, dest)
fxy060608's avatar
fxy060608 已提交
43 44
      }
    } else {
fxy060608's avatar
fxy060608 已提交
45
      options.silent !== true && console.log(`write: ${path.resolve(out, asset.path)}`)
fxy060608's avatar
fxy060608 已提交
46 47
      fs.outputFileSync(path.resolve(out, asset.path), asset.content)
    }
fxy060608's avatar
fxy060608 已提交
48
  })
fxy060608's avatar
fxy060608 已提交
49
  patchVant(files, assets, out)
fxy060608's avatar
fxy060608 已提交
50
}