# nav-new-publication-cli

This CLI tool is designed to automate the process of adding new publications to the Navigator web project. It collects user input, validates it, and updates or creates the necessary configuration files in the web repository.

## Features

- **Interactive Prompts**: Collects details such as platform, publication title, domain, AWS certificate ARNs, and autoscaling configurations.
- **Validation**: Ensures input is valid and publication names are unique.
- **File Updates**: Automatically updates or creates files like `drone/publication.json`, `storybook/preview.js`, and infrastructure configurations.
- **Template-Based File Creation**: Uses a predefined template to generate infrastructure configuration files.
- **Error Handling**: Logs errors and provides feedback during the process.

## Installation

1. Clone the repository:
   ```bash
   git clone git@bitbucket.org:trinitymirror-ondemand/nav-new-publication-cli.git
   cd nav-new-publication-cli
   ```

2. Install dependencies:
   ```bash
   yarn install
   ```

3. Build the project:
   ```bash
   yarn build
   ```

4. Link the CLI globally (optional):
   ```bash
   yarn link
   ```

## Usage

Run the CLI using the following command:
```bash
node dist/index.js
```

Alternatively, if linked globally:
```bash
create-nav-publication
```

### Options

- `-p, --platform <type>`: Specify the platform (e.g., nationals, regionals).
- `-t, --title <type>`: Set the publication title.
- `-c, --cue-name <type>`: Specify the name in the cue.
- `--vr, --vr-steps <type>`: Number of VR steps.
- `-f, --func-steps <type>`: Number of functional steps.
- `-d, --domain <type>`: Domain of the site.
- `--db, --debug`: Output extra debugging information.

### Example

```bash
create-nav-publication -p nationals -t "New Publication" -d "newpublication.co.uk"
```

## File Updates

The CLI updates or creates the following files in the web repository:

1. **`drone/publication.json`**:
   - **Purpose**: Stores the list of publications categorized by platform.
   - **Update Logic**: Finds the appropriate platform array (e.g., `"nationals": [`) and appends the new publication as a JSON object.
   - **Insertion Point**: The insertion point is determined by locating the platform key in the JSON structure.

2. **`storybook/preview.js`**:
   - **Purpose**: Manages CSS imports and configurations for Storybook.
   - **Update Logic**: Adds new `import` statements for the publication's CSS and updates the `cssList` and `cssVariables.files` sections.
   - **Insertion Point**: The last `import` statement or specific sections like `cssList` and `cssVariables.files`.

3. **Infrastructure Configurations**:
   - **Purpose**: Defines infrastructure settings for the publication.
   - **Creation Logic**: Uses the `template/infrastucture.txt` file to generate a new configuration file in `infrastructure/configs/publications/`.
   - **Template Replacement**: Replaces placeholders in the template with publication-specific details.

4. **`localProxy.config.js`**:
   - **Purpose**: Configures local proxy settings for the publication.
   - **Update Logic**: Adds a new entry to the `domainName` object.
   - **Insertion Point**: The closing brace of the `domainName` object.

5. **`web-fargate.ts`**:
   - **Purpose**: Configures AWS Fargate settings for the publication.
   - **Update Logic**: Adds a new entry for the publication's Fargate configuration.
   - **Insertion Point**: Determined by scanning for the appropriate section in the file.

6. **Constants Files**:
   - **Purpose**: Stores constants related to the publication.
   - **Update Logic**: Adds new constants for the publication.
   - **Insertion Point**: The end of the constants section.

7. **`utils.ts`**:
   - **Purpose**: Provides utility functions for logging, error handling, and other common tasks.
   - **Update Logic**: Adds new utility functions as required by the CLI.
   - **Insertion Point**: Functions are added at the end of the file or grouped logically with similar utilities.

8. **`constants.ts`**:
   - **Purpose**: Stores reusable constants used across the CLI.
   - **Update Logic**: Adds new constants for publications or other configurations as needed.
   - **Insertion Point**: The end of the constants section or a specific category of constants.

9. **`tsconfig.json`**:
   - **Purpose**: Configures TypeScript compilation settings for the project.
   - **Update Logic**: Rarely updated, but may include new paths or compiler options if additional files or features are added.
   - **Insertion Point**: Determined by the structure of the JSON file.

10. **`infrastucture.txt`**:
    - **Purpose**: Serves as a template for generating infrastructure configuration files.
    - **Update Logic**: Updated when new placeholders or configurations are required for infrastructure files.
    - **Insertion Point**: Placeholders are replaced dynamically during file generation.

## Development

### Project Structure

- `src/`: Contains the source code.
  - **`index.ts`**: Entry point of the CLI. Handles command-line arguments, initializes prompts, and orchestrates the overall workflow.
  - **`fileUpdates.ts`**: Contains logic for updating or creating files. Defines functions to locate insertion points and generate new content.
  - **`prompts.ts`**: Manages interactive prompts. Defines the structure and validation of user inputs.
  - **`readWritePublications.ts`**: Handles reading and writing of the `publication.json` file. Ensures data integrity during updates.
  - **`checkers.ts`**: Provides validation functions for user inputs, such as checking for valid ARNs, domains, and publication names.
  - **`utils.ts`**: Utility functions for logging errors, warnings, and success messages.
- `template/`: Contains the template for infrastructure configuration files.
- `drone/`: Stores the `publication.json` file.

### Scripts

- `yarn build`: Compiles the TypeScript code to JavaScript.
- `yarn build:watch`: Watches for changes and rebuilds the project.
- `yarn start`: Runs the CLI.

### How Insertion Points Are Decided

- **JSON Files**: The insertion point is determined by parsing the JSON structure and locating the appropriate key or array.
- **JavaScript Files**: The insertion point is identified by scanning for specific patterns, such as the last `import` statement or a specific object key.
- **Template Files**: Placeholders in the template are replaced with publication-specific details, and the file is saved in the appropriate directory.