<commit_before># Create and Run Scripts

Sometimes we have to execute some tasks from the command line. These tasks can serve different purposes such as populating a database (user creation, etc) for instance. They often need to access some of the app classes and functions. This is where shell scripts come into play.

# Create Scripts

A shell script is just a TypeScript file which is called from the command line.

They are located in the `src/scripts` directory. Let's create one: `src/scripts/display-users.ts`

# Write Scripts

Usually a script has a `main` function which is called directly in the file. This lets you easily use async/await keywords when dealing with asynchronous code.

From this file you can import the classes and functions of your app and use them. You can also create a database connection based on the database configuration of your app.

Let's take an example: a script that displays the users stored in the database.

```typescript
// 3p
import { createConnection } from 'typeorm';

// App
import { User } from './app/entities';

async function main() {
  const connection = await createConnection();
  const users = await connection.getRepository(User).find();
  console.log(users);
}

main();
```

If you want to create a script that is called with some parameters from the command line, you can use the command `foal g script my-script` to generate such a script.

# Build and Run Scripts

To run a script you first need  to build it.

```sh
npm run build
```

Then you can run it with this command:

```sh
node lib/scripts/my-script.js
```<commit_msg>[Docs] Update document with foal run-script.<commit_after># Create and Run Scripts

Sometimes we have to execute some tasks from the command line. These tasks can serve different purposes such as populating a database (user creation, etc) for instance. They often need to access some of the app classes and functions. This is when shell scripts come into play.

# Create Scripts

A shell script is just a TypeScript file located in the `src/scripts`. It must export a `main` function that is then called when running the script.

Let's create a new one with the command line: `foal g script display-users`. A new file with a default template should appear in you `src/scripts` directory.

# Write Scripts

Remove the content of `src/scripts/display-users.ts` and replace it with the below code.

```typescript
// 3p
import { createConnection } from 'typeorm';

// App
import { User } from './app/entities';

export async function main() {
  await createConnection();
  const users = await connection.getRepository(User).find();
  console.log(users);
}

```

As you can see, we can easily establish a connection to the database in the script as well as import some of the app components (the `User` in this case).

Encapsulating your code in a `main` function without calling it directly in the file has several benefits:
- You can import and test your `main` function in a separate file.
- Using a function lets you easily use async/await keywords when dealing with asynchronous code.

# Build and Run Scripts

To run a script you first need to build it.

```sh
npm run build
```

Then you can execute it with this command:

```sh
foal run-script my-script
```

> You can also provide additionnal arguments to your script (for example: `foal run-script my-script foo=1 bar='[ 3, 4 ]'`). The default template in the generated scripts shows you how to handle such behavior.
{'fraction_non_alphanumeric': 0.06909511419485401, 'fraction_numerical': 0.0014455044810638912, 'mean_word_length': 4.110782865583457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3517621', 'n_tokens_mistral': 911, 'n_tokens_neox': 886, 'n_words': 519}