Comparing version 1.0.2 to 1.1.0
#!/usr/bin/env node | ||
const { program } = require('commander'); | ||
const {program} = require('commander'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const pkg = fs.readJsonSync(path.join(__dirname, '../package.json')); | ||
const main = require('../index'); | ||
const changelog = require('../lib/changelog'); | ||
const getChanges = require('../lib/changes'); | ||
@@ -16,11 +17,14 @@ program.version(pkg.version); | ||
.option('-i, --increment <string>', 'Auto-increment version number with major, minor, or patch.') | ||
.option('-c, --changes <string>', 'Get changelog for given version number.') | ||
.option('-n, --new-version <semver>', 'New version.'); | ||
program.parse(process.argv); | ||
program.parse(); | ||
if (!program.date) { | ||
program.date = new Date(); | ||
const options = program.opts(); | ||
if (!options.date) { | ||
options.date = new Date(); | ||
} | ||
if (program.increment && program.newVersion) { | ||
if (options.increment && options.newVersion) { | ||
console.error('Options -i, --increment and -n, --new-version cannot be used together.'); | ||
@@ -30,25 +34,39 @@ process.exit(1); | ||
if (! (program.increment || program.newVersion)) { | ||
console.error('Please provide an increment via -i, --increment or a new version via -n, --new-version.'); | ||
if (!(options.increment || options.newVersion || options.changes)) { | ||
console.error('Please provide'); | ||
console.error(' - an increment via -i, --increment'); | ||
console.error(' - a new version via -n, --new-version, or'); | ||
console.error(' - an existing version via -c, --changes.'); | ||
process.exit(1); | ||
} | ||
if (!path.isAbsolute(program.file)) { | ||
program.file = path.join(process.cwd(), program.file); | ||
if (!path.isAbsolute(options.file)) { | ||
options.file = path.join(process.cwd(), options.file); | ||
} | ||
const changelogStr = fs.readFileSync(program.file, 'utf-8'); | ||
const changelogStr = fs.readFileSync(options.file, 'utf-8'); | ||
main(changelogStr, { | ||
increment: program.increment, | ||
newVersion: program.newVersion | ||
}).then(updateChangelog => { | ||
if (program.dryRun) { | ||
console.log(updateChangelog); | ||
} else { | ||
fs.writeFile(program.file, updateChangelog); | ||
main(); | ||
async function main() { | ||
try { | ||
if (options.changes) { | ||
const changes = getChanges(changelogStr, options.changes); | ||
console.log(changes); | ||
} else { | ||
const updateChangelog = await changelog(changelogStr, { | ||
increment: options.increment, | ||
newVersion: options.newVersion | ||
}); | ||
if (options.dryRun) { | ||
console.log(updateChangelog); | ||
} else { | ||
fs.writeFile(options.file, updateChangelog); | ||
} | ||
} | ||
} catch (err) { | ||
console.error(err.message); | ||
process.exit(1); | ||
} | ||
}).catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
} |
@@ -10,2 +10,10 @@ # Changelog | ||
## [1.1.0] - 2022-03-22 | ||
### Added | ||
- Get changelogs for a tag (see [issue 13](https://github.com/pheyvaer/changefrog/issues/13)) | ||
### Changed | ||
- Update dev dependencies | ||
## [1.0.2] - 2020-04-16 | ||
@@ -26,4 +34,5 @@ | ||
[1.1.0]: https://github.com/pheyvaer/changefrog/compare/v1.0.2...v1.1.0 | ||
[1.0.2]: https://github.com/pheyvaer/changefrog/compare/v1.0.1...v1.0.2 | ||
[1.0.1]: https://github.com/pheyvaer/changefrog/compare/v1.0.0...v1.0.1 | ||
[1.0.0]: https://github.com/pheyvaer/changefrog/releases/tag/v1.0.0 |
86
index.js
@@ -1,82 +0,4 @@ | ||
const semver = require('semver'); | ||
async function main(changelogStr, options) { | ||
let date = options.date; | ||
let newVersion = options.newVersion; | ||
let increment = options.increment; | ||
const lines = changelogStr.split('\n'); | ||
if (!newVersion) { | ||
if (!increment) { | ||
throw new Error(`Neither "increment" nor "newVersion" are provided.`); | ||
} | ||
const latestVersion = _findLatestVersion(lines); | ||
newVersion = semver.inc(latestVersion, increment); | ||
} | ||
if (!date) { | ||
date = new Date(); | ||
} | ||
date = _formatDate(date); | ||
let temp = changelogStr.replace('## Unreleased', `## Unreleased\n\n## [${newVersion}] - ${date}`); | ||
let i = 0; | ||
while (i < lines.length && !lines[i].startsWith('[') && !lines[i].includes(']: ')) { | ||
i ++; | ||
} | ||
const oldVersionUrl = lines[i]; | ||
let url = oldVersionUrl.split(' ')[1]; | ||
let oldVersion = 'v' + oldVersionUrl.split(' ')[0].replace('[', '').replace(']:', ''); | ||
url = url.replace(/\/v[^v]*\.\.\.v.*/, `/${oldVersion}...v${newVersion}`); | ||
temp = temp.replace(oldVersionUrl, `[${newVersion}]: ${url}\n${lines[i]}`); | ||
return temp; | ||
} | ||
/** | ||
* This method returns a formatted string of the provided date. | ||
* @param date The date to be used. | ||
* @returns {string} The formatted string. | ||
* @private | ||
*/ | ||
function _formatDate(date) { | ||
let month = '' + (date.getMonth() + 1); | ||
if (month.length === 1) { | ||
month = '0' + month; | ||
} | ||
let day = '' + date.getUTCDate(); | ||
if (day.length === 1) { | ||
day = '0' + day; | ||
} | ||
return `${date.getFullYear()}-${month}-${day}`; | ||
} | ||
function _findLatestVersion(lines) { | ||
let i = 0; | ||
while (i < lines.length && !lines[i].startsWith('[') && !lines[i].includes('] - ')) { | ||
i ++; | ||
} | ||
if (i < lines.length) { | ||
const elements = lines[i].split(' '); | ||
return elements[1].replace(/\[|\]/g, ''); | ||
} else { | ||
return null; | ||
} | ||
} | ||
module.exports = main; | ||
module.exports = { | ||
changelog: require('./lib/changelog'), | ||
getChanges: require('./lib/changes') | ||
} |
{ | ||
"name": "changefrog", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Automatically update changelog for next release.", | ||
@@ -26,4 +26,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"changelog-parser": "^2.8.0", | ||
"commander": "^5.0.0", | ||
"commander": "^9.1.0", | ||
"fs-extra": "^9.0.0", | ||
@@ -33,4 +32,4 @@ "semver": "^7.2.1" | ||
"devDependencies": { | ||
"mocha": "^7.1.1" | ||
"mocha": "^9.2.2" | ||
} | ||
} |
const assert = require('assert'); | ||
const main = require('../index'); | ||
const changelog = require('../lib/changelog'); | ||
const getChanges = require('../lib/changes'); | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
describe('Changefrog', function() { | ||
describe('Update changelog', function() { | ||
it('01', async function() { | ||
@@ -13,4 +14,4 @@ const inputFile = path.join(__dirname, '01/input.md'); | ||
const actual = await main(inputStr, {increment: 'major', date: new Date('2020-04-10')}); | ||
assert.equal(expected, actual); | ||
const actual = await changelog(inputStr, {increment: 'major', date: new Date('2020-04-10')}); | ||
assert.strictEqual(expected, actual); | ||
}); | ||
@@ -24,5 +25,52 @@ | ||
const actual = await main(inputStr, {increment: 'major', date: new Date('2020-04-10')}); | ||
assert.equal(expected, actual); | ||
const actual = await changelog(inputStr, {increment: 'major', date: new Date('2020-04-10')}); | ||
assert.strictEqual(expected, actual); | ||
}); | ||
}); | ||
describe('Get changes', function() { | ||
it('Existing version', async function() { | ||
const inputFile = path.join(__dirname, '03/input.md'); | ||
const expectedOutputFile = path.join(__dirname, '03/expected-output.md'); | ||
const inputStr = await fs.readFile(inputFile, 'utf-8'); | ||
const expected = await fs.readFile(expectedOutputFile, 'utf-8'); | ||
const actual = getChanges(inputStr, '0.2.0'); | ||
assert.strictEqual(expected, actual); | ||
}); | ||
it('Non-existing version', async function() { | ||
const inputFile = path.join(__dirname, '04/input.md'); | ||
const inputStr = await fs.readFile(inputFile, 'utf-8'); | ||
let error; | ||
try { | ||
getChanges(inputStr, '0.2.1'); | ||
} catch (e) { | ||
error = e; | ||
} finally { | ||
assert.strictEqual(`Version 0.2.1 was not found.`, error.message); | ||
} | ||
}); | ||
it('Existing version at top of file', async function() { | ||
const inputFile = path.join(__dirname, '05/input.md'); | ||
const expectedOutputFile = path.join(__dirname, '05/expected-output.md'); | ||
const inputStr = await fs.readFile(inputFile, 'utf-8'); | ||
const expected = await fs.readFile(expectedOutputFile, 'utf-8'); | ||
const actual = getChanges(inputStr, '1.0.0'); | ||
assert.strictEqual(expected, actual); | ||
}); | ||
it('Existing version at bottom of file', async function() { | ||
const inputFile = path.join(__dirname, '06/input.md'); | ||
const expectedOutputFile = path.join(__dirname, '06/expected-output.md'); | ||
const inputStr = await fs.readFile(inputFile, 'utf-8'); | ||
const expected = await fs.readFile(expectedOutputFile, 'utf-8'); | ||
const actual = getChanges(inputStr, '0.1.6'); | ||
assert.strictEqual(expected, actual); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14622
3
20
212
1
+ Addedcommander@9.5.0(transitive)
- Removedchangelog-parser@^2.8.0
- Removedchangelog-parser@2.8.1(transitive)
- Removedcommander@5.1.0(transitive)
- Removedline-reader@0.2.4(transitive)
- Removedremove-markdown@0.2.2(transitive)
Updatedcommander@^9.1.0