check-package-boundaries.mjs
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { buildKnowledgeGraph } from './knowledge-graph/build-knowledge-graph.mjs'
const NON_RUNTIME_PREFIXES = [
'.agents/',
'.superpowers/',
'docs/',
'node_modules/',
'outputs/',
'scripts/',
'test/',
'tests/',
'typings/',
]
function normalizePath(value) {
return value?.replaceAll('\\', '/').replace(/^\.\//, '')
}
function packageOwner(filePath, subpackageRoots) {
const normalized = normalizePath(filePath)
return (
subpackageRoots.find(
root => normalized === root || normalized?.startsWith(`${root}/`),
) ?? 'main'
)
}
function isRuntimePath(node, filePath) {
const normalized = normalizePath(filePath)
return (
Boolean(normalized) &&
node?.type !== 'test' &&
!NON_RUNTIME_PREFIXES.some(prefix => normalized.startsWith(prefix))
)
}
function packageEdges({ graph, subpackageRoots }) {
const nodeById = new Map(graph.nodes.map(node => [node.id, node]))
return graph.edges
.filter(edge => edge.type === 'imports' || edge.type === 'usesComponent')
.map(edge => ({
edge,
fromNode: nodeById.get(edge.from),
toNode: nodeById.get(edge.to),
fromPath: normalizePath(
nodeById.get(edge.from)?.path ?? edge.evidence?.path,
),
toPath: normalizePath(nodeById.get(edge.to)?.path),
}))
.filter(
({ fromNode, toNode, fromPath, toPath }) =>
isRuntimePath(fromNode, fromPath) && isRuntimePath(toNode, toPath),
)
.map(({ edge, fromPath, toPath }) => ({
type: edge.type,
from: edge.from,
to: edge.to,
fromPath,
toPath,
fromOwner: packageOwner(fromPath, subpackageRoots),
toOwner: packageOwner(toPath, subpackageRoots),
evidence: edge.evidence,
}))
.sort((left, right) =>
`${left.fromPath}:${left.toPath}:${left.type}`.localeCompare(
`${right.fromPath}:${right.toPath}:${right.type}`,
'en',
),
)
}
export function findIllegalPackageEdges({ graph, subpackageRoots }) {
return packageEdges({ graph, subpackageRoots }).filter(
edge =>
edge.toOwner !== 'main' &&
edge.fromOwner !== edge.toOwner,
)
}
export async function checkPackageBoundaries({ rootDir = process.cwd() } = {}) {
const absoluteRoot = path.resolve(rootDir)
const appConfig = JSON.parse(
fs.readFileSync(path.join(absoluteRoot, 'app.json'), 'utf8'),
)
const subpackageRoots = (appConfig.subpackages ?? appConfig.subPackages ?? [])
.map(item => normalizePath(item.root))
.filter(Boolean)
.sort()
const graph = await buildKnowledgeGraph({ rootDir: absoluteRoot })
const checkedEdges = packageEdges({ graph, subpackageRoots })
const illegalEdges = checkedEdges.filter(
edge => edge.toOwner !== 'main' && edge.fromOwner !== edge.toOwner,
)
return {
status: illegalEdges.length ? 'error' : 'ok',
subpackageRoots,
checkedEdges: checkedEdges.length,
illegalEdges,
}
}
const currentFile = fileURLToPath(import.meta.url)
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(currentFile)) {
const report = await checkPackageBoundaries()
console.log(JSON.stringify(report, null, 2))
if (report.illegalEdges.length) process.exitCode = 1
}