@brickflow/cli 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ import { exec } from 'child_process'
2
+ import fs from 'fs'
3
+ import path from 'path'
4
+ import { fileURLToPath } from 'url'
5
+
6
+ const args = process.argv.slice(3)
7
+
8
+ if (args.includes('--help') || args.includes('-h')) {
9
+ printHelp()
10
+ process.exit(0)
11
+ }
12
+
13
+ if (args.length > 0) {
14
+ printHelp()
15
+ process.exit(1)
16
+ }
17
+
18
+ const currentDir = path.dirname(fileURLToPath(import.meta.url))
19
+ const workspaceRoot = path.resolve(currentDir, '../../../..')
20
+ const packageJsonPaths = findPackageJsonPaths()
21
+ const localPackageNames = collectLocalBrickflowPackageNames(packageJsonPaths)
22
+
23
+ if (localPackageNames.length === 0) {
24
+ console.log('No local @brickflow/* packages found in the repository')
25
+ process.exit(0)
26
+ }
27
+
28
+ const latestVersions = new Map()
29
+
30
+ for (const packageName of localPackageNames) {
31
+ const version = await readLatestVersion(packageName)
32
+
33
+ if (version) {
34
+ latestVersions.set(packageName, version)
35
+ }
36
+ }
37
+
38
+ if (latestVersions.size === 0) {
39
+ throw new Error('Could not resolve latest versions for local @brickflow/* packages')
40
+ }
41
+
42
+ console.log('\x1b[32m', '\rBrickflow package upgrade')
43
+
44
+ let changedFiles = 0
45
+ let index = 0
46
+
47
+ for (const packageJsonPath of packageJsonPaths) {
48
+ index += 1
49
+
50
+ const packageJson = readJson(packageJsonPath)
51
+ const before = JSON.stringify(packageJson)
52
+
53
+ updateDependencySections(packageJson, latestVersions)
54
+
55
+ if (JSON.stringify(packageJson) !== before) {
56
+ writeJson(packageJsonPath, packageJson)
57
+ changedFiles += 1
58
+ console.log(`\nšŸ“¦ Updated ${getPackageLabel(packageJsonPath)}`)
59
+ }
60
+
61
+ const percent = Math.round((index * 100) / packageJsonPaths.length)
62
+ process.stdout.write(`\ršŸ”© Upgrade: [${'ā–ˆ'.repeat(Math.round(percent / 10)).padEnd(10, ' ')}] ${percent}%`)
63
+ }
64
+
65
+ process.stdout.write('\n')
66
+ console.log(`Updated ${changedFiles} package.json file(s)`)
67
+ console.log('\x1b[0m', '')
68
+
69
+ function collectLocalBrickflowPackageNames(filePaths) {
70
+ return filePaths
71
+ .map((filePath) => readJson(filePath).name)
72
+ .filter((packageName) => isBrickflowPackageName(packageName))
73
+ .sort()
74
+ }
75
+
76
+ function findPackageJsonPaths() {
77
+ return walkDirectoryForPackageJson(workspaceRoot).sort()
78
+ }
79
+
80
+ function getDependencySections(packageJson) {
81
+ return ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']
82
+ .map((sectionName) => ({
83
+ name: sectionName,
84
+ value: packageJson[sectionName],
85
+ }))
86
+ .filter((section) => section.value && typeof section.value === 'object')
87
+ }
88
+
89
+ function getPackageLabel(packageJsonPath) {
90
+ const relativePath = path.relative(workspaceRoot, packageJsonPath).replace(/\\/g, '/')
91
+ const packageDir = path.dirname(relativePath)
92
+
93
+ return packageDir === '.' ? 'root' : packageDir
94
+ }
95
+
96
+ function isBrickflowPackageName(packageName) {
97
+ return typeof packageName === 'string' && packageName.startsWith('@brickflow/')
98
+ }
99
+
100
+ function printHelp() {
101
+ console.log(`brick upgrade
102
+
103
+ Usage:
104
+ brick upgrade
105
+
106
+ Notes:
107
+ Finds local @brickflow/* package names from package.json files in the repository
108
+ Fetches the latest published version for each of those packages
109
+ Updates matching dependencies, devDependencies, peerDependencies and optionalDependencies in all package.json files`)
110
+ }
111
+
112
+ function readJson(filePath) {
113
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'))
114
+ }
115
+
116
+ function readLatestVersion(packageName) {
117
+ return new Promise((resolve) => {
118
+ exec(`npm view ${packageName} version`, (error, stdout) => {
119
+ if (error) {
120
+ console.warn(`Skipping ${packageName}: ${error.message}`)
121
+ resolve(null)
122
+ return
123
+ }
124
+
125
+ resolve(String(stdout).trim().replace('\n', ''))
126
+ })
127
+ })
128
+ }
129
+
130
+ function updateDependencySections(packageJson, latestVersionsMap) {
131
+ for (const section of getDependencySections(packageJson)) {
132
+ for (const dependencyName of Object.keys(section.value)) {
133
+ const latestVersion = latestVersionsMap.get(dependencyName)
134
+
135
+ if (latestVersion) {
136
+ section.value[dependencyName] = latestVersion
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ function writeJson(filePath, value) {
143
+ fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`)
144
+ }
145
+
146
+ function walkDirectoryForPackageJson(directoryPath) {
147
+ const entries = fs.readdirSync(directoryPath, { withFileTypes: true })
148
+ const filePaths = []
149
+
150
+ for (const entry of entries) {
151
+ if (entry.name === 'node_modules') {
152
+ continue
153
+ }
154
+
155
+ const entryPath = path.join(directoryPath, entry.name)
156
+
157
+ if (entry.isDirectory()) {
158
+ filePaths.push(...walkDirectoryForPackageJson(entryPath))
159
+ continue
160
+ }
161
+
162
+ if (entry.isFile() && entry.name === 'package.json') {
163
+ filePaths.push(entryPath)
164
+ }
165
+ }
166
+
167
+ return filePaths
168
+ }