forked from twilio/twilio-cli-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b372756
commit 7b5cfda
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-disable no-console */ | ||
const fs = require('fs'); | ||
const readline = require('readline'); | ||
|
||
class ChangeLogHelper { | ||
constructor(versionRegex, dateRegex, cliCoreChangelogFilename, oaiChangelogFilename) { | ||
this.versionRegex = versionRegex; | ||
this.dateRegex = dateRegex; | ||
this.cliCoreChangelogFilename = cliCoreChangelogFilename; | ||
this.oaiChangelogFilename = oaiChangelogFilename; | ||
} | ||
|
||
async getFirstTwoVersions() { | ||
const versions = []; | ||
const rl = await this.getReadLiner(this.oaiChangelogFilename); | ||
for await (const line of rl) { | ||
const version = this.versionRegex.exec(line); | ||
if (version) { | ||
versions.push(version[0]); | ||
if (versions.length === 2) break; | ||
} | ||
} | ||
/* | ||
* if (versions.length === 2) { | ||
* console.log(versions); | ||
* } else { | ||
* console.log('Invalid versions'); | ||
* } | ||
*/ | ||
return versions; | ||
} | ||
|
||
async getLatestChangelogGeneratedDate() { | ||
let latestDate; | ||
const rl = await this.getReadLiner(this.cliCoreChangelogFilename); | ||
for await (const line of rl) { | ||
latestDate = this.dateRegex.exec(line); | ||
if (latestDate) { | ||
latestDate = latestDate[0]; | ||
break; | ||
} | ||
} | ||
|
||
/* | ||
* if (latestDate) { | ||
* console.log(`Detected latest date: ${latestDate}`); | ||
* } else { | ||
* console.log('Did not detect any dates'); | ||
* } | ||
*/ | ||
|
||
return latestDate; | ||
} | ||
|
||
async getChangesAfterGivenDate(date) { | ||
let readLines = false; | ||
let fileData = ''; | ||
const rl = await this.getReadLiner(this.oaiChangelogFilename); | ||
for await (const line of rl) { | ||
const currentDate = this.dateRegex.exec(line); | ||
if (currentDate) { | ||
if (currentDate[0] > date) { | ||
readLines = true; | ||
} else { | ||
break; | ||
} | ||
} else if (readLines) { | ||
fileData += `${line}\n`; | ||
} | ||
} | ||
return fileData; | ||
} | ||
|
||
async appendAndGetChangesToChangelog() { | ||
const latestDate = await this.getLatestChangelogGeneratedDate(); // changes.md | ||
if (latestDate) { | ||
const changeLog = await this.getChangesAfterGivenDate(latestDate); // oai_changes.md | ||
if (changeLog) { | ||
const data = fs.readFileSync(this.cliCoreChangelogFilename); | ||
const fd = fs.openSync(this.cliCoreChangelogFilename, 'w+'); | ||
const insert = Buffer.from(changeLog); | ||
fs.writeSync(fd, insert, 0, insert.length, 0); | ||
fs.writeSync(fd, data, 0, data.length, insert.length); | ||
fs.close(fd, (err) => { | ||
if (err) throw err; | ||
}); | ||
return changeLog; | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
async getReadLiner(filename) { | ||
const fileStream = fs.createReadStream(filename); | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
}); | ||
return rl; | ||
} | ||
} | ||
module.exports = { | ||
ChangeLogHelper, | ||
}; |