|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const program = require('commander') |
| 6 | +const allVersions = require('../../lib/all-versions') |
| 7 | +const graphqlDir = path.join(process.cwd(), 'lib/graphql/static') |
| 8 | + |
| 9 | +// [start-readme] |
| 10 | +// |
| 11 | +// This script creates the static GraphQL files for a new version. |
| 12 | +// |
| 13 | +// [end-readme] |
| 14 | + |
| 15 | +program |
| 16 | + .description('Create GraphQL files in lib/graphql/static based on an existing version.') |
| 17 | + .option('-n, --newVersion <version>', 'The version to copy the files to. Must be in <plan@release> format.') |
| 18 | + .option('-o, --oldVersion <version>', 'The version to copy the files from. Must be in <plan@release> format.') |
| 19 | + .parse(process.argv) |
| 20 | + |
| 21 | +const newVersion = program.newVersion |
| 22 | +const oldVersion = program.oldVersion |
| 23 | + |
| 24 | +if (!(newVersion && oldVersion)) { |
| 25 | + console.log('Error! You must provide --newVersion and --oldVersion.') |
| 26 | + process.exit(1) |
| 27 | +} |
| 28 | + |
| 29 | +if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))) { |
| 30 | + console.log('Error! You must provide the full name of a currently supported version, e.g., [email protected].') |
| 31 | + process.exit(1) |
| 32 | +} |
| 33 | + |
| 34 | +const newVersionId = allVersions[newVersion].miscVersionName |
| 35 | +const oldVersionId = allVersions[oldVersion].miscVersionName |
| 36 | + |
| 37 | +// 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 | +fs.copyFileSync(oldSchemaFile, newSchemaFile) |
| 41 | + |
| 42 | +// check that it worked |
| 43 | +if (!fs.existsSync(newSchemaFile)) { |
| 44 | + console.log(`Error! Can't find ${newSchemaFile}.`) |
| 45 | + process.exit(1) |
| 46 | +} |
| 47 | + |
| 48 | +// 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') |
| 52 | + |
| 53 | +const previews = require(previewsFile) |
| 54 | +const changes = require(changesFile) |
| 55 | +const objects = require(objectsFile) |
| 56 | + |
| 57 | +previews[newVersionId] = previews[oldVersionId] |
| 58 | +changes[newVersionId] = changes[oldVersionId] |
| 59 | +objects[newVersionId] = objects[oldVersionId] |
| 60 | + |
| 61 | +// check that it worked |
| 62 | +if (!Object.keys(previews).includes(newVersionId)) { |
| 63 | + console.log(`Error! Can't find ${newVersionId} in ${previewsFile}.`) |
| 64 | + process.exit(1) |
| 65 | +} |
| 66 | + |
| 67 | +if (!Object.keys(changes).includes(newVersionId)) { |
| 68 | + console.log(`Error! Can't find ${newVersionId} in ${changesFile}.`) |
| 69 | + process.exit(1) |
| 70 | +} |
| 71 | + |
| 72 | +if (!Object.keys(objects).includes(newVersionId)) { |
| 73 | + console.log(`Error! Can't find ${newVersionId} in ${objectsFile}.`) |
| 74 | + process.exit(1) |
| 75 | +} |
| 76 | + |
| 77 | +// write the new files |
| 78 | +fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2)) |
| 79 | +fs.writeFileSync(changesFile, JSON.stringify(changes, null, 2)) |
| 80 | +fs.writeFileSync(objectsFile, JSON.stringify(objects, null, 2)) |
| 81 | + |
| 82 | +// print success message |
| 83 | +console.log(`Done! Copied ${oldVersion} GraphQL files to ${newVersion} files.`) |
0 commit comments