config
This is a built-in CLI process used to retrieve final config objects in various formats OR initialize a single/environment configuration.
Alias:
c
Initialize
mhy
is able to generate your config files for your environment. Use it to initialize your environment in your IDE/Editor.
By default mhy
won't overwrite your existing configuration files, it gives you a warning instead. In case you want to overwrite anyway, use the -o --overwrite
flag.
Use the -i --init
flag to initialize a single configuration file only.
Get
In certain situations you might want to initialize only a certain configuration or just want to check the final object mhy
will use.
Log out a single config object:
You may use the -f --format
flag to change the output format:
You might want to write the contents into a file, you just need to use the > operator which works both on Windows and Linux:
Available formats:
js
plain JavaScript objectmodule.exports = module.exports.default = {}
mjs
plain JavaScript objectexport default {}
json
plain JSON objectraw
try to print out as is
Set
Setting configuration values can be done in multiple ways. Before showing those, let's get famimilar with the configuration loading flow and some definitions first.
Entities
root
entities are being loaded alwaysenv
entities are being loaded if the current environment matches
Directory structure
This is an example of webpack
's structure:
mhy
will recursively load all configuration values from the directory structure recursively.
File names are becoming keys in the final object and folders are becoming arrays.
Example
From the directory structure above you will end up having the following object:
For further examples check out mhy
's repository.
Flow
Load
root
frommhy
Load
root
fromlocal
Load
root
frompackage.json
Load
env
frommhy
Load
env
fromlocal
Load
env
frompackage.json
Overriding
Using package.json (recommended)
mhy
's practice to to keep every configuration in the package.json
of the project. You can manipulate almost every aspect of a configariation object through JSON, even removals, appends, replaces, pushes or even searching and running JavaScript code.
All mhy
related values are being stored under the key "mhy"
in your package.json
. The structure is the same. By default your object basically are being deep merged.
mhy
is coming with support for json-merger which enables manipulation of JSON data easily without using JavaScript.
Let's assume the same webpack
directory structure as above and the follwing in out package.json
file:
The result for webpack
will be:
Using files
This method is for more advanced cases or those who prefer to manage their configs in files.
mhy
will search a .mhy
directory in your project root. There you need to have a config directory with the same environment base as in the previous examples:
In your files you need to default export a function that is returning the value. In the first parameter you'll also get the current value of the key if there's any.
Please note that there will be no code transformations applied to your custom config files. Only use syntax/features that are available in your current NodeJS version.
Local processes/commands
Using this method you can have your own, local processes/commands as well. More info on this is about to come, stay tuned!
Last updated