Open History to see the last commit
1import fs from 'fs'
2import path from 'node:path'
3import { globSync } from 'node:fs'
4import { execSync } from 'node:child_process'
5β
6const rootDir = path.join(import.meta.dirname, '..')
7β
8const packagesDir = path.join(rootDir, 'packages')
9const workspacePackageRanges = Object.fromEntries(
10 globSync('*/package.json', { cwd: packagesDir }).map((p) => {
11 const pkg = JSON.parse(fs.readFileSync(path.join(packagesDir, p), 'utf-8'))
12 return [pkg.name, `^${pkg.version}`]
13 }),
14)
15β
16const catalog = JSON.parse(
17 execSync('pnpm config get catalog --json', {
18 cwd: rootDir,
19 encoding: 'utf-8',
20 }),
21)
22const catalogDependencies = ['@tanstack/react-query', 'redaxios', 'vitest']
23const catalogPackageRanges = Object.fromEntries(
24 catalogDependencies.map((name) => {
25 const range = catalog[name]
26 if (!range) {
27 throw new Error(`Missing catalog entry for ${name}`)
28 }
29 return [name, range]
30 }),
31)
32const exampleDependencyRanges = {
33 ...workspacePackageRanges,
34 ...catalogPackageRanges,
35}
36β
37const examplesDir = path.join(rootDir, 'examples')
38const examplePkgPaths = globSync('**/package.json', {
39 cwd: examplesDir,
40 exclude: (p) => p.includes('node_modules'),
41})
42β
43let updatedCount = 0
44β
45for (const relPath of examplePkgPaths) {
46 const fullPath = path.join(examplesDir, relPath)
47 const content = fs.readFileSync(fullPath, 'utf-8')
48 const pkg = JSON.parse(content)
49β
50 let changed = false
51 for (const depType of ['dependencies', 'devDependencies']) {
52 const deps = pkg[depType]
53 if (!deps) {
54 continue
55 }
56 for (const [name, range] of Object.entries(deps)) {
57 const expectedRange = exampleDependencyRanges[name]
58 if (expectedRange && range !== expectedRange) {
59 deps[name] = expectedRange
60 changed = true
61 }
62 }
63 }
64β
65 if (changed) {
66 fs.writeFileSync(fullPath, JSON.stringify(pkg, null, 2) + '\n')
67 updatedCount++
68 console.log(`Updated ${relPath}`)
69 }
70}
71β
72console.log(`\nDone. Updated ${updatedCount} example(s).`)