utils.js 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const fs = require('fs')
fxy060608's avatar
fxy060608 已提交
2
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3 4
const chalk = require('chalk')

fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13 14
const priority = {
  'uni-shared': 100,
  'uni-i18n': 90,
  'uni-mp-vue': 80,
  'uni-mp-alipay': 70,
  'uni-mp-baidu': 70,
  'uni-mp-qq': 70,
  'uni-mp-toutiao': 70,
  'uni-mp-weixin': 70,
  'uni-quickapp-webview': 70,
fxy060608's avatar
fxy060608 已提交
15 16
  'uni-h5': 60,
  'uni-h5-vue': 50,
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
  'uni-cli-shared': 40,
  'vite-plugin-uni': 30,
  'size-check': 1,
}

fxy060608's avatar
fxy060608 已提交
22
const targets = (exports.targets = fs.readdirSync('packages').filter((f) => {
fxy060608's avatar
fxy060608 已提交
23 24 25 26
  if (!fs.statSync(`packages/${f}`).isDirectory()) {
    return false
  }
  try {
fxy060608's avatar
fxy060608 已提交
27 28 29 30
    return (
      fs.existsSync(path.resolve(__dirname, `../packages/${f}/build.json`)) ||
      fs.existsSync(path.resolve(__dirname, `../packages/${f}/build.js`))
    )
fxy060608's avatar
fxy060608 已提交
31 32
  } catch (e) {}
  return false
fxy060608's avatar
fxy060608 已提交
33
})).sort((a, b) => priority[b] - priority[a])
fxy060608's avatar
fxy060608 已提交
34 35
exports.fuzzyMatchTarget = (partialTargets, includeAllMatching) => {
  const matched = []
fxy060608's avatar
fxy060608 已提交
36
  partialTargets.forEach((partialTarget) => {
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    for (const target of targets) {
      if (target.match(partialTarget)) {
        matched.push(target)
        if (!includeAllMatching) {
          break
        }
      }
    }
  })
  if (matched.length) {
    return matched
  } else {
    console.log()
    console.error(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
        `Target ${chalk.underline(partialTargets)} not found!`
      )}`
    )
    console.log()

    process.exit(1)
  }
}