validate.js 1.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const fs = require('fs')
const path = require('path')

const migraters = {
  'mp-weixin': require('./mp-weixin').options
}

module.exports = function validate(input, out, options) {
  input = path.resolve(input)
  if (!fs.existsSync(input)) {
    return console.error(`错误: '${input}' 不存在`)
  }

  const platformOptions = migraters[options.platform]
fxy060608's avatar
fxy060608 已提交
15 16
  options.extname = platformOptions.extname
  const templateExtname = options.extname.template
fxy060608's avatar
fxy060608 已提交
17 18 19

  const stat = fs.lstatSync(input)
  if (stat.isFile()) {
fxy060608's avatar
fxy060608 已提交
20 21
    if (path.extname(input) !== templateExtname) {
      return console.error(`错误: 单文件转换需要传入 ${templateExtname.substr(1)} 文件地址`)
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
    }
    options.target = 'file'
  } else if (stat.isDirectory()) {
    options.base = input
    if (fs.existsSync(path.resolve(input, 'app.json'))) {
      options.target = 'app'
    } else {
      options.target = 'folder'
    }
  } else {
    return console.error(`错误: '${input}' 不支持转换`)
  }
  return true
}