2022-11-06 05:56:37 +00:00
# Commands
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
NocoBase Server Application is a powerful and extensible CLI tool in addition to being used as a WEB server.
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
Create a new `app.js` file with the following code.
2022-10-01 14:18:26 +00:00
```ts
const Application = require('@nocobase/server');
2022-11-06 05:56:37 +00:00
// omit the specific configuration here
const app = new Application({/*... */});
2022-10-01 14:18:26 +00:00
app.runAsCLI();
```
2022-11-06 05:56:37 +00:00
app.js run as ``runAsCLI()`` is a CLI, it will work like this in the command line tool.
2022-10-01 14:18:26 +00:00
```bash
2022-11-06 05:56:37 +00:00
node app.js install # install
node app.js start # start
2022-10-01 14:18:26 +00:00
```
2022-11-06 05:56:37 +00:00
To better develop, build and deploy NocoBase applications, NocoBase has many built-in commands, see the [NocoBase CLI ](/api/cli ) section for details.
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
## How to customize Command?
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
NocoBase CLI is designed to be very similar to [Laravel Artisan ](https://laravel.com/docs/9.x/artisan ), both are extensible. NocoBase CLI is based on [commander ](https://www.npmjs.com/ package/commander ) implementation, which extends Command like this
2022-10-01 14:18:26 +00:00
```ts
2022-10-31 14:41:24 +00:00
export class MyPlugin extends Plugin {
load() {
this.app
.command('echo')
2022-11-06 05:56:37 +00:00
.option('--v, --version');
2022-10-31 14:41:24 +00:00
.action(async ([options]) => {
console.log('Hello World!');
if (options.version) {
console.log('Current version:', app.getVersion());
}
});
}
}
2022-10-01 14:18:26 +00:00
```
2022-11-06 05:56:37 +00:00
This method defines the following command.
2022-10-01 14:18:26 +00:00
```bash
yarn nocobase echo
# Hello World!
yarn nocobase echo -v
# Hello World!
2022-10-31 14:41:24 +00:00
# Current version: 0.8.0-alpha.1
2022-10-01 14:18:26 +00:00
```
2022-11-06 05:56:37 +00:00
More API details can be found in the [Application.command() ](/api/server/application#command ) section.
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
## Example
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
### Defining a command for exporting collections
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
If we want to export the data in the application's collections to a JSON file, we can define a subcommand as follows.
2022-10-01 14:18:26 +00:00
```ts
import path from 'path';
import * as fs from 'fs/promises';
class MyPlugin extends Plugin {
load() {
this.app
.command('export')
.option('-o, --output-dir')
2022-11-06 05:56:37 +00:00
.action(async (options, . .collections) => {
2022-10-01 14:18:26 +00:00
const { outputDir = path.join(process.env.PWD, 'storage') } = options;
await collections.reduce((promise, collection) => promise.then(async () => {
if (!this.db.hasCollection(collection)) {
console.warn('No such collection:', collection);
return;
}
const repo = this.db.getRepository(collection);
const data = repo.find();
await fs.writeFile(path.join(outputDir, `${collection}.json` ), JSON.stringify(data), { mode: 0o644 });
}), Promise.resolve());
});
}
}
```
2022-11-06 05:56:37 +00:00
After registering and activating the plugin call from the command line.
2022-10-01 14:18:26 +00:00
```bash
2022-11-06 05:56:37 +00:00
mkdir -p . /storage/backups
yarn nocobase export -o . /storage/backups users
2022-10-01 14:18:26 +00:00
```
2022-11-06 05:56:37 +00:00
After execution, it will generate `. /storage/backups/users.json` file containing the data from the collections.
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
## Summary
2022-10-01 14:18:26 +00:00
2022-11-06 05:56:37 +00:00
The sample code covered in this chapter is integrated in the [packages/samples/command ](https://github.com/nocobase/nocobase/tree/main/packages/samples/command ) package and can be run directly locally to see the results.