CLI

Installation

pnpm add -D @agrume/cli

Note

You can also install the CLI globally by using the -g flag.

Configuration

To configure the CLI, you can either use the command options or create a configuration file.

At the root of your project, create a agrume.config.ts file:

import { defineConfig } from '@agrume/cli'

export default defineConfig({
  externalUrl: 'http://localhost:3000/',
  host: 'localhost',
  port: 8173,
  prefix: '/__agrume__/',
  watch: true,
  // As plugin options, if you use `externalUrl`, you can't use `tunnel` (and vice versa)
  // tunnel: {
  //   type: 'Localtunnel',
  // },
})

Note

You can also put the configuration file in a .config directory following the config dir proposal. In fact, the configuration is loaded using c12, so you can even use .js or .json even if it has not been tested.

The advantage of using a configuration file is that you can share the configuration with the plugin. For example, if you use the Vite plugin, you can write:

import { defineConfig } from 'vite'
import agrume from '@agrume/plugin/vite'
import agrumeConfig from './agrume.config'

export default defineConfig({
  plugins: [
    agrume({
      ...agrumeConfig,
      // other options
    }),
    // ...
  ]
})

agrume

You can use the CLI to start the server:

agrume

It will find the routes in your project and start a Fastify server.

Note

For development purposes, you can just use the agrume Vite plugin that will use the Vite server.

Options

OptionArgumentDescriptionDefault
-p, --portA numberPort to listen on3000
-h, --hostA stringHost to listen onlocalhost
-e, --entryA list of strings separated by a commaThe entry files to search for routes"index.js,index.ts,index.jsx,index.tsx,main.js,main.ts,main.jsx,main.tsx,app.js,app.ts,app.jsx,app.tsx,src/index.js,src/index.ts,src/index.jsx,src/index.tsx,src/main.js,src/main.ts,src/main.jsx,src/main.tsx,src/app.js,src/app.ts,src/app.jsx,src/app.tsx"
--watchOptional The directory to watch for changesWatch for changes in the target directorynot provided. If the option is present, defaults to the entry file found
--tunnelOptional The tunnel type (see the tunnel option in the configuration)Use a tunnel to access the servernot provided. If the option is present, defaults to the localtunnel tunnel
--allow-unsafeAllow loading routes from node_modulesfalse
--cors-regexpA stringThe regular expression to match the origin
--ngrok-domainA stringThe domain to use with Ngrok
--pinggy-subdomainA stringThe subdomain to use with Pinggy
--pinggy-tokenA stringThe token to use with pinggy

agrume build

You can use the CLI to build the routes:

agrume build

It allows you not being dependent on Agrume to run your backend.

By default, agrume build will write in TypeScript a Fastify server in the build directory. If a port is specified either in the configuration or in the command, you will be able to run the server with npx tsx build/index.mts. Otherwise, you will be able to import a registerServer function from the generated file and use it in your own Fastify server.

Options

OptionArgumentDescriptionDefault
libraryThe library to compile for. Choices: fastify"fastify"
-e, --entryA list of strings separated by a commaThe entry files to search for routes"index.js,index.ts,index.jsx,index.tsx,main.js,main.ts,main.jsx,main.tsx,app.js,app.ts,app.jsx,app.tsx,src/index.js,src/index.ts,src/index.jsx,src/index.tsx,src/main.js,src/main.ts,src/main.jsx,src/main.tsx,src/app.js,src/app.ts,src/app.jsx,src/app.tsx"
-o, --outputA directory were to writeThe output directory"build"
--disable-loggerDisable the logger of the serverfalse
--single-fileGenerate a single filefalse
-p, --portA string for an environment variable, or a numberThe port the server will listen on. By default, it uses the port from the config file. Set a string to select an environment variable, or leave empty to avoid generating listen code.not provided
-w, --watchOptional The directory to watch for changesWatch for changes in the target directorynot provided

For example, if you want to build a Fastify server in the dist directory that listens on the port 1234, you can run:

agrume build fastify --output dist --port 1234

Use as a Fastify plugin

You can generate a function that will register the routes in a Fastify server:

agrume build fastify --output agrume-server --port ""

Then, you can use the generated function in your Fastify server:

import fastify from 'fastify'
import { registerServer } from './agrume-server'

const app = fastify()

registerServer(app)

// Do something with the app, like listening on a port