Advanced command execution and configuration management for VS Code
The envTrack VSCode extension allows you to manage and execute commands across different environments. This guide will help you understand and configure the extension effectively.
The configuration is typically stored in a YAML file. The main components of the configuration are:
Example yaml
externalConfigPath: ./external_config.yaml
paths:
deploy-config: ./../../deployment-configs
local-scripts: ./scripts
commands:
Deploy:Frontend:
background: true
shell: bash
forceEnv: true
isJSONOutput: true
hasTemplate: true
defaultFileName: ${env}_frontend_deploy_${curDateTime}
templateName: frontend_deploy_template
command: npm run deploy
killCmd: pkill -f "npm run deploy"
defaultSession: staging
autoSelectDefaultSession: true
importFrom:
- Common:Utilities
configMap:
envConfig: staging-fe
params:
buildType: production
cacheEnabled: true
sessions:
staging:
configMap:
envConfig: staging-fe
production:
configMap:
envConfig: prod-fe
params:
buildType: production-optimized
Database:Backup:
background: false
shell: bash
command: pg_dump -U ${DB_USER} -d ${DB_NAME} > backup_${curDateTime}.sql
defaultSession: development
protectionOnEnv: production
params:
DB_USER: configs.dbConfig.username
DB_NAME: myapp_db
sessions:
development:
configMap:
dbConfig: dev-db
production:
configMap:
dbConfig: prod-db
configs:
staging-fe:
location: ${paths.deploy-config}/staging-frontend.yaml
prod-fe:
location: ${paths.deploy-config}/production-frontend.yaml
dev-db:
location: ${paths.local-scripts}/dev-database.yaml
prod-db:
location: ${paths.local-scripts}/prod-database.yaml
commandTemplatesPaths:
- ./templates/commands
commandFolderPaths:
- ./custom-commands
observableCommands:
- Deploy:Frontend
- Database:Backup
autostart:
Deploy:Frontend: staging
Database:Backup: development
The externalConfigPath
feature allows you to extend your current configuration with values from an external file. This powerful functionality enables modular and reusable configuration setups.
Key points:
externalConfigPath
property in the main configuration file.paths
section, e.g., ${paths.alias}
.Example:
externalConfigPath: ./external_config.yaml
The paths
section in the configuration file allows you to define key-value pairs for commonly used paths in your project. This feature provides a convenient way to reference and manage paths throughout your configuration.
Key features of the paths
section:
Path Aliases: Define aliases for frequently used paths in your project.
Variable Substitution: Use path aliases in other parts of the configuration using the ${paths.alias}
syntax.
Relative Path Resolution: Paths are resolved relative to the location of the configuration file.
Absolute Path Conversion: The system automatically converts relative paths to absolute paths for consistency.
Cross-Section Usage: Path aliases can be used in various sections of the configuration, including externalConfigPath
, commandTemplatesPaths
, commandFolderPaths
, and configs
.
Example usage:
paths:
deploy-config: ./../../deployment-configs
local-scripts: ./scripts
LOCAL_CONFIG: ./local_configs
externalConfigPath: ${paths.deploy-config}/external_config.yaml
commandTemplatesPaths:
- ${paths.local-scripts}/templates
configs:
xyz:
location: ${paths.LOCAL_CONFIG}/creds.yaml
In this example, ${paths.deploy-config}
will be replaced with the absolute path to ./../../deployment-configs
relative to the config file’s location.
The ConfigManager
class in config.ts
handles the resolution and validation of these paths:
resolvePathAliases
method converts relative paths to absolute paths.applyPathAliases
method replaces path aliases in the configuration with their resolved absolute paths.replacePathAliases
method handles the actual string replacement of path aliases.By using the paths section, you can maintain a centralized list of important directories in your project, making it easier to update and manage paths across your entire configuration.
The commands section in the configuration file allows you to define a key value pair of commands with various properties. Each command is identified by a unique name (it’s key) and can have multiple attributes to customize its behavior.
Example:
Test:School:1
will be displayed as
red
: Error foreground colorgreen
: Test passed colorblue
: Debug start coloryellow
: Warning colororange
: Warning notification colorpurple
: Text link colorgray
: Description text colorExample:
commands:
Deploy:Frontend:
color: green
command: npm run deploy
Database:Backup:
color: orange
command: pg_dump -U ${DB_USER}
"bash"
: Starts bash with your profile (bash -l -c
)"vscode"
: Uses VS Code integrated terminal"external"
: Uses system default or custom external terminal(In development)
autocloseCommand
: Boolean flag. If true, the terminal will close as soon as the command is done (planned feature).importFrom
: An array of commands to import parameters, configuration maps, or sessions from.extend
: Extend the command with additional settings from another command. All fields will be loaded from the extended command and overwritten by the current command.skipTerminalAutofocus
: Boolean flag. If true, the terminal or background logs will not automatically gain focus when the command starts.enableLog
: Boolean flag. If true, command output will be saved to log files in the configured log directory.informationalCommandSettings
: Configure settings for informational commands
fileWatcher
: Array of file patterns to watch for changes. When matching files are created or modified, the command automatically re-executes.interval
: The interval in seconds for refreshing the command.commandTrigger
: Watch for triggers of the command to re-execute.order
: The order in which the command should be shown in the UI.Example:
commands:
System:Status:
isInformational: true
command: systemctl status myservice
informationalCommandSettings:
fileWatcher:
- "**/status/*.log"
- "**/health/*.json"
configMap
: Allows the use of dynamic config files. Values can be referenced in the command or params.params
: Defines parameters for the command. Supports simple values and complex references using configs.requiredParams
: List of parameters that must be provided for the command to execute.The paramOptions
field allows you to define metadata and validation rules for command parameters:
commands:
Deploy:App:
command: deploy.sh --env ${environment} --mode ${mode} --debug ${enableDebug}
paramOptions:
environment:
type: enum
description: Target deployment environment
enumValues: ['dev', 'staging', 'prod']
required: true
mode:
type: string
description: Deployment mode
default: 'standard'
enableDebug:
type: boolean
description: Enable debug logging
default: false
Supported options for each parameter:
type: Defines the parameter type (‘string’ | ‘number’ | ‘boolean’ | ‘enum’) |
sessions
: Defines environment-specific configurations that will overwrite configs or config maps when running with a particular session.template
: Optional template name that this command is based on.Commands can include subcommands, which are executed before the main command. This feature allows for creating complex command structures and workflows.
Key aspects of subcommands:
Definition: Subcommands are defined within a command using the subCommands
key, which contains an array of subcommand definitions.
commandName
: The name of the existing command to be executed as a subcommand.params
(optional): Parameters specific to this subcommand execution.configMap
(optional): Configuration map specific to this subcommand execution.session
(optional): Session information for this subcommand.Execution Order: Subcommands are executed in the order they are listed, before the main command.
Example:
Subcommand Command:
command: echo "Started group"
params:
a: b
configMap:
cnf: cool
subCommands:
- commandName: Test:Echo config map
- commandName: Test:Echo config map
configMap:
cnf: bar
- commandName: Test:Echo var
- commandName: Test:Echo var
params:
echo: bar
- commandName: Test:Echo
- commandName: Inherit:1
session:
params:
configMap:
In this example:
This subcommand feature enables the creation of complex command sequences and allows for fine-grained control over parameter and configuration inheritance within command groups.
The configs
section in the configuration file allows you to define external configuration files that can be used across your commands. This feature provides a flexible way to manage environment-specific settings and variables.
location
: The path to the configuration file. This can be an absolute path or a relative path from the main config file’s location.location
can use path aliases defined in the paths section, allowing for more flexible and maintainable configurations.configs.<config-name>.<variable-name>
.Example usage:
configs:
staging-fe:
location: ${paths.deploy-config}/staging-frontend.yaml
prod-fe:
location: ${paths.deploy-config}/production-frontend.yaml
dev-db:
location: ${paths.local-scripts}/dev-database.yaml
prod-db:
location: ${paths.local-scripts}/prod-database.yaml
commands:
Deploy:Frontend:
command: npm run deploy
configMap:
envConfig: staging-fe
params:
DB_USER: configs.envConfig.DB_USERNAME
Database:Backup:
command: pg_dump -U ${DB_USER} -d ${DB_NAME} > backup_${curDateTime}.sql
configMap:
dbConfig: dev-db
params:
DB_USER: configs.dbConfig.username
DB_NAME: myapp_db
In this example, different configuration files are specified for staging and production environments. The Deploy:Frontend
command uses the staging-fe
config by default, while the Database:Backup
command uses the dev-db
config.
By using the configs section, you can maintain separate configuration files for different environments or components of your project, making it easier to manage complex setups and switch between different configurations as needed.
Configs can be dynamically used in combination with sessions and config maps to create flexible, environment-specific command configurations. This powerful feature allows you to easily switch between different settings based on the execution context.
Example usage:
commands:
Deploy:Frontend:
command: npm run deploy --env=${ENV_TYPE}
configMap:
envConfig: staging-fe
params:
ENV_TYPE: configs.envConfig.environment
sessions:
staging:
configMap:
envConfig: staging-fe
production:
configMap:
envConfig: prod-fe
configs:
staging-fe:
location: ${paths.deploy-config}/staging-frontend.yaml
prod-fe:
location: ${paths.deploy-config}/production-frontend.yaml
# Content of staging-frontend.yaml
variables:
environment: staging
# Content of production-frontend.yaml
variables:
environment: production
In this example:
Deploy:Frontend
command uses a configMap
to alias envConfig
to staging-fe
by default.ENV_TYPE
parameter is set to configs.envConfig.environment
, which will resolve to the environment variable in the selected config file.staging
session, it uses the staging-fe
config.production
session, it switches to the prod-fe
config.ENV_TYPE
parameter will resolve to “staging” or “production” based on the selected session, without changing the command definition.This setup allows you to easily switch between different environments by simply changing the session, while keeping the command structure consistent. It provides a powerful way to manage environment-specific configurations and promotes code reuse across different deployment scenarios.
Command Templates provide a powerful way to define reusable command structures that can be inherited by other commands. This feature allows for efficient configuration management and promotes code reuse across different command definitions.
Key aspects of Command Templates:
Definition: Command templates are defined in separate YAML files located in directories specified by the commandTemplatesPaths
configuration.
Structure: A command template has the same fields as a regular command but may omit certain fields. It serves as a base structure for other commands.
Inheritance: Commands can inherit from templates using the template field, which specifies the name of the template to use.
Overriding: Commands that use a template can override any field defined in the template, allowing for customization.
Loading: Templates are loaded automatically from the specified commandTemplatesPaths
directories.
Flexibility: Templates can define partial command configurations, which can be completed or modified by the inheriting commands.
Example of a Command Template:
DB:Gen Connection String URL:
command: |-
echo ${URL};
requiredParams:
- PORT
params:
ORB_DB_USERNAME: configs.envConfig.values.ORB_DB_USERNAME
sessions:
shadow:
configMap:
envConfig: ShadowFe
qa:
configMap:
envConfig: QaFe
prod:
params:
envConfig: ProdFe
Usage in a Command:
commands:
Generate:DB:URL:
template: DB:Gen Connection String URL
params:
PORT: 5432
URL: "postgresql://${ORB_DB_USERNAME}:${ORB_DB_PASSWORD}@${ORB_DB_HOST}:${PORT}/${ORB_DB_NAME}"
By leveraging Command Templates, you can create a more modular and maintainable command configuration system, promoting best practices and reducing potential errors in command definitions.
The Observable Commands feature allows you to specify a list of commands that can be monitored and tracked in real-time through the VSCode status bar. This feature provides quick visibility into the state of important processes in your project.
observableCommands
key. This is an array of command names that you want to monitor.Visual Indicators: Each Observable Command is displayed with an icon indicating its current status:
Example configuration:
observableCommands:
- Deploy:Frontend
- Database:Backup
- API:HealthCheck
By utilizing Observable Commands, you can keep track of critical processes in your project without the need to manually check their status repeatedly. This feature enhances your workflow by providing at-a-glance information about key commands and allowing quick interaction when needed.
The Autostart feature allows you to configure commands to start automatically when the application launches. This is particularly useful for setting up your development environment or initializing key processes without manual intervention.
Autostart is defined in the main configuration file as a key-value pair object:
autostart:
CommandName1: SessionName
CommandName2: null
Example
autostart:
Deploy:Frontend: staging
Database:Backup: null
In this example:
Deploy:Frontend
command will automatically start with the staging session.Database:Backup
command will start for all available sessions.defaultSession
: Specifies the default session to use when none is explicitly set.autoSelectDefaultSession
: When set to true, automatically selects the default session without prompting.By utilizing the Autostart feature, you can significantly streamline your development workflow and ensure that critical processes are initialized consistently across your team or deployment environments.
The extension supports comprehensive logging capabilities for command outputs:
NOTE: This feature is NOT available for background commands. Use outputs tab for background commands.
Enable logging for specific commands by setting the enableLog
flag in the command configuration:
commands:
Deploy:Frontend:
enableLog: true
command: npm run deploy
Logs are stored in one of two locations:
Example:
"envTrack.logFolder": "./my-custom-logs"
If no custom location is specified, logs are stored in .vscode/logs
Command logs can be accessed through:
(To be expanded)
(To be expanded)