@firefoxic/utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ # MIT License
2
+
3
+ Copyright © Sergey Artemov <firefoxic.dev@gmail.com> 2024
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @firefoxic/utils
2
+
3
+ Collection of useful utilities for JavaScript and Node.js.
4
+
5
+ ## License
6
+
7
+ This project is licensed under the MIT License — see the [LICENSE.md](./LICENSE.md) file for details.
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { updateChangelog } from "../lib/updateChangelog.js"
4
+
5
+ await updateChangelog()
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ export { updateChangelog } from "./updateChangelog.js"
@@ -0,0 +1,87 @@
1
+ import { readFile, writeFile } from 'node:fs/promises'
2
+
3
+ /**
4
+ * Updates the CHANGELOG.md file with the latest version.
5
+ *
6
+ * @return {Promise<void>} - A promise that resolves when the CHANGELOG.md file is successfully updated.
7
+ * @throws {Error} - If there is an error updating the CHANGELOG.md file.
8
+ */
9
+ export async function updateChangelog () {
10
+ try {
11
+ const changelogPath = `CHANGELOG.md`
12
+ let changelogContent = await readFile(changelogPath, `utf-8`)
13
+
14
+ changelogContent = await updateContent(changelogContent)
15
+
16
+ await writeFile(changelogPath, changelogContent)
17
+ } catch (error) {
18
+ console.error(`Error updating CHANGELOG.md:`, error)
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Updates the content of the CHANGELOG.md file with the latest version.
24
+ *
25
+ * @param {string} content - The content of the CHANGELOG.md file.
26
+ * @return {Promise<string>} - The updated content of the CHANGELOG.md file.
27
+ * @throws {Error} - If the CHANGELOG.md format is invalid.
28
+ */
29
+ async function updateContent (content) {
30
+ const separator = `[Unreleased]`
31
+ const changelogParts = content.split(separator)
32
+
33
+ if (changelogParts.length !== 3) throw new Error(`Invalid CHANGELOG.md: [Unreleased] link not found.`)
34
+
35
+ const newVersion = await getNewVersion()
36
+ const currentDate = formatDate((new Date))
37
+
38
+ changelogParts[1] = `\n\n## [${newVersion}] — ${currentDate}${changelogParts[1]}`
39
+ changelogParts[2] = updateChangelogLinks(changelogParts[2], newVersion)
40
+
41
+ return changelogParts.join(separator)
42
+ }
43
+
44
+ /**
45
+ * Asynchronously reads the version number from the package.json file and returns it.
46
+ *
47
+ * @return {Promise<string>} - A Promise that resolves with the version number from the package.json file.
48
+ */
49
+ async function getNewVersion () {
50
+ const packageJson = JSON.parse(await readFile(`package.json`, `utf-8`))
51
+
52
+ return packageJson.version
53
+ }
54
+
55
+ /**
56
+ * Formats a given date into a string in the format "YYYY-MM-DD".
57
+ *
58
+ * @param {Date} date - The date to be formatted.
59
+ * @return {string} The formatted date string.
60
+ */
61
+ function formatDate (date) {
62
+ const yyyy = date.getFullYear()
63
+ const mm = String(date.getMonth() + 1).padStart(2, `0`)
64
+ const dd = String(date.getDate()).padStart(2, `0`)
65
+
66
+ return `${yyyy}–${mm}–${dd}`
67
+ }
68
+
69
+ /**
70
+ * Updates the CHANGELOG.md file by modifying the version link in the [Unreleased] section.
71
+ *
72
+ * @param {string} content - The content of the CHANGELOG.md file.
73
+ * @param {string} newVersion - The new version number to be added.
74
+ * @return {string} The modified CHANGELOG.md content.
75
+ * @throws {Error} If the [Unreleased] link syntax is incorrect.
76
+ */
77
+ function updateChangelogLinks (content, newVersion) {
78
+ const linkRegex = /(: .+\/compare\/v)([0-9.]+)(...HEAD)/
79
+ const linkMatch = content.match(linkRegex)
80
+
81
+ if (!linkMatch) throw new Error(`Invalid CHANGELOG.md: [Unreleased] link syntax is incorrect.`)
82
+
83
+ const previousVersion = linkMatch[2]
84
+ const newLink = `${linkMatch[1]}${newVersion}${linkMatch[3]}\n[${newVersion}]${linkMatch[1]}${previousVersion}...v${newVersion}`
85
+
86
+ return content.replace(linkRegex, newLink)
87
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@firefoxic/utils",
3
+ "description": "Collection of useful utilities for JavaScript and Node.js.",
4
+ "version": "0.1.0",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Sergey Artemov",
8
+ "email": "firefoxic.dev@gmail.com"
9
+ },
10
+ "homepage": "https://github.com/firefoxic/utils#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/firefoxic/utils/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git://github.com/firefoxic/utils.git"
17
+ },
18
+ "type": "module",
19
+ "exports": "./lib/index.js",
20
+ "files": [
21
+ "./lib/"
22
+ ],
23
+ "bin": {
24
+ "update-changelog": "./bin/update-changelog.js"
25
+ },
26
+ "engines": {
27
+ "node": "^18.12 || >=20.9"
28
+ },
29
+ "devDependencies": {
30
+ "@eslint/js": "^9.1.1",
31
+ "@stylistic/eslint-plugin-js": "^1.7.2",
32
+ "eslint": "^9.1.1",
33
+ "globals": "^15.0.0",
34
+ "husky": "^9.0.11"
35
+ },
36
+ "keywords": [
37
+ "javascript",
38
+ "node.js",
39
+ "prepare",
40
+ "utils"
41
+ ],
42
+ "scripts": {
43
+ "preversion": "pnpm test",
44
+ "version": "./bin/update-changelog.js && git add CHANGELOG.md",
45
+ "postversion": "pnpm publish --access=public",
46
+ "lint": "eslint",
47
+ "test": "node --test",
48
+ "pretest": "pnpm lint"
49
+ }
50
+ }