Skip to content

Commit 47745a6

Browse files
authored
Add cc init command to create changelog from scratch (#91)
Closes #85
1 parent 1532ce7 commit 47745a6

5 files changed

Lines changed: 26 additions & 5 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Add release(s) to `CHANGELOG.md` and populate it with commits. If no `CHANGELOG.
177177
- The `prerelease` type works the same as `prepatch` if the current version is a non-prerelease. If the current is already a prerelease then it's simply incremented (for example `4.0.0-rc.2` to `4.0.0-rc.3`).
178178
- A [semver-valid](https://semver.org/) version like 2.4.0.
179179

180-
If the (resulting) version is greater than the current version then commits will be taken from the semver-latest tag until HEAD. I.e. documenting a new release before it's git-tagged. If the version matches an existing tag then a release will be inserted at the appriopriate place, populated with commits between that version's tag and the one before it. I.e. documenting a past release after it's git-tagged.
180+
If the (resulting) version is greater than the current version then commits will be taken from the semver-latest tag until HEAD. I.e. documenting a new release before it's git-tagged. If the version matches an existing tag then a release will be inserted at the appriopriate place, populated with commits between that version's tag and the one before it. I.e. documenting a past release after it's git-tagged. If the version equals `0.0.1` or `1.0.0` and zero versions exist, then a [notice](https://common-changelog.org/#23notice) will be inserted (rather than commits) containing the text `:seedling: Initial release.`.
181181

182182
Additional options for this command:
183183

@@ -189,6 +189,16 @@ Works best on a linear git history. If `hallmark` encounters other tags in the c
189189

190190
The `cc add` command also fixes markdown (both existing content and generated content) but only in `CHANGELOG.md`. After you tweak the release following [Common Changelog](https://common-changelog.org) you may want to run `hallmark fix`.
191191

192+
#### `cc init`
193+
194+
Create a `CHANGELOG.md` from scratch. Inserts releases for every (semver-valid) git tag and then populates them with commits. If no git tags exist then the resulting `CHANGELOG.md` will merely have a `# Changelog` heading, without releases.
195+
196+
Additional options for this command:
197+
198+
- `--no-commits`: create empty releases
199+
- `--gte <version>`: only include versions greater than or equal to this version
200+
- `--lte <version>`: only include versions less than or equal to this version.
201+
192202
## Package Options
193203

194204
You can add a `hallmark` object to your `package.json` with additional configuration. For example:

USAGE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Commands:
77
lint [file...] Lint markdown files (default)
88
fix [file...] Fix markdown files
99
cc add <target...> Add release(s) to CHANGELOG.md.
10+
cc init Create a CHANGELOG.md from scratch. Inserts releases
11+
for all versions unless --gte or --lte are provided.
1012

1113
Arguments:
1214
file By default hallmark includes files matching "*.md".
@@ -23,6 +25,8 @@ Options:
2325
--[no-]color Force color in report (detected by default)
2426
--fix Backwards-compatible alias for fix command
2527
--no-commits Don't populate release with commits
28+
--gte <version> Only include versions greater than or equal to this
29+
--lte <version> Only include versions less than or equal to this
2630

2731
Examples:
2832
# Lint *.md files

cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ if (argv.help) {
4242
const targets = rest.slice(2)
4343
if (!targets.length || !targets.every(Boolean)) usage(1)
4444
hallmark.cc.add(targets, { ...options, commits }, done)
45+
} else if (rest[1] === 'init') {
46+
const { gte, lte, ...rest } = options
47+
hallmark.cc.add({ gte, lte }, { ...rest, commits }, done)
4548
} else {
4649
console.error('Error: unknown command.')
4750
usage(1)

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ export const cc = {
170170
if (!target.every(t => typeof t === 'string' && t.trim() !== '')) {
171171
throw new TypeError('First argument "target" must be a string or array of strings')
172172
}
173+
} else if (typeof target === 'object' && target !== null) {
174+
// Range
173175
} else if (typeof target !== 'string') {
174176
throw new TypeError('First argument "target" must be a string or array of strings')
175177
}
@@ -181,11 +183,13 @@ export const cc = {
181183
options = {}
182184
}
183185

184-
if (!fs.existsSync(path.join(options.cwd || '.', 'CHANGELOG.md'))) {
185-
fs.writeFileSync(path.join(options.cwd || '.', 'CHANGELOG.md'), '')
186+
const files = ['CHANGELOG.md']
187+
const fp = path.join(options.cwd || '.', files[0])
188+
189+
if (!fs.existsSync(fp)) {
190+
fs.writeFileSync(fp, '# Changelog\n')
186191
}
187192

188-
const files = ['CHANGELOG.md']
189193
const changelog = {
190194
commits: options.commits !== false,
191195
...options.changelog,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"remark": "^14.0.1",
2828
"remark-autolink-references": "^2.0.0",
2929
"remark-collapse": "~0.1.2",
30-
"remark-common-changelog": "^2.0.0",
30+
"remark-common-changelog": "^2.1.0",
3131
"remark-gfm": "^3.0.1",
3232
"remark-github": "^11.2.2",
3333
"remark-lint": "^9.1.0",

0 commit comments

Comments
 (0)