1+ const fs = require ( 'fs' )
2+ const path = require ( 'path' )
3+ const yaml = require ( 'js-yaml' ) // You'll need to install this packageCopy
4+ const { execSync } = require ( 'child_process' )
5+
6+ // Function to read package.json and extract version
7+ function getPublicPackageVersion ( packagePath ) {
8+ const pkgPath = path . join ( packagePath , 'package.json' )
9+ if ( fs . existsSync ( pkgPath ) ) {
10+ const pkg = require ( pkgPath )
11+ return pkg . version
12+ }
13+ return null
14+ }
15+
16+ function findPackageJsonFiles ( dir ) {
17+ let results = [ ]
18+ const list = fs . readdirSync ( dir )
19+
20+ list . forEach ( file => {
21+ const filePath = path . join ( dir , file )
22+ const stat = fs . statSync ( filePath )
23+
24+ if ( stat && stat . isDirectory ( ) ) {
25+ // Recursive case: it's a directory
26+ if ( file === 'node_modules' ) {
27+ return
28+ }
29+ results = results . concat ( findPackageJsonFiles ( filePath ) )
30+ } else {
31+ // Base case: it's a file
32+ if ( file === 'package.json' ) {
33+ results . push ( filePath )
34+ }
35+ }
36+ } )
37+
38+ return results
39+ }
40+
41+ function isVersionIncreased ( oldVersion , newVersion ) {
42+ const oldParts = oldVersion . split ( '.' ) . map ( Number )
43+ const newParts = newVersion . split ( '.' ) . map ( Number )
44+
45+ for ( let i = 0 ; i < 3 ; i ++ ) {
46+ if ( newParts [ i ] > oldParts [ i ] ) return true
47+ if ( newParts [ i ] < oldParts [ i ] ) return false
48+ }
49+ return false
50+ }
51+
52+ /**
53+ * Iterate through packages directory and extract versions of public packages
54+ */
55+ const packagesDir = path . join ( process . cwd ( ) , 'packages' )
56+ const packageVersions = { }
57+ fs . readdirSync ( packagesDir ) . forEach ( packageName => {
58+ const packagePath = path . join ( packagesDir , packageName )
59+ if ( fs . statSync ( packagePath ) . isDirectory ( ) ) {
60+ const version = getPublicPackageVersion ( packagePath )
61+ if ( version ) {
62+ packageVersions [ packageName ] = version
63+ }
64+ }
65+ } )
66+
67+ /**
68+ * Get catalog versions for shared deps like prisma, typescript etc
69+ */
70+ const workspaceYaml = fs . readFileSync ( './pnpm-workspace.yaml' , 'utf8' )
71+ const workspaceConfig = yaml . load ( workspaceYaml )
72+ const catalogVersions = workspaceConfig . catalog || { }
73+
74+ /**
75+ * Update example package.json with new versions, triggering prisma generation
76+ * if @prisma/client has been updated.
77+ * @param {string } examplePath
78+ */
79+ function updateExamplePkg ( examplePath ) {
80+ const pkgPath = path . join ( examplePath , 'package.json' )
81+ const pkg = JSON . parse ( fs . readFileSync ( pkgPath , 'utf8' ) )
82+ let prismaClientUpdated = false
83+
84+ // Update workspace dependencies
85+ for ( const [ name , version ] of Object . entries ( packageVersions ) ) {
86+ if ( pkg . dependencies && pkg . dependencies [ `@keystone-6/${ name } ` ] ) {
87+ pkg . dependencies [ `@keystone-6/${ name } ` ] = `^${ version } `
88+ }
89+ }
90+
91+ // Update catalog dependencies
92+ for ( const [ name , newVersion ] of Object . entries ( catalogVersions ) ) {
93+ if ( pkg . dependencies && pkg . dependencies [ name ] ) {
94+ const oldVersion = pkg . dependencies [ name ] . replace ( 'catalog:' , '' )
95+
96+ if ( name === '@prisma/client' ) {
97+ if ( isVersionIncreased ( oldVersion , newVersion ) ) {
98+ prismaClientUpdated = true
99+ }
100+ }
101+ pkg . dependencies [ name ] = `${ newVersion } `
102+ }
103+
104+ if ( pkg . devDependencies && pkg . devDependencies [ name ] ) {
105+ pkg . devDependencies [ name ] = `${ newVersion } `
106+ }
107+ }
108+
109+ fs . writeFileSync ( pkgPath , JSON . stringify ( pkg , null , 2 ) )
110+
111+ if ( prismaClientUpdated === true ) {
112+ console . log ( `Updating GraphQL and Prisma schemas ${ examplePath } ` )
113+ try {
114+ execSync ( 'pnpm keystone build --no-ui' , { cwd : examplePath , stdio : 'inherit' } )
115+ } catch ( error ) {
116+ console . error ( `Error running command in ${ examplePath } :` , error )
117+ }
118+ }
119+ }
120+
121+ // Traverse the examples directory and update each package.json
122+ const examplesDir = path . join ( process . cwd ( ) , 'examples' )
123+ const exampleDirsToSkip = [ 'nextjs-keystone-two-servers' ]
124+ fs . readdirSync ( examplesDir ) . forEach ( example => {
125+ if ( exampleDirsToSkip . includes ( example ) ) {
126+ console . log ( `Skipping directory: ${ example } ` )
127+ return // Skip this iteration
128+ }
129+
130+ const examplePath = path . join ( examplesDir , example )
131+ if ( fs . statSync ( examplePath ) . isDirectory ( ) ) {
132+ const packageJsonFiles = findPackageJsonFiles ( examplePath )
133+ packageJsonFiles . forEach ( packageJsonPath => {
134+ updateExamplePkg ( path . dirname ( packageJsonPath ) )
135+ } )
136+ }
137+ } )
138+
139+ console . log ( 'Package versions:' , packageVersions )
140+ console . log ( 'Catalog versions:' , catalogVersions )
141+ console . log ( 'Example packages updated successfully.' )
0 commit comments