|  | 
|  | 1 | +const fs = require('fs'); | 
|  | 2 | +const readline = require('readline'); | 
|  | 3 | + | 
|  | 4 | +const { logger } = require('../../src/services/messaging/logging'); | 
|  | 5 | + | 
|  | 6 | +const defaultVersionRegex = /(\d+)\.(\d+)\.(\d+)/; | 
|  | 7 | +const defaultDateRegex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/; | 
|  | 8 | +const cliCoreChangelogFile = 'CHANGES.md'; | 
|  | 9 | +const oaiChangelogFile = 'OAI_CHANGES.md'; | 
|  | 10 | + | 
|  | 11 | +class ChangeLogHelper { | 
|  | 12 | +  constructor( | 
|  | 13 | +    cliCoreChangelogFilename = cliCoreChangelogFile, | 
|  | 14 | +    oaiChangelogFilename = oaiChangelogFile, | 
|  | 15 | +    versionRegex = defaultVersionRegex, | 
|  | 16 | +    dateRegex = defaultDateRegex, | 
|  | 17 | +  ) { | 
|  | 18 | +    this.versionRegex = versionRegex; | 
|  | 19 | +    this.dateRegex = dateRegex; | 
|  | 20 | +    this.cliCoreChangelogFilename = cliCoreChangelogFilename; | 
|  | 21 | +    this.oaiChangelogFilename = oaiChangelogFilename; | 
|  | 22 | +    this.logger = logger; | 
|  | 23 | +  } | 
|  | 24 | + | 
|  | 25 | +  async getAllReleaseVersionsFromGivenDate(date) { | 
|  | 26 | +    this.logger.info(`Started detecting the versions from the date: ${date}`); | 
|  | 27 | +    const versions = []; | 
|  | 28 | +    const readLine = await this.getReadLiner(this.oaiChangelogFilename); | 
|  | 29 | +    for await (const line of readLine) { | 
|  | 30 | +      const currentDate = this.dateRegex.exec(line); | 
|  | 31 | +      if (currentDate) { | 
|  | 32 | +        const version = this.versionRegex.exec(line); | 
|  | 33 | +        if (version) { | 
|  | 34 | +          versions.push(version[0]); | 
|  | 35 | +        } | 
|  | 36 | +        if (currentDate[0] <= date) { | 
|  | 37 | +          break; | 
|  | 38 | +        } | 
|  | 39 | +      } | 
|  | 40 | +    } | 
|  | 41 | +    this.logger.info(`Detected Versions: ${versions}`); | 
|  | 42 | +    return versions; | 
|  | 43 | +  } | 
|  | 44 | + | 
|  | 45 | +  async getLatestChangelogGeneratedDate() { | 
|  | 46 | +    this.logger.info('Started detecting the latest date in cli core changelog'); | 
|  | 47 | +    let latestDate; | 
|  | 48 | +    const readLine = await this.getReadLiner(this.cliCoreChangelogFilename); | 
|  | 49 | +    for await (const line of readLine) { | 
|  | 50 | +      latestDate = this.dateRegex.exec(line); | 
|  | 51 | +      if (latestDate) { | 
|  | 52 | +        latestDate = latestDate[0]; | 
|  | 53 | +        this.logger.info(`Detected the latest Date: ${latestDate}`); | 
|  | 54 | +        break; | 
|  | 55 | +      } | 
|  | 56 | +    } | 
|  | 57 | +    return latestDate; | 
|  | 58 | +  } | 
|  | 59 | + | 
|  | 60 | +  async getChangesAfterGivenDate(date) { | 
|  | 61 | +    this.logger.info(`Started getting the changelog from given date: ${date}`); | 
|  | 62 | +    let readLines = false; | 
|  | 63 | +    let fileData = ''; | 
|  | 64 | +    const readLine = await this.getReadLiner(this.oaiChangelogFilename); | 
|  | 65 | +    for await (const line of readLine) { | 
|  | 66 | +      const currentDate = this.dateRegex.exec(line); | 
|  | 67 | +      if (currentDate) { | 
|  | 68 | +        if (currentDate[0] > date) { | 
|  | 69 | +          this.logger.info('Reading the lines'); | 
|  | 70 | +          readLines = true; | 
|  | 71 | +        } else { | 
|  | 72 | +          this.logger.info(`Changes from OpenAPI specs: ${fileData}`); | 
|  | 73 | +          break; | 
|  | 74 | +        } | 
|  | 75 | +      } else if (readLines) { | 
|  | 76 | +        fileData += `${line}\n`; | 
|  | 77 | +      } | 
|  | 78 | +    } | 
|  | 79 | +    return fileData; | 
|  | 80 | +  } | 
|  | 81 | + | 
|  | 82 | +  async appendChangesToChangelog() { | 
|  | 83 | +    this.logger.info('Started appendChangesToChangelog'); | 
|  | 84 | +    try { | 
|  | 85 | +      const latestDate = await this.getLatestChangelogGeneratedDate(); // changes.md | 
|  | 86 | +      if (latestDate) { | 
|  | 87 | +        const changeLog = await this.getChangesAfterGivenDate(latestDate); // oai_changes.md | 
|  | 88 | +        if (changeLog) { | 
|  | 89 | +          this.logger.info('Updating the CHANGES.md'); | 
|  | 90 | +          const data = fs.readFileSync(this.cliCoreChangelogFilename); | 
|  | 91 | +          if (data.toString().includes(changeLog)) { | 
|  | 92 | +            this.logger.info(`Provided changes are already in cli core changeLog: ${changeLog}`); | 
|  | 93 | +            return; | 
|  | 94 | +          } | 
|  | 95 | +          const fd = fs.openSync(this.cliCoreChangelogFilename, 'w+'); | 
|  | 96 | +          const insert = Buffer.from(changeLog); | 
|  | 97 | +          fs.writeSync(fd, insert, 0, insert.length, 0); | 
|  | 98 | +          fs.writeSync(fd, data, 0, data.length, insert.length); | 
|  | 99 | +          fs.close(fd, (err) => { | 
|  | 100 | +            if (err) throw err; | 
|  | 101 | +          }); | 
|  | 102 | +          fs.writeFileSync('changeLog.md', changeLog); | 
|  | 103 | +        } | 
|  | 104 | +      } | 
|  | 105 | +    } catch (error) { | 
|  | 106 | +      this.logger.error(`Error while updating the changelog: ${error.message}`); | 
|  | 107 | +      throw new Error(error); | 
|  | 108 | +    } | 
|  | 109 | +  } | 
|  | 110 | + | 
|  | 111 | +  async getReadLiner(filename) { | 
|  | 112 | +    if (!fs.existsSync(filename)) { | 
|  | 113 | +      throw new Error(`File not found: ${filename}`); | 
|  | 114 | +    } | 
|  | 115 | +    const fileStream = fs.createReadStream(filename); | 
|  | 116 | +    return readline.createInterface({ | 
|  | 117 | +      input: fileStream, | 
|  | 118 | +    }); | 
|  | 119 | +  } | 
|  | 120 | +} | 
|  | 121 | +module.exports = { | 
|  | 122 | +  ChangeLogHelper, | 
|  | 123 | +}; | 
0 commit comments