Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
firefoxic committed May 9, 2024
0 parents commit 37b9102
Show file tree
Hide file tree
Showing 19 changed files with 1,085 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
indent_style = tab
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.svg]
insert_final_newline = false

[*.{yaml,yml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test project

on:
push:
branches:
- main
pull_request:
branches:
- '*'

env:
FORCE_COLOR: true

jobs:
check:
name: Test

runs-on: ubuntu-latest

steps:

- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: 'package.json'

- name: Restore cache
uses: actions/cache@v3
with:
path: ~/.local/share/pnpm/store/v3
key: cache-pnpm-${{ hashFiles('./pnpm-lock.yaml') }}
restore-keys: cache-pnpm-

- name: Install dependencies
run: corepack enable pnpm && pnpm i

- name: Run checks
run: pnpm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
git stash -ku

trap "git stash pop" EXIT

npm run lint
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
engine-strict=true
shell-emulator=true
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- markdownlint-disable MD007 MD024 -->
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]

### Added

- The basic functionality of the `update-changelog` CLI utility.

[Unreleased]: https://github.com/firefoxic/update-changelog/compare/v0.0.1...HEAD
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MIT License

Copyright © Sergey Artemov <[email protected]> 2024

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# @firefoxic/update-changelog

[![License: MIT][license-image]][license-url]
[![Changelog][changelog-image]][changelog-url]
[![NPM version][npm-image]][npm-url]
[![Test Status][test-image]][test-url]

CLI utility for automatic update of `CHANGELOG.md`.

## Purpose

Increasing the version of a package usually requires creating a commit (extra for history) with a message something like `Prepare release`. This commit should manually add a header to `CHANGELOG.md` with the new version and the release date, and change the links to the comparison at the bottom of the file.

The `update-changelog` utility gets rid of this chore, random typos, and an unnecessary commit.

## Installation

```shell
pnpm add -D @firefoxic/update-changelog
```

## Configuration

In the `scripts` section of your `package.json`, add a `version` hook that will run when `pnpm version patch` (or `minor`, or `major`) is executed after updating the version in `package.json`, but before creating the commit.

```json
{
"scripts": {
"version": "update-changelog && git add CHANGELOG.md".
},
}
```

Optionally, other hooks can be added to conveniently automate package publishing (see `package.json` of this project for an example).

## Some restrictions

The `update-changelog` expects the following:

- The name of the changelog file is `CHANGELOG.md`.
- The format of the changelog is consistent with [Keep a changelog](https://keepachangelog.com).
- Descriptions of all user-important changes are already in the changelog under the heading `[Unreleased]`. Ideally, you should commit them along with the changes themselves.
- If this is the first release of a package, there should be only one reference for [Unreleased] at the end of the changelog in the following format for correct reference updating:

```md
[Unreleased]: https://github.com/<user-name>/<project-name>/compare/v0.0.1...HEAD
```

## Usage

When publishing a new version, simply do not create the `Prepare release` commit. Running `pnpm version patch` (or `minor`, or `major`) will now do everything for you 🥳

[license-url]: https://github.com/firefoxic/update-changelog/blob/main/LICENSE.md
[license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg

[changelog-url]: https://github.com/firefoxic/update-changelog/blob/main/CHANGELOG.md
[changelog-image]: https://img.shields.io/badge/CHANGELOG-md-limegreen

[npm-url]: https://npmjs.com/package/@firefoxic/update-changelog
[npm-image]: https://badge.fury.io/js/@firefoxic%2Fupdate-changelog.svg

[test-url]: https://github.com/firefoxic/update-changelog/actions
[test-image]: https://github.com/firefoxic/update-changelog/actions/workflows/test.yml/badge.svg?branch=main
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { updateChangelog } from "../lib/updateChangelog.js"

await updateChangelog()
12 changes: 12 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { default as firefoxicEslintConfig, globals } from "@firefoxic/eslint-config"

export default [
{
languageOptions: {
globals: {
...globals.nodeBuiltin,
},
},
},
...firefoxicEslintConfig,
]
13 changes: 13 additions & 0 deletions lib/formatDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Formats a given date into a string in the format "YYYY–MM–DD".
*
* @param {Date} date - The date to be formatted.
* @return {string} The formatted date string.
*/
export function formatDate (date) {
let yyyy = date.getFullYear()
let mm = String(date.getMonth() + 1).padStart(2, `0`)
let dd = String(date.getDate()).padStart(2, `0`)

return `${yyyy}${mm}${dd}`
}
12 changes: 12 additions & 0 deletions lib/getNewVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readFile } from "node:fs/promises"

/**
* Asynchronously reads the version number from the package.json file and returns it.
*
* @return {Promise<string>} - A Promise that resolves with the version number from the package.json file.
*/
export async function getNewVersion () {
let packageJson = JSON.parse(await readFile(`package.json`, `utf-8`))

return packageJson.version
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { updateChangelog } from "./updateChangelog.js"
22 changes: 22 additions & 0 deletions lib/updateChangelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFile, writeFile } from "node:fs/promises"

import { updateContent } from "./updateContent.js"

/**
* Updates the CHANGELOG.md file with the latest version.
*
* @return {Promise<void>} - A promise that resolves when the CHANGELOG.md file is successfully updated.
* @throws {Error} - If there is an error updating the CHANGELOG.md file.
*/
export async function updateChangelog () {
try {
let changelogPath = `CHANGELOG.md`
let changelogContent = await readFile(changelogPath, `utf-8`)

changelogContent = await updateContent(changelogContent)

await writeFile(changelogPath, changelogContent)
} catch (error) {
console.error(`Error updating CHANGELOG.md:`, error)
}
}
24 changes: 24 additions & 0 deletions lib/updateChangelogLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Updates the CHANGELOG.md file by modifying the version link in the [Unreleased] section.
*
* @param {string} content - The content of the CHANGELOG.md file.
* @param {string} newVersion - The new version number to be added.
* @return {string} The modified CHANGELOG.md content.
* @throws {Error} If the [Unreleased] link syntax is incorrect.
*/
export function updateChangelogLinks (content, newVersion) {
let linkRegex = /(: .+\/compare\/v)([0-9.]+)(...HEAD)/
let linkMatch = content.match(linkRegex)

if (!linkMatch) throw new Error(`Invalid CHANGELOG.md: [Unreleased] link syntax is incorrect.`)

let previousVersion = linkMatch[2]
let existingVersionRegex = new RegExp(`\\[${previousVersion}\\]:`)
let newVersionLinkPath = content.match(existingVersionRegex)
? `${linkMatch[1]}${previousVersion}...v${newVersion}`
: `${linkMatch[1].replace(`compare`, `releases/tag`)}${newVersion}`

let newLinks = `${linkMatch[1]}${newVersion}${linkMatch[3]}\n[${newVersion}]${newVersionLinkPath}`

return content.replace(linkRegex, newLinks)
}
25 changes: 25 additions & 0 deletions lib/updateContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { formatDate } from "./formatDate.js"
import { getNewVersion } from "./getNewVersion.js"
import { updateChangelogLinks } from "./updateChangelogLinks.js"

/**
* Updates the content of the CHANGELOG.md file with the latest version.
*
* @param {string} content - The content of the CHANGELOG.md file.
* @return {Promise<string>} - The updated content of the CHANGELOG.md file.
* @throws {Error} - If the CHANGELOG.md format is invalid.
*/
export async function updateContent (content) {
let separator = `[Unreleased]`
let changelogParts = content.split(separator)

if (changelogParts.length !== 3) throw new Error(`Invalid CHANGELOG.md: [Unreleased] link not found.`)

let newVersion = await getNewVersion()
let currentDate = formatDate((new Date))

changelogParts[1] = `\n\n## [${newVersion}] — ${currentDate}${changelogParts[1]}`
changelogParts[2] = updateChangelogLinks(changelogParts[2], newVersion)

return changelogParts.join(separator)
}
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@firefoxic/update-changelog",
"description": "CLI utility for automatic update of CHANGELOG.md",
"version": "0.0.1",
"license": "MIT",
"author": {
"name": "Sergey Artemov",
"email": "[email protected]"
},
"homepage": "https://github.com/firefoxic/update-changelog#readme",
"bugs": {
"url": "https://github.com/firefoxic/update-changelog/issues"
},
"repository": {
"type": "git",
"url": "git://github.com/firefoxic/update-changelog.git"
},
"type": "module",
"exports": "./lib/index.js",
"files": [
"./lib/"
],
"bin": {
"update-changelog": "./bin/cli.js"
},
"engines": {
"node": "^18.12 || >=20.9"
},
"packageManager": "[email protected]",
"scripts": {
"prepare": "husky",
"preversion": "pnpm test",
"version": "./bin/cli.js && git add CHANGELOG.md",
"postversion": "pnpm publish --access=public",
"postpublish": "git push --follow-tags",
"lint": "eslint",
"test": "node --test",
"pretest": "pnpm lint"
},
"devDependencies": {
"@firefoxic/eslint-config": "^1.0.0",
"eslint": "^9.2.0",
"husky": "^9.0.11"
},
"keywords": [
"changelog",
"cli",
"javascript",
"node.js",
"prepare",
"update"
]
}
Loading

0 comments on commit 37b9102

Please sign in to comment.