utils.js 1.6 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
const priority = {
  'uni-shared': 100,
fxy060608's avatar
fxy060608 已提交
7
  'uni-app': 90,
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15
  '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 已提交
16 17
  'uni-cli-shared': 60,
  'uni-h5': 50,
fxy060608's avatar
fxy060608 已提交
18
  'uni-h5-vite': 40,
fxy060608's avatar
fxy060608 已提交
19
  'uni-app-vue': 35,
fxy060608's avatar
fxy060608 已提交
20
  'uni-app-plus': 30,
fxy060608's avatar
fxy060608 已提交
21
  'uni-app-vite': 30,
fxy060608's avatar
fxy060608 已提交
22
  'vite-plugin-uni': 20,
fxy060608's avatar
fxy060608 已提交
23 24 25
  'size-check': 1,
}

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

    process.exit(1)
  }
}