utils.ts 689 B

12345678910111213141516171819
  1. import fs from 'fs'
  2. import { blake2AsHex } from '@polkadot/util-crypto'
  3. type ReplaceLinesInFileParams = {
  4. filePath: string
  5. regex: RegExp
  6. newContent: string
  7. }
  8. export function replaceInFile({ filePath, regex, newContent }: ReplaceLinesInFileParams): void {
  9. const paramsHash = blake2AsHex(filePath + '|' + regex.source + '|' + newContent)
  10. const startMark = `/* BEGIN REPLACED CONTENT ${paramsHash} */`
  11. const endMark = `/* END REPLACED CONTENT ${paramsHash} */`
  12. const fileContent = fs.readFileSync(filePath).toString()
  13. if (fileContent.includes(startMark)) {
  14. return
  15. }
  16. fs.writeFileSync(filePath, fileContent.replace(regex, `${startMark}\n${newContent}\n${endMark}`))
  17. }