<commit_before>import fs from 'fs'
import path from 'path'
import { workerName } from './constants'

const template = fs.readFileSync(path.join(__dirname, './template.js'), 'utf-8')

const helperSWRegister = function () {
  const registerContent = template.replace('__workerName__', workerName)
  return `<script async>${registerContent}</script>`
}

const injectSWRegisterWithContent = (script) => (publicDir) => {
  const indexHTMLPath = path.join(publicDir, 'index.html')

  // early return when no index.html presets in public directory
  if (!fs.existsSync(indexHTMLPath)) {
    return
  }

  const fileContent = fs.readFileSync(indexHTMLPath, 'utf-8')
  const injectedContent = fileContent.replace(/<\/body>\s*<\/html>\s*$/, `${script}</body></html>`)
  fs.writeFileSync(indexHTMLPath, injectedContent)
}

export default injectSWRegisterWithContent(helperSWRegister())
<commit_msg>fix(inject-sw-register): early return when injected before<commit_after>import fs from 'fs'
import path from 'path'
import { workerName } from './constants'

const template = fs.readFileSync(path.join(__dirname, './template.js'), 'utf-8')

const helperSWRegister = function () {
  const registerContent = template.replace('__workerName__', workerName)
  return `<script async>${registerContent}</script>`
}

const injectSWRegisterWithContent = (script) => (publicDir) => {
  const indexHTMLPath = path.join(publicDir, 'index.html')

  // early return when no index.html presets in public directory
  if (!fs.existsSync(indexHTMLPath)) {
    return
  }

  const fileContent = fs.readFileSync(indexHTMLPath, 'utf-8')

  // early return if it has been injected before
  if (fileContent.includes(`${script}</body></html>`)) {
    return
  }

  const injectedContent = fileContent.replace(/<\/body>\s*<\/html>\s*$/, `${script}</body></html>`)
  fs.writeFileSync(indexHTMLPath, injectedContent)
}

export default injectSWRegisterWithContent(helperSWRegister())

{'fraction_non_alphanumeric': 0.14572605561277033, 'fraction_numerical': 0.0020597322348094747, 'mean_word_length': 5.207667731629393, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '4528124', 'n_tokens_mistral': 585, 'n_tokens_neox': 573, 'n_words': 136}