Skip to content

Running Scripts with the CLI

You can easily execute scripts from the CLI using the npx accel run command. This command operates with an initialized database connection and leverages Vite internally, allowing you to pass TypeScript files directly.

If any of the following functions are defined in the script, they will be executed automatically:

  • A function exported as export default
  • A function exported as main

Script File and Execution Example

src/scripts/sample.ts
import { Account } from "./models/index.js";
export default async () => {
const count = Account.count();
console.log(`Total accounts: ${count}`);
};
Terminal window
$ npx accel run src/scripts/sample.ts
Total accounts: 10

Custom CLI Commands

In addition to running scripts, you can also register custom CLI commands in the files under src/commands/. This allows you to execute custom processes with the database connection fully initialized.

Creating Custom Commands

src/commands/hello.ts
import { program } from "accella/cli";
import { Account } from "../models";
program
.command("hello")
.description("Hello command")
.action(() => {
console.log("Hello from Accella!");
const count = Account.count();
console.log(`Total accounts: ${count}`);
});

Running Custom Commands

Terminal window
# Run the command
$ npx accel hello
Hello from Accella!
Total accounts: 5