@firefoxic/utils 0.2.0 → 0.3.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/lib/index.js CHANGED
@@ -1,3 +1 @@
1
1
  export { getProjectRoot } from "./getProjectRoot.js"
2
-
3
- export { updateChangelog } from "./updateChangelog.js"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@firefoxic/utils",
3
3
  "description": "Collection of useful utilities for JavaScript and Node.js.",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Sergey Artemov",
@@ -20,17 +20,13 @@
20
20
  "files": [
21
21
  "./lib/"
22
22
  ],
23
- "bin": {
24
- "update-changelog": "./bin/update-changelog.js"
25
- },
26
23
  "engines": {
27
24
  "node": "^18.12 || >=20.9"
28
25
  },
29
26
  "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",
27
+ "@firefoxic/eslint-config": "^1.0.0",
28
+ "@firefoxic/update-changelog": "^0.1.0",
29
+ "eslint": "^9.2.0",
34
30
  "husky": "^9.0.11"
35
31
  },
36
32
  "keywords": [
@@ -40,11 +36,11 @@
40
36
  "utils"
41
37
  ],
42
38
  "scripts": {
43
- "preversion": "pnpm test",
44
- "version": "./bin/update-changelog.js && git add CHANGELOG.md",
45
- "postversion": "pnpm publish --access=public",
46
39
  "lint": "eslint",
40
+ "pretest": "pnpm lint",
47
41
  "test": "node --test",
48
- "pretest": "pnpm lint"
42
+ "preversion": "pnpm test",
43
+ "version": "update-changelog && git add CHANGELOG.md",
44
+ "postversion": "pnpm publish --access=public"
49
45
  }
50
46
  }
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { updateChangelog } from "../lib/updateChangelog.js"
4
-
5
- await updateChangelog()
@@ -1,92 +0,0 @@
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 existingVersionRegex = new RegExp(`\\[${previousVersion}\\]:`)
85
- const newVersionLinkPath = content.match(existingVersionRegex)
86
- ? `${linkMatch[1]}${previousVersion}...v${newVersion}`
87
- : `${linkMatch[1].replace(`compare`, `releases/tag`)}${newVersion}`
88
-
89
- const newLinks = `${linkMatch[1]}${newVersion}${linkMatch[3]}\n[${newVersion}]${newVersionLinkPath}`
90
-
91
- return content.replace(linkRegex, newLinks)
92
- }