Environment files
A new project needs a .env next to each app before it runs, with a few secrets that nobody should type by hand.
prepare env makes them. The starters run it as pnpm prepare:env.
prepare env
pnpx @frigear-nu/cli prepare env [--dry-run] [--cwd <dir>]
| Argument | Default | Purpose |
|---|---|---|
--dry-run | Print what would change and write nothing. | |
--cwd | the current directory | Where to start; the command works on the nearest workspace root above it. |
For each apps/*/.env.example and packages/*/.env.example, it:
- Copies
.env.exampleto.envnext to it, unless.envexists. New files are only readable by you (0600). - Fills in the secrets below whose line is empty (
KEY=,KEY=""orKEY=''), in a new or an existing.env.
A value that is already set is never changed, so running it again is safe: after a git pull that adds a variable
to an example, add the line to your .env with no value and run it again.
apps/web/.env: created from .env.example; generated NUXT_APP_SECRET
apps/mobile/.env: up to date, nothing changed
Built-in secrets
| Variable | Generator | Used by |
|---|---|---|
NUXT_APP_SECRET | random:32 | Nuxt's appSecret; nuxt-auth-utils seals the session cookie with it. |
NUXT_OAUTH_SERVER_PRIVATE_KEY | rsa | @frigear-nu/oauth-server signs its tokens with it. |
Your own secrets
Add secrets, or change where it looks, in the frigear field of the root package.json, next to your
custom platforms:
{
"frigear": {
"env": {
"secrets": {
"NUXT_WEBHOOK_SECRET": "hex:32",
"NUXT_DEVICE_ID": "uuid",
"NUXT_OAUTH_SERVER_PRIVATE_KEY": false
},
"dirs": ["apps", "packages", "services"]
}
}
}
| Field | Purpose |
|---|---|
secrets | Variable name → generator. A built-in's name replaces its generator; false turns it off. |
dirs | The folders whose direct subfolders are searched for .env.example. Default: apps and packages. |
The generators:
| Generator | Value |
|---|---|
random:<bytes> | Random bytes, base64 (random:32 gives 44 characters). |
hex:<bytes> | Random bytes, hex (hex:32 gives 64 characters). |
uuid | A random UUID. |
rsa | A 2048-bit PKCS#8 RSA private key on one line with \n escapes, in double quotes. |
An unknown generator stops the command with the variable's name, before anything is written for that file.
From code
For a value no generator covers, call prepareEnv from a script of your own and pass a function:
import { prepareEnv, describeEnvResult } from '@frigear-nu/cli'
const results = await prepareEnv({
root: process.cwd(),
secrets: {
NUXT_PUBLIC_DEV_PORT: () => String(3000 + Math.floor(Math.random() * 1000)),
},
})
for (const result of results) console.log(describeEnvResult(result))
secrets is applied after the built-ins and frigear.env, so it can add, replace ('hex:16' or a function) or
turn off (false) any of them. See From code for every option.
Projects with their own script
Projects made from a starter before this command shipped have a scripts/prepare-env.mjs. It does the same thing,
so it keeps working. To switch, delete it and set the root script:
{
"scripts": {
"prepare:env": "pnpx @frigear-nu/cli prepare env"
}
}
If you added secrets to your copy of the script, move them to frigear.env.secrets.
Next steps
- From code –
prepareEnvand its options. - Custom platforms – the rest of the
frigearfield.