11#!/usr/bin/env node
2- import fs from 'fs'
2+ import fs from 'fs/promises '
33import path from 'path'
44import program from 'commander'
55import xMkdirp from 'mkdirp'
@@ -50,10 +50,12 @@ const oldVersionId = allVersions[oldVersion].miscVersionName
5050// copy the schema file wholesale (there are separate schema files per version)
5151const newSchemaFile = path . join ( graphqlStaticDir , `schema-${ newVersionId } .json` )
5252const oldSchemaFile = path . join ( graphqlStaticDir , `schema-${ oldVersionId } .json` )
53- fs . copyFileSync ( oldSchemaFile , newSchemaFile )
53+ await fs . copyFile ( oldSchemaFile , newSchemaFile )
5454
5555// check that it worked
56- if ( ! fs . existsSync ( newSchemaFile ) ) {
56+ try {
57+ await fs . readFile ( newSchemaFile )
58+ } catch ( e ) {
5759 console . log ( `Error! Can't find ${ newSchemaFile } .` )
5860 process . exit ( 1 )
5961}
@@ -64,10 +66,10 @@ const changesFile = path.join(graphqlStaticDir, 'upcoming-changes.json')
6466const objectsFile = path . join ( graphqlStaticDir , 'prerendered-objects.json' )
6567const inputObjectsFile = path . join ( graphqlStaticDir , 'prerendered-input-objects.json' )
6668
67- const previews = JSON . parse ( fs . readFileSync ( previewsFile ) )
68- const changes = JSON . parse ( fs . readFileSync ( changesFile ) )
69- const objects = JSON . parse ( fs . readFileSync ( objectsFile ) )
70- const inputObjects = JSON . parse ( fs . readFileSync ( inputObjectsFile ) )
69+ const previews = JSON . parse ( await fs . readFile ( previewsFile ) )
70+ const changes = JSON . parse ( await fs . readFile ( changesFile ) )
71+ const objects = JSON . parse ( await fs . readFile ( objectsFile ) )
72+ const inputObjects = JSON . parse ( await fs . readFile ( inputObjectsFile ) )
7173// The prerendered objects file for the "old version" contains hardcoded links with the old version number.
7274// We need to update those links to include the new version to prevent a test from failing.
7375const regexOldVersion = new RegExp ( oldVersion , 'gi' )
@@ -104,33 +106,35 @@ if (!Object.keys(inputObjects).includes(newVersionId)) {
104106}
105107
106108// write the new files
107- fs . writeFileSync ( previewsFile , JSON . stringify ( previews , null , 2 ) )
108- fs . writeFileSync ( changesFile , JSON . stringify ( changes , null , 2 ) )
109- fs . writeFileSync ( objectsFile , JSON . stringify ( objects , null , 2 ) )
110- fs . writeFileSync ( inputObjectsFile , JSON . stringify ( inputObjects , null , 2 ) )
109+ await fs . writeFile ( previewsFile , JSON . stringify ( previews , null , 2 ) )
110+ await fs . writeFile ( changesFile , JSON . stringify ( changes , null , 2 ) )
111+ await fs . writeFile ( objectsFile , JSON . stringify ( objects , null , 2 ) )
112+ await fs . writeFile ( inputObjectsFile , JSON . stringify ( inputObjects , null , 2 ) )
111113
112114// now create the new version directory in data/graphql
113115const srcDir = path . join ( graphqlDataDir , oldVersionId )
114116const destDir = path . join ( graphqlDataDir , newVersionId )
115117mkdirp ( destDir )
116118
117119// copy the files
118- fs . readdirSync ( srcDir ) . forEach ( ( file ) => {
120+ const files = await fs . readdir ( srcDir )
121+ for ( const file of files ) {
119122 const srcFile = path . join ( srcDir , file )
120123 const destFile = path . join ( destDir , file )
121- fs . copyFileSync ( srcFile , destFile )
122- } )
124+ await fs . copyFile ( srcFile , destFile )
125+ }
123126
124127// check that it worked
125- if ( ! fs . existsSync ( destDir ) ) {
128+ try {
129+ const destDirResult = await fs . readdir ( destDir )
130+ if ( ! destDirResult . length ) {
131+ console . log ( `Error! The directory created at ${ destDir } is empty.` )
132+ process . exit ( 1 )
133+ }
134+ } catch ( e ) {
126135 console . log ( `Error! A new directory was not successfully created at ${ destDir } .` )
127136 process . exit ( 1 )
128137}
129138
130- if ( ! fs . readdirSync ( destDir ) . length ) {
131- console . log ( `Error! The directory created at ${ destDir } is empty.` )
132- process . exit ( 1 )
133- }
134-
135139// print success message
136140console . log ( `Done! Copied ${ oldVersion } GraphQL files to ${ newVersion } files.` )
0 commit comments