utils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // @ts-check
  2. import fs from 'node:fs'
  3. import pico from 'picocolors'
  4. import { createRequire } from 'node:module'
  5. const require = createRequire(import.meta.url)
  6. export const targets = fs.readdirSync('packages').filter(f => {
  7. if (
  8. !fs.statSync(`packages/${f}`).isDirectory() ||
  9. !fs.existsSync(`packages/${f}/package.json`)
  10. ) {
  11. return false
  12. }
  13. const pkg = require(`../packages/${f}/package.json`)
  14. if (pkg.private && !pkg.buildOptions) {
  15. return false
  16. }
  17. return true
  18. })
  19. /**
  20. *
  21. * @param {ReadonlyArray<string>} partialTargets
  22. * @param {boolean | undefined} includeAllMatching
  23. */
  24. export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
  25. /** @type {Array<string>} */
  26. const matched = []
  27. partialTargets.forEach(partialTarget => {
  28. if (!includeAllMatching && targets.includes(partialTarget)) {
  29. matched.push(partialTarget)
  30. return
  31. }
  32. for (const target of targets) {
  33. if (target.match(partialTarget)) {
  34. matched.push(target)
  35. if (!includeAllMatching) {
  36. break
  37. }
  38. }
  39. }
  40. })
  41. if (matched.length) {
  42. return matched
  43. } else {
  44. console.log()
  45. console.error(
  46. ` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(
  47. `Target ${pico.underline(partialTargets.toString())} not found!`,
  48. )}`,
  49. )
  50. console.log()
  51. process.exit(1)
  52. }
  53. }