utils.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. const { RushConfiguration } = require('@rushstack/rush-sdk')
  6. const getRushConfiguration = (function () {
  7. let rushConfiguration = null
  8. return function () {
  9. // eslint-disable-next-line
  10. return (rushConfiguration ||= RushConfiguration.loadFromDefaultLocation({
  11. startingFolder: process.cwd(),
  12. }))
  13. }
  14. })()
  15. function getChangedPackages(changedFiles) {
  16. const changedPackages = new Set()
  17. try {
  18. const rushConfiguration = getRushConfiguration()
  19. const { rushJsonFolder } = rushConfiguration
  20. const lookup = rushConfiguration.getProjectLookupForRoot(rushJsonFolder)
  21. for (const file of changedFiles) {
  22. const project = lookup.findChildPath(file)
  23. // 如果没找到注册的包信息,则认为是通用文件更改
  24. const packageName = project?.packageName || 'misc'
  25. if (!changedPackages.has(packageName)) {
  26. changedPackages.add(packageName)
  27. }
  28. }
  29. } catch (e) {
  30. console.error(e)
  31. throw e
  32. }
  33. return changedPackages
  34. }
  35. exports.getChangedPackages = getChangedPackages
  36. exports.getRushConfiguration = getRushConfiguration