Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cc init command to create changelog from scratch #91

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Add release(s) to `CHANGELOG.md` and populate it with commits. If no `CHANGELOG.
- 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`).
- A [semver-valid](https://semver.org/) version like 2.4.0.

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 (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.`.

Additional options for this command:

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

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`.

#### `cc init`

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.

Additional options for this command:

- `--no-commits`: create empty releases
- `--gte <version>`: only include versions greater than or equal to this version
- `--lte <version>`: only include versions less than or equal to this version.

## Package Options

You can add a `hallmark` object to your `package.json` with additional configuration. For example:
Expand Down
4 changes: 4 additions & 0 deletions USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Commands:
lint [file...] Lint markdown files (default)
fix [file...] Fix markdown files
cc add <target...> Add release(s) to CHANGELOG.md.
cc init Create a CHANGELOG.md from scratch. Inserts releases
for all versions unless --gte or --lte are provided.

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

Examples:
# Lint *.md files
Expand Down
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ if (argv.help) {
const targets = rest.slice(2)
if (!targets.length || !targets.every(Boolean)) usage(1)
hallmark.cc.add(targets, { ...options, commits }, done)
} else if (rest[1] === 'init') {
const { gte, lte, ...rest } = options
hallmark.cc.add({ gte, lte }, { ...rest, commits }, done)
} else {
console.error('Error: unknown command.')
usage(1)
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export const cc = {
if (!target.every(t => typeof t === 'string' && t.trim() !== '')) {
throw new TypeError('First argument "target" must be a string or array of strings')
}
} else if (typeof target === 'object' && target !== null) {
// Range
} else if (typeof target !== 'string') {
throw new TypeError('First argument "target" must be a string or array of strings')
}
Expand All @@ -181,11 +183,13 @@ export const cc = {
options = {}
}

if (!fs.existsSync(path.join(options.cwd || '.', 'CHANGELOG.md'))) {
fs.writeFileSync(path.join(options.cwd || '.', 'CHANGELOG.md'), '')
const files = ['CHANGELOG.md']
const fp = path.join(options.cwd || '.', files[0])

if (!fs.existsSync(fp)) {
fs.writeFileSync(fp, '# Changelog\n')
}

const files = ['CHANGELOG.md']
const changelog = {
commits: options.commits !== false,
...options.changelog,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"remark": "^14.0.1",
"remark-autolink-references": "^2.0.0",
"remark-collapse": "~0.1.2",
"remark-common-changelog": "^2.0.0",
"remark-common-changelog": "^2.1.0",
"remark-gfm": "^3.0.1",
"remark-github": "^11.2.2",
"remark-lint": "^9.1.0",
Expand Down