βοΈ Configuration
How host, token, project and branch are resolved from file, environment and options.
resolveConfig() builds a SonarQubeConfig (host, token, project, branch) in layers, each overriding the last:
- the config file
- the environment
- explicit options
Undefined and empty values never overwrite a lower layer.
import { resolveConfig } from "@kud/sonarqube"
const config = resolveConfig({ branch: "main" })Settings
| Setting | Environment (first set wins) | File key |
|---|---|---|
| host | SONARQUBE_HOST, SONAR_HOST_URL | host |
| token | SONARQUBE_TOKEN, SONAR_TOKEN | token |
| project | SONARQUBE_PROJECT | project |
| branch | SONARQUBE_BRANCH | branch |
The older SONARQUBE_DASH_* names are still read as fallbacks: for host, token, project and branch alike, the names above win over them.
There is no default host. Constructing a client without one throws MissingHostError.
Config file
The file is $XDG_CONFIG_HOME/sonarqube/config.json, defaulting to ~/.config/sonarqube/config.json. The path is resolved on every call, so changes to XDG_CONFIG_HOME are honoured. The legacy ~/.config/sonarqube-dash-cli/config.json is still read when the new file does not exist, and writeConfig() carries its values across on the first write to the new location.
{
"host": "https://sonarqube.example.com",
"project": "acme-api"
}| Function | Purpose |
|---|---|
configFilePath(env?) | The config file location |
readConfigFile(file) | Parsed file, or {} when missing or invalid |
writeConfig(data, file?) | Merges data over the existing file, creates the directory, returns { path, data } |
Project-key fallback
With no project from any layer, resolveConfig reads sonar.projectKey from sonar-project.properties. readSonarProjectKey(dir?) does that lookup on its own and skips blank and # or ! comment lines.
Resolve options
resolveConfig accepts the four settings plus three testing hooks:
| Option | Default |
|---|---|
configPath | The XDG location |
cwd | process.cwd(), where sonar-project.properties is looked up |
env | process.env |