Skip to content

Commit

Permalink
Add info about getProjectRoot to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
firefoxic committed May 11, 2024
1 parent 4d6b632 commit 825feba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@

Collection of useful utilities for JavaScript and Node.js.

## Installation

```shell
pnpm add -D @firefoxic/utils
```

## Usage

### `getProjectRoot`

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:

```yaml
jobs:
job_name:
runs-on: ubuntu-latest
steps:

# Any steps before building a project

- name: Build project
run: pnpm build
env:
REPO_NAME: ${{ github.event.repository.name }}
PR_NUMBER: ${{ github.event.pull_request.number }} # For PR action

- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: ./dist
branch: gh-pages
target-folder: ${{ github.event.pull_request.number }} # For PR action
```
Than you can get the path to the project root when deploying to GitHub Pages, for example in `vite.config.js`:

```js
import { defineConfig } from "vite"
import { getProjectRoot } from "@firefoxic/utils"
export default defineConfig({
base: `${getProjectRoot()}/`,
})
```

[license-url]: https://github.com/firefoxic/utils/blob/main/LICENSE.md
[license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg

Expand Down
14 changes: 4 additions & 10 deletions lib/getProjectRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import { env } from "node:process"

/**
* Get the path to the project root when deploying to GitHub Pages.
* Your action should set `REPO_NAME` and `REPO_NAME` environment variables at the step you need
* (most likely when building the project), for example like this:
*
* - name: Build project
* run: pnpm build
* env:
* REPO_NAME: ${{ github.event.repository.name }}
* PR_NUMBER: ${{ github.event.pull_request.number }}
* Your action should set `REPO_NAME` and `PR_NUMBER` environment variables at the step you need
* (most likely when building the project).
*
* @returns {string} The path to the project directory from the domain root if working in a GitHub CI environment. If not, returns `/`.
*/
export function getProjectRoot () {
if (!env.CI || !env.REPO_NAME) { return `` }
if (!env.CI || !env.REPO_NAME) return ``

if (!env.PR_NUMBER) { return `/${env.REPO_NAME}` }
if (!env.PR_NUMBER) return `/${env.REPO_NAME}`

return `/${env.REPO_NAME}/${env.PR_NUMBER}`
}

0 comments on commit 825feba

Please sign in to comment.