@firefoxic/utils 2.1.1 → 2.2.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
@@ -2,7 +2,6 @@
2
2
 
3
3
  [![License: MIT][license-image]][license-url]
4
4
  [![Changelog][changelog-image]][changelog-url]
5
- [![NPM version][npm-image]][npm-url]
6
5
  [![Test Status][test-image]][test-url]
7
6
 
8
7
  Collection of useful utilities for JavaScript and Node.js.
@@ -91,14 +90,50 @@ jobs:
91
90
  branch: gh-pages
92
91
  ```
93
92
 
93
+ ### `get-node-matrix`
94
+
95
+ A CLI utility that reads the `engines.node` field from the project's `package.json` and prints the minimum Node.js versions (as a JSON array) for use in a GitHub Actions matrix. When running in a GitHub Actions environment, it also appends `node_versions=...` to the file pointed to by `GITHUB_OUTPUT`.
96
+
97
+ Use it in the `discover` job of your workflow:
98
+
99
+ ```yaml
100
+ jobs:
101
+ discover:
102
+ runs-on: ubuntu-latest
103
+ outputs:
104
+ node_versions: ${{ steps.matrix.outputs.node_versions }}
105
+ steps:
106
+ - uses: actions/checkout@v7
107
+ - uses: pnpm/setup@v3
108
+ with:
109
+ cache: true
110
+ node-version: "26"
111
+ - id: matrix
112
+ run: pnpm dlx --package @firefoxic/utils get-node-matrix
113
+
114
+ test:
115
+ needs: discover
116
+ strategy:
117
+ fail-fast: false
118
+ matrix:
119
+ node: ${{ fromJSON(needs.discover.outputs.node_versions) }}
120
+ runs-on: ubuntu-latest
121
+ steps:
122
+ - uses: actions/checkout@v7
123
+ - uses: pnpm/setup@v3
124
+ with:
125
+ cache: true
126
+ runtime: node@${{ matrix.node }}
127
+ - run: make verify
128
+ ```
129
+
130
+ The utility resolves the project's `package.json` from the current working directory (walking up the directory tree), or from the `npm_package_json` environment variable when run through an npm/pnpm script.
131
+
94
132
  [license-url]: https://github.com/firefoxic/utils/blob/main/LICENSE.md
95
133
  [license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg
96
134
 
97
135
  [changelog-url]: https://github.com/firefoxic/utils/blob/main/CHANGELOG.md
98
136
  [changelog-image]: https://img.shields.io/badge/CHANGELOG-md-limegreen
99
137
 
100
- [npm-url]: https://www.npmjs.com/package/@firefoxic/utils
101
- [npm-image]: https://badge.fury.io/js/@firefoxic%2Futils.svg
102
-
103
138
  [test-url]: https://github.com/firefoxic/utils/actions
104
139
  [test-image]: https://github.com/firefoxic/utils/actions/workflows/test.yaml/badge.svg?branch=main
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env node
2
+ import { appendFileSync, readFileSync, statSync } from "node:fs";
3
+ import path from "node:path";
4
+ function resolvePackageJsonPath() {
5
+ let fromEnv = process.env.npm_package_json;
6
+ if (fromEnv) {
7
+ try {
8
+ if (statSync(fromEnv).isFile())
9
+ return fromEnv;
10
+ }
11
+ catch {
12
+ // Fall through to the directory search below.
13
+ }
14
+ }
15
+ let dir = path.resolve(process.cwd());
16
+ while (true) {
17
+ let candidate = path.resolve(dir, `package.json`);
18
+ try {
19
+ if (statSync(candidate).isFile())
20
+ return candidate;
21
+ }
22
+ catch {
23
+ // Keep walking up the directory tree.
24
+ }
25
+ let parent = path.dirname(dir);
26
+ if (parent === dir)
27
+ break;
28
+ dir = parent;
29
+ }
30
+ throw new Error(`Project package.json not found`);
31
+ }
32
+ let packageJson = JSON.parse(readFileSync(resolvePackageJsonPath(), `utf8`));
33
+ let range = packageJson.engines?.node;
34
+ if (!range)
35
+ throw new Error(`Project package.json does not specify engines.node`);
36
+ let versions = range.split(`||`).map((part) => {
37
+ let version = part.match(/(\d+)(?:\.(\d+))?(?:\.(\d+))?/u);
38
+ if (!version)
39
+ throw new Error(`Cannot resolve minimum Node.js version: ${part}`);
40
+ return `${version[1]}.${version[2] ?? 0}.${version[3] ?? 0}`;
41
+ });
42
+ let output = JSON.stringify([...new Set(versions)]);
43
+ if (process.env.GITHUB_OUTPUT) {
44
+ appendFileSync(process.env.GITHUB_OUTPUT, `node_versions=${output}\n`);
45
+ }
46
+ console.info(`Node.js matrix: ${output}`);
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Get the path to the project root when deploying to GitHub Pages.
3
+ * Your action should set `REPO_NAME` and `PR_NUMBER` environment variables at the step you need
4
+ * (most likely when building the project).
5
+ * @returns {string} The path to the project directory from the domain root if working in a GitHub CI environment. If not, returns `/`.
6
+ */
7
+ export declare function getProjectRoot(): string;
@@ -0,0 +1,14 @@
1
+ import { env } from "node:process";
2
+ /**
3
+ * Get the path to the project root when deploying to GitHub Pages.
4
+ * Your action should set `REPO_NAME` and `PR_NUMBER` environment variables at the step you need
5
+ * (most likely when building the project).
6
+ * @returns {string} The path to the project directory from the domain root if working in a GitHub CI environment. If not, returns `/`.
7
+ */
8
+ export function getProjectRoot() {
9
+ if (!env.CI || !env.REPO_NAME)
10
+ return ``;
11
+ if (!env.PR_NUMBER)
12
+ return `/${env.REPO_NAME}`;
13
+ return `/${env.REPO_NAME}/${env.PR_NUMBER}`;
14
+ }
package/dist/index.d.ts CHANGED
@@ -1,19 +1,2 @@
1
- /**
2
- * Get the path to the project root when deploying to GitHub Pages.
3
- * Your action should set `REPO_NAME` and `PR_NUMBER` environment variables at the step you need
4
- * (most likely when building the project).
5
- * @returns {string} The path to the project directory from the domain root if working in a GitHub CI environment. If not, returns `/`.
6
- */
7
- declare function getProjectRoot(): string;
8
-
9
- /**
10
- * Sets an environment variable in the GitHub Actions environment file.
11
- * @async
12
- * @param {string} varName - The name of the environment variable to set.
13
- * @param {string} varValue - The value of the environment variable to set.
14
- * @returns {Promise<void>} A promise that resolves when the environment variable has been set.
15
- * @throws {Error} If the function is called outside of a GitHub Actions environment or if the GITHUB_ENV environment variable is not set.
16
- */
17
- declare function setGitHubEnvVar(varName: string, varValue: string): Promise<void>;
18
-
19
- export { getProjectRoot, setGitHubEnvVar };
1
+ export { getProjectRoot } from "./get-project-root.ts";
2
+ export { setGitHubEnvVar } from "./set-github-env-var.ts";
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{env as r}from"process";function e(){return!r.CI||!r.REPO_NAME?"":r.PR_NUMBER?`/${r.REPO_NAME}/${r.PR_NUMBER}`:`/${r.REPO_NAME}`}import{appendFile as i}from"fs/promises";import{env as t}from"process";async function E(n,o){if(t.CI){if(!t.GITHUB_ENV)throw new Error("GITHUB_ENV environment variable is not set. This function should only be used in a GitHub Actions environment.");await i(t.GITHUB_ENV,`
2
- ${n}=${o}`)}}export{e as getProjectRoot,E as setGitHubEnvVar};
1
+ export { getProjectRoot } from "./get-project-root.js";
2
+ export { setGitHubEnvVar } from "./set-github-env-var.js";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Sets an environment variable in the GitHub Actions environment file.
3
+ * @async
4
+ * @param {string} varName - The name of the environment variable to set.
5
+ * @param {string} varValue - The value of the environment variable to set.
6
+ * @returns {Promise<void>} A promise that resolves when the environment variable has been set.
7
+ * @throws {Error} If the function is called outside of a GitHub Actions environment or if the GITHUB_ENV environment variable is not set.
8
+ */
9
+ export declare function setGitHubEnvVar(varName: string, varValue: string): Promise<void>;
@@ -0,0 +1,17 @@
1
+ import { appendFile } from "node:fs/promises";
2
+ import { env } from "node:process";
3
+ /**
4
+ * Sets an environment variable in the GitHub Actions environment file.
5
+ * @async
6
+ * @param {string} varName - The name of the environment variable to set.
7
+ * @param {string} varValue - The value of the environment variable to set.
8
+ * @returns {Promise<void>} A promise that resolves when the environment variable has been set.
9
+ * @throws {Error} If the function is called outside of a GitHub Actions environment or if the GITHUB_ENV environment variable is not set.
10
+ */
11
+ export async function setGitHubEnvVar(varName, varValue) {
12
+ if (!env.CI)
13
+ return;
14
+ if (!env.GITHUB_ENV)
15
+ throw new Error(`GITHUB_ENV environment variable is not set. This function should only be used in a GitHub Actions environment.`);
16
+ await appendFile(env.GITHUB_ENV, `\n${varName}=${varValue}`);
17
+ }
package/package.json CHANGED
@@ -1,48 +1,64 @@
1
1
  {
2
- "name": "@firefoxic/utils",
3
- "description": "Collection of useful utilities for JavaScript and Node.js.",
4
- "version": "2.1.1",
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": {
20
- ".": {
21
- "import": "./dist/index.js",
22
- "types": "./dist/index.d.ts"
23
- }
24
- },
25
- "files": [
26
- "./dist/"
27
- ],
28
- "engines": {
29
- "node": ">=22.18",
30
- "pnpm": ">=10.30"
31
- },
32
- "scripts": {
33
- "help": "make help"
34
- },
35
- "devDependencies": {
36
- "@firefoxic/oxlint-config": "^0.1.3",
37
- "@types/node": "^25.3.0",
38
- "oxlint": "^1.50.0",
39
- "tsup": "^8.5.1",
40
- "typescript": "^5.9.3"
41
- },
42
- "keywords": [
43
- "javascript",
44
- "node.js",
45
- "prepare",
46
- "utils"
47
- ]
48
- }
2
+ "name": "@firefoxic/utils",
3
+ "description": "Collection of useful utilities for JavaScript and Node.js.",
4
+ "version": "2.2.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+https://github.com/firefoxic/utils.git"
17
+ },
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/index.js",
22
+ "types": "./dist/index.d.ts"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "bin": {
27
+ "get-node-matrix": "./dist/get-node-matrix.js"
28
+ },
29
+ "files": [
30
+ "./dist/"
31
+ ],
32
+ "engines": {
33
+ "node": "^22.18 || ^24.15 || >=26.13"
34
+ },
35
+ "devEngines": {
36
+ "runtime": {
37
+ "name": "node",
38
+ "version": ">=26.13",
39
+ "onFail": "download"
40
+ },
41
+ "packageManager": {
42
+ "name": "pnpm",
43
+ "version": ">=12.19",
44
+ "onFail": "download"
45
+ }
46
+ },
47
+ "devDependencies": {
48
+ "@firefoxic/oxlint-config": "~0.6",
49
+ "@types/node": "^26",
50
+ "@vitest/coverage-v8": "^5",
51
+ "oxlint": "^1",
52
+ "tsrs": "^0.25.0-ts7",
53
+ "vitest": "^5"
54
+ },
55
+ "keywords": [
56
+ "javascript",
57
+ "node.js",
58
+ "prepare",
59
+ "utils"
60
+ ],
61
+ "scripts": {
62
+ "help": "make help"
63
+ }
64
+ }