3
3
const fs = require ( 'fs' )
4
4
const path = require ( 'path' )
5
5
const program = require ( 'commander' )
6
+ const mkdirp = require ( 'mkdirp' ) . sync
6
7
const 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' )
8
10
9
11
// [start-readme]
10
12
//
@@ -35,8 +37,8 @@ const newVersionId = allVersions[newVersion].miscVersionName
35
37
const oldVersionId = allVersions [ oldVersion ] . miscVersionName
36
38
37
39
// 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` )
40
42
fs . copyFileSync ( oldSchemaFile , newSchemaFile )
41
43
42
44
// check that it worked
@@ -46,9 +48,9 @@ if (!fs.existsSync(newSchemaFile)) {
46
48
}
47
49
48
50
// 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' )
52
54
53
55
const previews = require ( previewsFile )
54
56
const changes = require ( changesFile )
@@ -79,5 +81,28 @@ fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2))
79
81
fs . writeFileSync ( changesFile , JSON . stringify ( changes , null , 2 ) )
80
82
fs . writeFileSync ( objectsFile , JSON . stringify ( objects , null , 2 ) )
81
83
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
+
82
107
// print success message
83
108
console . log ( `Done! Copied ${ oldVersion } GraphQL files to ${ newVersion } files.` )
0 commit comments