@firefoxic/utils 0.3.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 +99 -2
- package/lib/getProjectRoot.js +4 -10
- package/lib/index.js +2 -0
- package/lib/setGitHubEnvVar.js +16 -0
- package/package.json +1 -1
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
|
-
##
|
|
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
|
-
|
|
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
|
package/lib/getProjectRoot.js
CHANGED
|
@@ -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 `
|
|
6
|
-
* (most likely when building the project)
|
|
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)
|
|
11
|
+
if (!env.CI || !env.REPO_NAME) return ``
|
|
18
12
|
|
|
19
|
-
if (!env.PR_NUMBER)
|
|
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
|
@@ -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
|
+
}
|