From 5342c22b9731ba9c8a48d5de1f6d1e92220899a4 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 27 Aug 2024 13:46:11 +0200 Subject: [PATCH] src: add config file support --- doc/api/cli.md | 53 ++++++++ doc/node.1 | 3 + doc/node_config_json_schema.json | 16 +++ lib/internal/process/pre_execution.js | 8 ++ node.gyp | 2 + src/node.cc | 21 +++ src/node_options.cc | 3 + src/node_options.h | 1 + src/node_rc.cc | 127 ++++++++++++++++++ src/node_rc.h | 36 +++++ .../dotenv/node-options-no-tranform.env | 1 + test/fixtures/rc/empty-object.json | 4 + test/fixtures/rc/empty.json | 1 + test/fixtures/rc/invalid-import.json | 3 + test/fixtures/rc/loaders.json | 7 + test/fixtures/rc/override-property.json | 4 + test/fixtures/rc/transform-types.json | 3 + test/parallel/test-config_rc.js | 125 +++++++++++++++++ 18 files changed, 418 insertions(+) create mode 100644 doc/node_config_json_schema.json create mode 100644 src/node_rc.cc create mode 100644 src/node_rc.h create mode 100644 test/fixtures/dotenv/node-options-no-tranform.env create mode 100644 test/fixtures/rc/empty-object.json create mode 100644 test/fixtures/rc/empty.json create mode 100644 test/fixtures/rc/invalid-import.json create mode 100644 test/fixtures/rc/loaders.json create mode 100644 test/fixtures/rc/override-property.json create mode 100644 test/fixtures/rc/transform-types.json create mode 100644 test/parallel/test-config_rc.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 9c5af91ae13f91..91e36644a1d6ec 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -911,6 +911,59 @@ added: v23.6.0 Enable experimental import support for `.node` addons. +### `--experimental-config-file` + + + +> Stability: 1.0 - Early development + +Use this flag to specify a configuration file that will be loaded and parsed +before the application starts. +Node.js will read the configuration file and apply the settings. +The configuration file should be a JSON file +with the following structure: + +```json +{ + "experimental_transform_types": true, + "import": [ + "amaro/transform" + ] +} +``` + +Each key in the configuration file corresponds to a flag that can be passed +as a command-line argument. The value of the key is the value that would be +passed to the flag. + +For example, the configuration file above is equivalent to +the following command-line arguments: + +```bash +node --experimental-transform-types --import amaro/transform +``` + +The priority in configuration is as follows: + +* NODE\_OPTIONS and command-line options +* Config file +* Dotenv NODE\_OPTIONS + +If multiple keys are present in the configuration file, +the last one will override the previous ones. +Unknown keys will be ignored. + +It possible to use the official json schema to validate the configuration file, +which may vary depending on the Node.js version. + +```json +{ + "$schema": "https://nodejs.org/dist/REPLACEME/node_config_json_schema.json", +} +``` + ### `--experimental-eventsource`