Skip to content

Commit

Permalink
fix(plugin-schema-snapshot): unlink file on init (#34527)
Browse files Browse the repository at this point in the history
  • Loading branch information
njbmartin committed Jan 19, 2022
1 parent 195188c commit 42ed5ef
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/gatsby-plugin-schema-snapshot/gatsby-node.js
@@ -1,6 +1,21 @@
const fs = require(`fs`)
const path = require(`path`)

exports.onPluginInit = ({ reporter }, options = {}) => {
const filePath = path.resolve(options.path || `schema.gql`)
try {
if (fs.existsSync(filePath) && options.update) {
fs.unlinkSync(filePath)
reporter.info("Removed schema file")
}
} catch (error) {
reporter.error(
`The plugin \`gatsby-plugin-schema-snapshot\` encountered an error`,
error
)
}
}

exports.createSchemaCustomization = ({ actions, reporter }, options = {}) => {
const { createTypes, printTypeDefinitions } = actions

Expand All @@ -21,15 +36,15 @@ exports.createSchemaCustomization = ({ actions, reporter }, options = {}) => {
createTypes(schema, { name: `default-site-plugin` })

if (options.update) {
fs.unlinkSync(filePath)
printTypeDefinitions(options)
}
} else {
printTypeDefinitions(options)
}
} catch (error) {
reporter.error(
`The plugin \`gatsby-plugin-schema-snapshot\` encountered an error`, error
`The plugin \`gatsby-plugin-schema-snapshot\` encountered an error`,
error
)
}
}

0 comments on commit 42ed5ef

Please sign in to comment.