@firefoxic/utils 0.2.0 → 0.4.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/README.md CHANGED
@@ -1,7 +1,104 @@
1
1
  # @firefoxic/utils
2
2
 
3
+ [![License: MIT][license-image]][license-url]
4
+ [![Changelog][changelog-image]][changelog-url]
5
+ [![NPM version][npm-image]][npm-url]
6
+ [![Test Status][test-image]][test-url]
7
+
3
8
  Collection of useful utilities for JavaScript and Node.js.
4
9
 
5
- ## License
10
+ ## Installation
11
+
12
+ ```shell
13
+ pnpm add -D @firefoxic/utils
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### `getProjectRoot`
19
+
20
+ Your action should set the `REPO_NAME` environment variable to the step you want. If your action works with pull requests, you should also add two lines (which are marked below with a special comment) to use the `PR_NUMBER` variable. For example:
21
+
22
+ ```yaml
23
+ jobs:
24
+ job_name:
25
+ runs-on: ubuntu-latest
26
+ steps:
27
+
28
+ # Any steps before building a project
29
+
30
+ - name: Build project
31
+ run: pnpm build
32
+ env:
33
+ REPO_NAME: ${{ github.event.repository.name }}
34
+ PR_NUMBER: ${{ github.event.pull_request.number }} # For PR action
35
+
36
+ - name: Deploy
37
+ uses: JamesIves/github-pages-deploy-action@v4
38
+ with:
39
+ folder: ./dist
40
+ branch: gh-pages
41
+ target-folder: ${{ github.event.pull_request.number }} # For PR action
42
+ ```
43
+
44
+ Than you can get the path to the project root when deploying to GitHub Pages, for example in `vite.config.js`:
45
+
46
+ ```js
47
+ import { defineConfig } from "vite"
48
+ import { getProjectRoot } from "@firefoxic/utils"
49
+
50
+ export default defineConfig({
51
+ base: `${getProjectRoot()}/`,
52
+ })
53
+ ```
54
+
55
+ ### `setGitHubEnvVar`
56
+
57
+ Your action must accept the environment variable you pass into it from the JS. For example, you can pass the path to the distribution directory from the `vite.config.js`:
58
+
59
+ ```js
60
+ import { defineConfig } from "vite"
61
+
62
+ import { setGitHubEnvVar } from "@firefoxic/utils"
63
+
64
+ const PATH_TO_SRC = `./a_very_strange_path_to_source`
65
+ const PATH_TO_DIST = `./a_very_strange_path_to_dist`
66
+
67
+ setGitHubEnvVar(`PATH_TO_DIST`, PATH_TO_DIST)
68
+
69
+ export default defineConfig({
70
+ root: PATH_TO_SRC,
71
+ build: {
72
+ outDir: `.${PATH_TO_DIST}`,
73
+ },
74
+ })
75
+ ```
76
+
77
+ Then you can use this variable in the deploy step of your action:
78
+
79
+ ```yaml
80
+ jobs:
81
+ job_name:
82
+ runs-on: ubuntu-latest
83
+ steps:
84
+
85
+ # Any steps before deploying a project
86
+
87
+ - name: Deploy
88
+ uses: JamesIves/github-pages-deploy-action@v4
89
+ with:
90
+ folder: ${{ env.PATH_TO_DIST }}
91
+ branch: gh-pages
92
+ ```
93
+
94
+ [license-url]: https://github.com/firefoxic/utils/blob/main/LICENSE.md
95
+ [license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg
96
+
97
+ [changelog-url]: https://github.com/firefoxic/utils/blob/main/CHANGELOG.md
98
+ [changelog-image]: https://img.shields.io/badge/CHANGELOG-md-limegreen
99
+
100
+ [npm-url]: https://npmjs.org/package/@firefoxic/utils
101
+ [npm-image]: https://badge.fury.io/js/@firefoxic%2Futils.svg
6
102
 
7
- This project is licensed under the MIT License — see the [LICENSE.md](./LICENSE.md) file for details.
103
+ [test-url]: https://github.com/firefoxic/utils/actions
104
+ [test-image]: https://github.com/firefoxic/utils/actions/workflows/test.yml/badge.svg?branch=main
@@ -2,21 +2,15 @@ import { env } from "node:process"
2
2
 
3
3
  /**
4
4
  * Get the path to the project root when deploying to GitHub Pages.
5
- * Your action should set `REPO_NAME` and `REPO_NAME` environment variables at the step you need
6
- * (most likely when building the project), for example like this:
7
- *
8
- * - name: Build project
9
- * run: pnpm build
10
- * env:
11
- * REPO_NAME: ${{ github.event.repository.name }}
12
- * PR_NUMBER: ${{ github.event.pull_request.number }}
5
+ * Your action should set `REPO_NAME` and `PR_NUMBER` environment variables at the step you need
6
+ * (most likely when building the project).
13
7
  *
14
8
  * @returns {string} The path to the project directory from the domain root if working in a GitHub CI environment. If not, returns `/`.
15
9
  */
16
10
  export function getProjectRoot () {
17
- if (!env.CI || !env.REPO_NAME) { return `` }
11
+ if (!env.CI || !env.REPO_NAME) return ``
18
12
 
19
- if (!env.PR_NUMBER) { return `/${env.REPO_NAME}` }
13
+ if (!env.PR_NUMBER) return `/${env.REPO_NAME}`
20
14
 
21
15
  return `/${env.REPO_NAME}/${env.PR_NUMBER}`
22
16
  }
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { getProjectRoot } from "./getProjectRoot.js"
2
2
 
3
- export { updateChangelog } from "./updateChangelog.js"
3
+ export { setGitHubEnvVar } from "./setGitHubEnvVar.js"
@@ -0,0 +1,16 @@
1
+ import { appendFile } from "node:fs/promises"
2
+ import { env } from "node:process"
3
+
4
+ /**
5
+ * Sets an environment variable in the GitHub Actions environment file.
6
+ *
7
+ * @param {string} varName The name of the environment variable to set.
8
+ * @param {string} varValue The value of the environment variable to set.
9
+ *
10
+ * @returns {Promise<void>}
11
+ */
12
+ export async function setGitHubEnvVar (varName, varValue) {
13
+ if (!env.CI) return
14
+
15
+ await appendFile(env.GITHUB_ENV, `\n${varName}=${varValue}`)
16
+ }
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.4.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
- }