utils.mjs 1009 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import fs from 'node:fs'
  2. import chalk from 'chalk'
  3. import { createRequire } from 'node:module'
  4. const require = createRequire(import.meta.url)
  5. export const targets = fs.readdirSync('packages').filter(f => {
  6. if (!fs.statSync(`packages/${f}`).isDirectory()) {
  7. return false
  8. }
  9. const pkg = require(`../packages/${f}/package.json`)
  10. if (pkg.private && !pkg.buildOptions) {
  11. return false
  12. }
  13. return true
  14. })
  15. export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
  16. const matched = []
  17. partialTargets.forEach(partialTarget => {
  18. for (const target of targets) {
  19. if (target.match(partialTarget)) {
  20. matched.push(target)
  21. if (!includeAllMatching) {
  22. break
  23. }
  24. }
  25. }
  26. })
  27. if (matched.length) {
  28. return matched
  29. } else {
  30. console.log()
  31. console.error(
  32. ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
  33. `Target ${chalk.underline(partialTargets)} not found!`
  34. )}`
  35. )
  36. console.log()
  37. process.exit(1)
  38. }
  39. }