keep-a-changelog
Advanced tools
Comparing version 0.2.0 to 0.3.1
69
bin.js
@@ -5,8 +5,71 @@ #!/usr/bin/env node | ||
const path = require('path'); | ||
const url = require('url'); | ||
const { parser } = require('./src'); | ||
const argv = require('yargs-parser')(process.argv.slice(2), { | ||
default: { | ||
file: 'CHANGELOG.md', | ||
url: null, | ||
https: true | ||
}, | ||
boolean: ['https'] | ||
}); | ||
const file = path.join(process.cwd(), 'CHANGELOG.md'); | ||
const file = path.join(process.cwd(), argv.file); | ||
const changelog = parser(fs.readFileSync(file, 'UTF-8')); | ||
try { | ||
const changelog = parser(fs.readFileSync(file, 'UTF-8')); | ||
fs.writeFileSync(file, changelog.toString()); | ||
if (!changelog.url && !argv.url) { | ||
const gitconfig = require('gitconfiglocal'); | ||
gitconfig(process.cwd(), (err, config) => { | ||
if (err) { | ||
console.error(red(err)); | ||
return; | ||
} | ||
changelog.url = getHttpUrl( | ||
config.remote && | ||
config.remote.origin && | ||
config.remote.origin.url | ||
); | ||
save(file, changelog); | ||
}); | ||
} else { | ||
changelog.url = getHttpUrl(argv.url || changelog.url); | ||
save(file, changelog); | ||
} | ||
} catch (err) { | ||
console.error(red(err.message)); | ||
} | ||
function getHttpUrl(remoteUrl) { | ||
if (!remoteUrl) { | ||
return; | ||
} | ||
const parsed = url.parse(remoteUrl.replace('git@', `https://`)); | ||
if (!argv.https) { | ||
parsed.protocol = 'http'; | ||
} | ||
parsed.pathname = parsed.pathname | ||
.replace(/\.git$/, '') | ||
.replace(/^\/\:/, '/'); | ||
return url.format(parsed); | ||
} | ||
function save(file, changelog) { | ||
fs.writeFileSync(file, changelog.toString()); | ||
console.log(green('Updated file'), file); | ||
} | ||
function red(message) { | ||
return '\u001b[' + 31 + 'm' + message + '\u001b[' + 39 + 'm'; | ||
} | ||
function green(message) { | ||
return '\u001b[' + 32 + 'm' + message + '\u001b[' + 39 + 'm'; | ||
} |
@@ -8,2 +8,21 @@ # Changelog | ||
## [0.3.1] - 2017-12-11 | ||
### Added | ||
- CLI Api: New option `--file`, to change the filename used | ||
- CLI Api: New option `--url`, to set or change the project url | ||
- CLI Api: New option `--https`, to change the use of https in the url scheme | ||
## [0.3.0] - 2017-12-08 | ||
### Added | ||
- Added colors in CLI | ||
- Parser errors contains the line number in the CHANGELOG file | ||
### Fixed | ||
- Fixed parsing bug when the changelog is using incorrect title levels | ||
## [0.2.0] - 2017-12-07 | ||
@@ -20,2 +39,6 @@ | ||
### Fixed | ||
- The last version should't have diff link | ||
## 0.1.0 - 2017-12-07 | ||
@@ -25,2 +48,4 @@ | ||
[0.3.1]: https://github.com/oscarotero/keep-a-changelog/compare/v0.3.0...v0.3.1 | ||
[0.3.0]: https://github.com/oscarotero/keep-a-changelog/compare/v0.2.0...v0.3.0 | ||
[0.2.0]: https://github.com/oscarotero/keep-a-changelog/compare/v0.1.0...v0.2.0 |
const fs = require('fs'); | ||
const { Changelog, Release, Change } = require('./src'); | ||
const { Changelog, Release } = require('./src'); | ||
const file = __dirname + '/CHANGELOG.md'; | ||
const changelog = new Changelog('Changelog') | ||
.addRelease( | ||
new Release('0.1.0', '2017-12-07', 'First version') | ||
) | ||
.addRelease( | ||
new Release('0.2.0', '2017-12-07') | ||
.changed('Parser improvements') | ||
.changed('Changed the constructor arguments of Changelog, Change and Release classes') | ||
.removed('Removed static factories. Use `new` instead.') | ||
.fixed('The last version should\'t have diff link') | ||
); | ||
.addRelease( | ||
new Release('0.1.0', '2017-12-07', 'First version') | ||
) | ||
.addRelease( | ||
new Release('0.2.0', '2017-12-07') | ||
.changed('Parser improvements') | ||
.changed('Changed the constructor arguments of Changelog, Change and Release classes') | ||
.removed('Removed static factories. Use `new` instead.') | ||
.fixed('The last version should\'t have diff link') | ||
) | ||
.addRelease( | ||
new Release('0.3.0', '2017-12-08') | ||
.added('Added colors in CLI') | ||
.added('Parser errors contains the line number in the CHANGELOG file') | ||
.fixed('Fixed parsing bug when the changelog is using incorrect title levels') | ||
) | ||
.addRelease( | ||
new Release('0.3.1', '2017-12-11') | ||
.added('CLI Api: New option `--file`, to change the filename used') | ||
.added('CLI Api: New option `--url`, to set or change the project url') | ||
.added('CLI Api: New option `--https`, to change the use of https in the url scheme') | ||
) | ||
@@ -17,0 +29,0 @@ changelog.url = 'https://github.com/oscarotero/keep-a-changelog'; |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/oscarotero/keep-a-changelog#readme", | ||
"version": "0.2.0", | ||
"version": "0.3.1", | ||
"main": "src/index.js", | ||
@@ -29,3 +29,5 @@ "bin": { | ||
"dependencies": { | ||
"semver": "^5.4.1" | ||
"gitconfiglocal": "^2.0.1", | ||
"semver": "^5.4.1", | ||
"yargs-parser": "^8.0.0" | ||
}, | ||
@@ -37,5 +39,6 @@ "devDependencies": { | ||
"scripts": { | ||
"prettier": "prettier src/*.js test/*.js --single-quote --tab-width 4 --write", | ||
"test": "mocha test" | ||
"prettier": "prettier src/*.js test/*.js bin.js --single-quote --tab-width 4 --write", | ||
"test": "mocha test", | ||
"changelog": "node example.js" | ||
} | ||
} |
@@ -5,4 +5,12 @@ # keep-a-changelog | ||
## Usage: | ||
## Install | ||
You can install it from the [npm repository](https://www.npmjs.com/package/keep-a-changelog) using npm/yarn: | ||
```sh | ||
npm install keep-a-changelog | ||
``` | ||
## Usage | ||
```js | ||
@@ -43,2 +51,20 @@ const { parse } = require('keep-a-changelog'); | ||
This library provides the `changelog` command to normalize the changelog format. | ||
This library provides the `changelog` command to normalize the changelog format. It reads the CHANGELOG.md file and override it with the new format: | ||
```sh | ||
changelog | ||
``` | ||
To use other file name: | ||
```sh | ||
changelog --file=History.md | ||
``` | ||
Available options: | ||
Option | Description | ||
-------|------------- | ||
`--file` | The markdown file of the changelog. The default value is `CHANGELOG.md`. | ||
`--url` | The base url used to build the diff urls of the different releases. It is taken from the existing diff urls in the markdown. If no urls are found, try to catch it using the url of the git remote repository. | ||
`--https` | Set to false to use `http` instead `https` in the url (`--https=false`). |
@@ -7,3 +7,16 @@ const Changelog = require('./Changelog'); | ||
const lines = markdown.trim().split('\n'); | ||
const totalLines = lines.length; | ||
try { | ||
return parseLines(lines); | ||
} catch (error) { | ||
throw new Error( | ||
`Parse error in the line ${totalLines - lines.length}: ${ | ||
error.message | ||
}` | ||
); | ||
} | ||
}; | ||
function parseLines(lines) { | ||
const changelog = new Changelog(); | ||
@@ -39,7 +52,7 @@ | ||
} else { | ||
throw new Error(`Syntax error in the release title\n${line}`); | ||
throw new Error(`Syntax error in the release title "${line}"`); | ||
} | ||
//Release description | ||
release.description = getUntil(lines, '### ', '['); | ||
release.description = getUntil(lines, '## ', '### ', '['); | ||
@@ -88,8 +101,7 @@ //Release change | ||
if (lines.length) { | ||
throw new Error(`Syntax error the following line: | ||
${lines[0]}`); | ||
throw new Error(`Syntax error: "${lines[0]}"`); | ||
} | ||
return changelog; | ||
}; | ||
} | ||
@@ -96,0 +108,0 @@ function getUntil(lines, ...starts) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29368
421
69
3
+ Addedgitconfiglocal@^2.0.1
+ Addedyargs-parser@^8.0.0
+ Addedcamelcase@4.1.0(transitive)
+ Addedgitconfiglocal@2.1.0(transitive)
+ Addedini@1.3.8(transitive)
+ Addedyargs-parser@8.1.0(transitive)