-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
workspace.js
130 lines (115 loc) · 3.42 KB
/
workspace.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import glob from 'fast-glob'
import { existsSync, readFileSync } from 'fs'
import path from 'path'
import yaml from 'yaml'
import { workspaceRoot } from './workspaceRoot.js'
/**
*
* @param {{ dir: string, allLocalPackageNames: string[] }} param
* @returns {import('./types.js').PackageDetails | null}
*/
function getPackageDetails({ dir, allLocalPackageNames }) {
const packageJsonPath = path.join(dir, 'package.json')
if (!existsSync(packageJsonPath)) {
return null
}
const packageJson = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'))
return {
name: packageJson.name,
dir,
version: packageJson.version,
scripts: packageJson.scripts ?? {},
localDeps: Object.keys(packageJson.dependencies ?? {})
.filter((dep) => allLocalPackageNames.includes(dep))
// TODO: This sort is depended upon, test it!!!
.sort(),
}
}
/**
* @type {import('./types.js').RepoDetails | null}
*/
let _repoDetails = null
/**
*
* @returns {string[]}
*/
function getWorkspaceGlobs() {
try {
const pnpmWorkspaceYamlPath = path.join(workspaceRoot, 'pnpm-workspace.yaml')
if (existsSync(pnpmWorkspaceYamlPath)) {
const workspaceConfig = yaml.parse(readFileSync(pnpmWorkspaceYamlPath, 'utf8').toString())
return workspaceConfig?.packages || []
} else {
const packageJson = JSON.parse(readFileSync(path.join(workspaceRoot, 'package.json'), 'utf8'))
return packageJson?.workspaces || []
}
} catch (e) {
return []
}
}
function getPackageJsonPaths() {
const workspaceGlobs = getWorkspaceGlobs()
const workspacePaths = workspaceGlobs.flatMap((pattern) => {
return glob.sync(path.join(workspaceRoot, pattern, 'package.json'))
})
return workspacePaths
}
/**
* @returns {import('./types.js').RepoDetails}
*/
export function getRepoDetails() {
if (_repoDetails) {
return _repoDetails
}
const packageJsonPaths = getPackageJsonPaths()
const packageJsonObjects = packageJsonPaths.map((path) => JSON.parse(readFileSync(path, 'utf8')))
const allLocalPackageNames = packageJsonObjects.map((packageJson) => packageJson.name)
const packages = Object.fromEntries(
packageJsonPaths
.map((path, i) => [
packageJsonObjects[i].name,
getPackageDetails({ dir: path.replace('/package.json', ''), allLocalPackageNames }),
])
.filter(([, result]) => result !== null),
)
_repoDetails = {
packagesByName: packages,
packagesInTopologicalOrder: topologicalSortPackages(packages),
}
return _repoDetails
}
/**
*
* @param {Record<string, import('./types.js').PackageDetails>} packages
* @returns {import('./types.js').PackageDetails[]}
*/
export function topologicalSortPackages(packages) {
/**
* @type {import('./types.js').PackageDetails[]}
*/
const sorted = []
/**
* @type {Set<string>}
*/
const visited = new Set()
/**
*
* @param {string} packageName
* @param {string[]} path
* @returns
*/
function visit(packageName, path) {
if (visited.has(packageName)) {
return
}
visited.add(packageName)
const packageDetails = packages[packageName]
if (!packageDetails) {
throw new Error(`Could not find package ${packageName}. path: ${path.join(' -> ')}`)
}
packageDetails.localDeps.forEach((dep) => visit(dep, [...path, dep]))
sorted.push(packageDetails)
}
Object.keys(packages).forEach((packageName) => visit(packageName, [packageName]))
return sorted
}