π± Seeding
Sometimes plugins recommend or require certain files to be present in your project. Seeding is a way to automate this process.
Seed on Installβ
When you install a plugin with the Robo CLI, you may be asked if you want files included and a description of them.
npx robo add @robojs/server
For example, the @robojs/server plugin may seed optional files in /src/api
that shows how to use it. They are optional and can be deleted if you don't want them.
You can also deny seeding by using the --no-seed
flag or by entering n
when prompted.
π¦ Installing plugin
β @robojs/server
π± Seed files detected
- @robojs/server: Example API route and index page
Would you like to include these files? [Y/n]: n
β¨ Plugin successfully installed and ready to use.
If you opted out of seeding, you can always add them later by running robo add
again.
TypeScriptβ
Seeds are smart enough to detect if you're using TypeScript and seed .ts
files instead of .js
.
However, this is only true if the plugin developer provided TypeScript files in the seed. If they didn't, you'll get JavaScript files instead even if you're using TypeScript.
If you're a plugin developer, we highly recommend providing .ts
files in the seed, even if your plugin wasn't written in TypeScript. This way, users can benefit from type safety right away. The Robo Compiler will automatically compile them to JavaScript for those who don't use TypeScript.
You can also provide both .ts
and .js
files if you want finer control over the output instead of relying on the compiler.
Use Casesβ
Seeding is useful for all kind of things, such as examples, databases, configurations, and more. Here's a list:
- Example files to get you started
- Configuration files
- Database files
- Complex setups
- Boilerplates
Plugins can seed anything they want, and it's up to you to decide if you want them or not.
Plugin Developersβ
If you're developing a plugin and want users to have certain files in their project, you can seed them by creating a /seed
folder in your plugin's root directory.
Anything you put in /seed
gets copied to the user's /src
directory. If you want to copy files into the root directory, you can use the /seed/_root
folder.
/seed
βββ /_root
β βββ index.html
βββ /api
βββ hello.ts
When the user installs your plugin, these files are copied to their project if they approve.
While optional, a description of the files is recommended to help users understand what they are getting. You can provide a description in your plugin's Robo Config file:
export default {
// ... rest of config
seed: {
description: 'Example API route and index page'
}
}
Now when users install your plugin with robo add
, they will see a description of the files being seeded before they decide to include them. This helps them make an informed decision.
Environment Variable Seedingβ
In addition to copying files, plugins can seed environment variables. Robo surfaces the additions before asking for confirmation so users understand exactly what will be written.
π Environment additions detected
- @robojs/auth: Adds secure defaults for authentication flows.
AUTH_SECRET β Secret used to sign and verify authentication payloads.
Target files: .env
Add these variables to .env? [Y/n]: Y
π Environment variables applied
β @robojs/auth added AUTH_SECRET
Declaring Static Keysβ
Add static values under seed.env.variables
. Robo merges these into any detected .env*
files without overwriting existing keys unless you explicitly opt in.
import type { Config } from 'robo.js'
const config: Config = {
seed: {
description: 'Adds environment defaults for Auth.',
env: {
description: 'Secures authentication payloads.',
variables: {
AUTH_SECRET: {
description: 'Secret used to sign and verify authentication payloads.',
overwrite: false,
value: 'replace-me'
}
}
}
}
}
export default config
Generating Values with Hooksβ
Use seed.hook
when you need to compute values on the fly or tweak the seeding experience. The hook runs before Robo shows the prompt, and anything you return is merged into the seed
configβperfect for populating environment variables without shipping a separate file.
import type { Config } from 'robo.js'
export default <Config>{
seed: {
description: 'Initializes Auth secrets.',
hook: async ({ generators }) => {
const secret = generators.randomHex(32)
return {
env: {
description: 'Generates a unique AUTH_SECRET.',
variables: {
AUTH_SECRET: {
description: 'Secret used to sign and verify authentication payloads.',
overwrite: false,
value: secret
}
}
}
}
}
}
}
Hooks run before Robo asks the user to confirm the changes. The inline function return value mirrors the seed
config, so you can mix static variables
with computed ones or leave the hook to handle everything dynamically.
Helpful Utilitiesβ
Within hooks, the helpers.generators
bundle exposes:
randomHex(bytes?: number)
β cryptographically strong hex strings.randomBase64(bytes?: number)
β base64 strings without padding.randomUUID()
β UUID v4 values.
The helpers.log
method prints to the installer's console with the plugin prefix, useful when debugging seeding logic.
Hooks are inline onlyβthere's no need to manage separate files. Robo compiles the function into the manifest automatically.
Limitationsβ
Seeds cannot be used to modify existing files in the project for security reasons. They can only add new files.
Similarly, seeds can only affect the Robo project they are installed in. They cannot modify other projects or files outside of the project's directory.