33const fs = require ( 'fs' )
44const path = require ( 'path' )
55const program = require ( 'commander' )
6+ const mkdirp = require ( 'mkdirp' ) . sync
67const allVersions = require ( '../../lib/all-versions' )
7- const graphqlDir = path . join ( process . cwd ( ) , 'lib/graphql/static' )
8+ const graphqlStaticDir = path . join ( process . cwd ( ) , 'lib/graphql/static' )
9+ const graphqlDataDir = path . join ( process . cwd ( ) , 'data/graphql' )
810
911// [start-readme]
1012//
@@ -35,8 +37,8 @@ const newVersionId = allVersions[newVersion].miscVersionName
3537const oldVersionId = allVersions [ oldVersion ] . miscVersionName
3638
3739// copy the schema file wholesale (there are separate schema files per version)
38- const newSchemaFile = path . join ( graphqlDir , `schema-${ newVersionId } .json` )
39- const oldSchemaFile = path . join ( graphqlDir , `schema-${ oldVersionId } .json` )
40+ const newSchemaFile = path . join ( graphqlStaticDir , `schema-${ newVersionId } .json` )
41+ const oldSchemaFile = path . join ( graphqlStaticDir , `schema-${ oldVersionId } .json` )
4042fs . copyFileSync ( oldSchemaFile , newSchemaFile )
4143
4244// check that it worked
@@ -46,9 +48,9 @@ if (!fs.existsSync(newSchemaFile)) {
4648}
4749
4850// the other files are objects with vers3091iuions as keys, so we need to require them
49- const previewsFile = path . join ( graphqlDir , 'previews.json' )
50- const changesFile = path . join ( graphqlDir , 'upcoming-changes.json' )
51- const objectsFile = path . join ( graphqlDir , 'prerendered-objects.json' )
51+ const previewsFile = path . join ( graphqlStaticDir , 'previews.json' )
52+ const changesFile = path . join ( graphqlStaticDir , 'upcoming-changes.json' )
53+ const objectsFile = path . join ( graphqlStaticDir , 'prerendered-objects.json' )
5254
5355const previews = require ( previewsFile )
5456const changes = require ( changesFile )
@@ -79,5 +81,28 @@ fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2))
7981fs . writeFileSync ( changesFile , JSON . stringify ( changes , null , 2 ) )
8082fs . writeFileSync ( objectsFile , JSON . stringify ( objects , null , 2 ) )
8183
84+ // now create the new version directory in data/graphql
85+ const srcDir = path . join ( graphqlDataDir , oldVersionId )
86+ const destDir = path . join ( graphqlDataDir , newVersionId )
87+ mkdirp ( destDir )
88+
89+ // copy the files
90+ fs . readdirSync ( srcDir ) . forEach ( file => {
91+ const srcFile = path . join ( srcDir , file )
92+ const destFile = path . join ( destDir , file )
93+ fs . copyFileSync ( srcFile , destFile )
94+ } )
95+
96+ // check that it worked
97+ if ( ! fs . existsSync ( destDir ) ) {
98+ console . log ( `Error! A new directory was not successfully created at ${ destDir } .` )
99+ process . exit ( 1 )
100+ }
101+
102+ if ( ! fs . readdirSync ( destDir ) . length ) {
103+ console . log ( `Error! The directory created at ${ destDir } is empty.` )
104+ process . exit ( 1 )
105+ }
106+
82107// print success message
83108console . log ( `Done! Copied ${ oldVersion } GraphQL files to ${ newVersion } files.` )
0 commit comments