pnpm

Форк
0
/
buildSequence.ts 
65 строк · 2.3 Кб
1
import { graphSequencer } from '@pnpm/deps.graph-sequencer'
2
import { type PackageManifest, type PatchFile } from '@pnpm/types'
3
import filter from 'ramda/src/filter'
4

5
export interface DependenciesGraphNode {
6
  children: Record<string, string>
7
  depPath: string
8
  name: string
9
  dir: string
10
  fetchingBundledManifest?: () => Promise<PackageManifest | undefined>
11
  filesIndexFile?: string
12
  hasBin: boolean
13
  hasBundledDependencies: boolean
14
  installable?: boolean
15
  isBuilt?: boolean
16
  optional: boolean
17
  optionalDependencies: Set<string>
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
  requiresBuild?: boolean | any // this is a dirty workaround added in https://github.com/pnpm/pnpm/pull/4898
20
  patchFile?: PatchFile
21
}
22

23
export interface DependenciesGraph {
24
  [depPath: string]: DependenciesGraphNode
25
}
26

27
export function buildSequence (
28
  depGraph: Record<string, Pick<DependenciesGraphNode, 'children' | 'requiresBuild'>>,
29
  rootDepPaths: string[]
30
): string[][] {
31
  const nodesToBuild = new Set<string>()
32
  getSubgraphToBuild(depGraph, rootDepPaths, nodesToBuild, new Set<string>())
33
  const onlyFromBuildGraph = filter((depPath: string) => nodesToBuild.has(depPath))
34
  const nodesToBuildArray = Array.from(nodesToBuild)
35
  const graph = new Map(
36
    nodesToBuildArray
37
      .map((depPath) => [depPath, onlyFromBuildGraph(Object.values(depGraph[depPath].children))])
38
  )
39
  const graphSequencerResult = graphSequencer(graph, nodesToBuildArray)
40
  const chunks = graphSequencerResult.chunks as string[][]
41
  return chunks
42
}
43

44
function getSubgraphToBuild (
45
  graph: Record<string, Pick<DependenciesGraphNode, 'children' | 'requiresBuild' | 'patchFile'>>,
46
  entryNodes: string[],
47
  nodesToBuild: Set<string>,
48
  walked: Set<string>
49
): boolean {
50
  let currentShouldBeBuilt = false
51
  for (const depPath of entryNodes) {
52
    const node = graph[depPath]
53
    if (!node) continue // packages that are already in node_modules are skipped
54
    if (walked.has(depPath)) continue
55
    walked.add(depPath)
56
    const childShouldBeBuilt = getSubgraphToBuild(graph, Object.values(node.children), nodesToBuild, walked) ||
57
      node.requiresBuild ||
58
      node.patchFile != null
59
    if (childShouldBeBuilt) {
60
      nodesToBuild.add(depPath)
61
      currentShouldBeBuilt = true
62
    }
63
  }
64
  return currentShouldBeBuilt
65
}
66

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.