kibana/packages/kbn-cli-dev-mode
Kibana Machine 72d0f2de84
[8.x] [Dev Server] Remove dead code (#193736) (#193759)
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Dev Server] Remove dead code
(#193736)](https://github.com/elastic/kibana/pull/193736)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Alejandro Fernández
Haro","email":"alejandro.haro@elastic.co"},"sourceCommit":{"committedDate":"2024-09-23T17:01:08Z","message":"[Dev
Server] Remove dead code
(#193736)","sha":"e6e170e687a2221ebdcf49c0942cf6e40aed9b59","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Operations","technical
debt","release_note:skip","v9.0.0","backport:prev-minor"],"title":"[Dev
Server] Remove dead
code","number":193736,"url":"https://github.com/elastic/kibana/pull/193736","mergeCommit":{"message":"[Dev
Server] Remove dead code
(#193736)","sha":"e6e170e687a2221ebdcf49c0942cf6e40aed9b59"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193736","number":193736,"mergeCommit":{"message":"[Dev
Server] Remove dead code
(#193736)","sha":"e6e170e687a2221ebdcf49c0942cf6e40aed9b59"}}]}]
BACKPORT-->

Co-authored-by: Alejandro Fernández Haro <alejandro.haro@elastic.co>
2024-09-23 13:31:10 -05:00
..
src [8.x] [Dev Server] Remove dead code (#193736) (#193759) 2024-09-23 13:31:10 -05:00
index.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
jest.config.js Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
jest.integration.config.js Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kibana.jsonc [Move @kbn/config-schema to server] @kbn/cli-dev-mode (#189523) 2024-07-30 12:47:04 -06:00
package.json Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
README.mdx switch out chokidar for @parcel/watcher in dev cli (#148924) 2023-01-13 16:42:13 -07:00
tsconfig.json Add http2 support for Kibana server (#183465) 2024-06-03 09:34:13 +02:00

---
id: kibDevDocsOpsCliDevMode
slug: /kibana-dev-docs/ops/cli-dev-mode
title: "@kbn/cli-dev-mode"
description: A package to manage the Kibana cli behavior when in development
date: 2022-05-24
tags: ['kibana', 'dev', 'contributor', 'operations', 'cli', 'dev', 'mode']
---

This package exposes a function that manages the alternate behavior of the Kibana cli when using 
the `--dev` flag. This mode provides several useful features in a single CLI for a nice developer 
experience:

  - automatic server restarts when code changes
  - runs the `@kbn/optimizer` to build browser bundles
  - runs a base path proxy which helps developers test that they are writing code which is 
compatible with custom basePath settings while they work
  - pauses requests when the server or optimizer are not ready to handle requests so that when 
users load Kibana in the browser it's always using the code as it exists on disk

To accomplish this, and to make it easier to test, the `CliDevMode` class manages the following 
objects.

## `Watcher`

The `Watcher` manages a [@parcel/watcher](https://github.com/parcel-bundler/watcher) instance to watch the 
server files, logs about file changes observed and provides an observable to the `DevServer` via 
its `serverShouldRestart$()` method.

## `DevServer`

The `DevServer` object is responsible for everything related to running and restarting the Kibana 
server process:
 - listens to restart notifications from the `Watcher` object, sending `SIGKILL` to the existing 
server and launching a new instance with the current code
 - writes the stdout/stderr logs from the Kibana server to the parent process
 - gracefully kills the process if the SIGINT signal is sent
 - kills the server if the SIGTERM signal is sent, process.exit() is used, a second SIGINT is 
sent, or the graceful shutdown times out
 - proxies SIGHUP notifications to the child process, though the core team is working on 
migrating this functionality to the KP and making this unnecessary

## `Optimizer`

The `Optimizer` object manages a `@kbn/optimizer` instance, adapting its configuration and 
logging to the data available to the CLI.

## `BasePathProxyServer`

This proxy injects a random three character base path in the URL that Kibana is served from to 
help ensure that Kibana features are written to adapt to custom base path configurations from users.

The basePathProxy also has another important job, ensuring that requests don't fail because the 
server is restarting and that the browser receives front-end assets containing all saved 
changes. We accomplish this by observing the ready state of the `Optimizer` and `DevServer` 
objects and pausing all requests through the proxy until both objects report that they 
aren't building/restarting based on recently saved changes.