<commit_before>const fs = require(`fs`)
const path = require(`path`)

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

  if (!printTypeDefinitions) {
    reporter.error(
      `\`gatsby-plugin-schema-snapshot\` needs Gatsby v2.13.55 or above.`
    )
    return
  }

  const filePath = path.resolve(options.path || `schema.gql`)

  try {
    if (fs.existsSync(filePath)) {
      reporter.info(`Reading GraphQL type definitions from ${filePath}`)
      const schema = fs.readFileSync(filePath, { encoding: `utf-8` })

      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
    )
  }
}<commit_msg>fix(plugin-schema-snapshot): unlink file on init (#34527)<commit_after>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

  if (!printTypeDefinitions) {
    reporter.error(
      `\`gatsby-plugin-schema-snapshot\` needs Gatsby v2.13.55 or above.`
    )
    return
  }

  const filePath = path.resolve(options.path || `schema.gql`)

  try {
    if (fs.existsSync(filePath)) {
      reporter.info(`Reading GraphQL type definitions from ${filePath}`)
      const schema = fs.readFileSync(filePath, { encoding: `utf-8` })

      createTypes(schema, { name: `default-site-plugin` })

      if (options.update) {
        printTypeDefinitions(options)
      }
    } else {
      printTypeDefinitions(options)
    }
  } catch (error) {
    reporter.error(
      `The plugin \`gatsby-plugin-schema-snapshot\` encountered an error`,
      error
    )
  }
}
{'fraction_non_alphanumeric': 0.13939651508712284, 'fraction_numerical': 0.007224819379515512, 'mean_word_length': 3.030821917808219, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6609527', 'n_tokens_mistral': 758, 'n_tokens_neox': 744, 'n_words': 151}